Spring Boot加载自定义配置文件-----学习05

一、使用@PropertySource加载自定义配置文件

1、创建项目

在这里插入图片描述
在这里插入图片描述

2、初始化

在这里插入图片描述

3、创建自定义配置文件

  • 在resources下创建myconfig.properties文件
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
student.id=20190101
student.name=Howard
student.age=18

注意:如果有中文必须采用unicode,否则乱码

4、创建自定义配置类

(1)、在main/java下创建net.lesson04.config子包

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

(2)、创建配置类StudentConfig

在这里插入图片描述
在这里插入图片描述

package net.lesson04.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 功能:学生配置类
 * 作者:tp
 * 日期:2021年05月07日
 */
@Component //交给spring容器管理
@PropertySource("classpath:myconfig.properties")//加载自定义配置文件
@ConfigurationProperties(prefix = "student") // 此注解必须要Component注解
public class StudentConfig {
    private String id;
    private String name;
    private int age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "StudentConfig{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

5、编写测试方法

在这里插入图片描述

package net.tp.lesson04;

import net.tp.lesson04.config.StudentConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest

class ConfigDemo01ApplicationTests {

    @Autowired  // 注入学生配置实体
    private StudentConfig studentConfig;

    @Test
    void contextLoads() {
    }
    @Test
    public void testStudentConfig(){
        // 输出学生配置实体信息
        System.out.println(studentConfig);
    }

}

6、运行查看效果

在这里插入图片描述

7、修改测试方法代码

在这里插入图片描述

8、再次运行测试方法

在这里插入图片描述

二、课堂练习:在Web页面显示学生配置信息

1、在main/java下创建net.tp.lesson04.controller子包

在这里插入图片描述

2、创建控制器ConfigDemo01Controller

在这里插入图片描述

package net.tp.lesson04.controller;

import net.tp.lesson04.config.StudentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RestController
public class StudentController {
    @Autowired
    StudentConfig studentConfig;
    @ResponseBody
    @GetMapping("/student")
    public String student() {

        return "" +studentConfig ;

    }

}

3、运行查看效果

在这里插入图片描述

三、使用@ImportResource加载XML配置文件

1、Spring Boot Web项目ConfigDemo02

在这里插入图片描述

  • 添加依赖
    在这里插入图片描述

2、初始化

在这里插入图片描述

3、在main/java下创建net.tp.lesson.service子包

在这里插入图片描述
在这里插入图片描述

4、创建自定义服务类

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package net.tp.lesson04.service;

import org.springframework.stereotype.Service;

/**
 * 功能:自定义服务类
 *作者:tp
 * 日期:2021年05月07日
 */

public class CustomService {
    public void welcome() {
        System.out.println("欢迎您访问泸州职业技术学院");
    }
}

5、在resources目录里创建配置文件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

 <bean name="customService" class="net.tp.lesson04.service.CustomService"></bean>

6、在启动类上添加注解,加载自定义JavaBean配置文件

  • 在启动类上添加注解@ImportResource("classpath:spring-config.xml")

在这里插入图片描述
在这里插入图片描述

7、打开测试类,编写测试方法

在这里插入图片描述

package net.tp.lesson04;

import net.tp.lesson04.service.CustomService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ConfigDemo02ApplicationTests {
    // 注入自定义
    @Autowired
    private CustomService customService;

    @Test
    void contextLoads() {
    }
    @Test
    public void testCustomService(){
        // 调用自定义Bean的方法
        customService.welcome();
    }
}

8、运行查看效果

在这里插入图片描述

四、使用@Configuration编写自定义配置类

1、创建Spring Boot Web项目ConfigDemo03

在这里插入图片描述

  • 添加依赖
    在这里插入图片描述

2、初始化

在这里插入图片描述

3、在main/java下创建net.tp.lesson.service子包

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4、创建CustomService类

在这里插入图片描述
在这里插入图片描述

package net.tp.lesson04.service;

import org.springframework.context.annotation.Configuration;

/**
 * 功能:自定义配置类
 */

public class CustomService {
    public void welcome() {
        System.out.println("欢迎您访问泸职院信息工程学院");
    }
}

5、创建自定义配置类CustomConfig

在这里插入图片描述

package net.tp.lesson04.config;

import net.tp.lesson04.service.CustomService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CustomConfig {
    @Bean(name ="cs")
    public CustomService getCustomService(){
        return new CustomService();
    }
}

6、打开测试类,编写测试方法

在这里插入图片描述

package net.tp.lesson04;

import net.tp.lesson04.service.CustomService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ConfigDemo03ApplicationTests {
    @Autowired // 注入自定义服务类
    private CustomService customerService;

    @Test
    void contextLoads() {
    }
    @Test
    public void testCustomService() {
        customerService.welcome();
    }

}

7、运行查看效果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值