文章目录
零、学习目标
一、为什么要使用自定义配置
二、使用@PropertySource加载自定义配置文件
(1).创建Spring Boot Web项目ConfigDemo01
–** 为了在属性文件中使用中文,设置一下idea如下
(2)在resources下创建myconfig.properties文件
–说明:如果在配置文件里使用user.name,通过配置文件获取的值可能是操作系统中的用户名,因为操作系统中也是有user.name属性的。
(3)创建自定义配置类
–**在net.dw.boot.config包里创建配置类StudentConfig
package net.dw.boot.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:myconfig.properties") //加载自定义配置文件
@ConfigurationProperties(prefix = "student")
public class StudentConfig {
private String id; //学号
private String name; //姓名
private String gender; //性别
private int age;//年龄
@Override
public String toString() {
return "StudentConfig{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + 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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
(4)编写测试方法
–编写测试方法,注入学生配置实体,创建testStudentConfig()测试方法,在里面输出学生配置实体信息
(5)修改测试方法代码
–说明:注入的StudentConfig名称不必是studentConfig,在Spring Boot 2.4.5里,StudentConfig的注解@Component默认是单例的,因此不会因为注入名称是studentConfig1而产生的两个StudentConfig实例。
–可以看到,StudentConfig注入名称改成studentConfig1之后,测试结果依然相同,不受注入名称变化的任何影响。
(6)在web上显示
–创建controller子包,在子包里控制器ConfigDemo01Controller
package net.dw.boot.controller;
import net.dw.boot.config.StudentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigDemo01Controller {
@Autowired
private StudentConfig studentConfig;
@RequestMapping(value = "/student",method = RequestMethod.GET)
public String student(){
return studentConfig.toString();
}
}
–在浏览器里访问http://localhost:8080/student
三、使用@ImportResource加载XML配置文件
(1)、建创建Spring Boot Web项目ConfigDemo02
(2)在net.dw.boot包里创建service子包,在子包里创建CustomService类
package net.dw.boot.service;
public class CustomService {
public void welcome(){
System.out.println("welcome to this place");
}
}
(3)在resources目录里创建配置文件spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 定义一个Bean,指定Bean的名称及类所在的路径-->
<bean name="customService" class="net.dw.boot.service.CustomService"/>
</beans>
(4).在启动类上添加注解,加载自定义Spring配置文件
–在启动类上添加注解@ImportResource(“classpath:spring-config.xml”)
–在Spring Boot启动后,Spring容器中就会自动实例化一个名为customService的Bean对象
(5)打开测试类,编写测试方法
–点开测试类ConfigDemo02ApplicationTests
package net.dw.boot;
import net.dw.boot.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(){
customService.welcome();
}
}
四、使用@Configuration编写自定义配置类
–使用@Configuration编写自定义配置类,这是Spring Bboot的推荐方式
(1)创建Spring Boot Web项目ConfigDemo03
(2)新建service包并创建自定义服务类CustomService类![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/6b3346099bbcc109ee2ef0f910b4a418.png)
package net.dw.boot.service;
public class CustomService {
public void welcome() {
System.out.println("welcome to the famous place ");
}
}
(3)创建自定义配置类CustomConfig
–在net.dw.boot包里创建config子包,在子包里创建自定义配置类CustomConfig
–添加注解@Configuration,指定配置类
package net.dw.boot.config;
import net.dw.boot.service.CustomService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomConfig {
@Bean(name = "cs") //指定Bean的名称为“cs” ,否则采用默认的customService 作为名称
public CustomService getCustomService(){
return new CustomService();
}
}
(4)打开测试类,编写测试方法
–** 注入在CustomConfig配置类里定义的Bean,创建测试方法testCustomService(),然后调用自定义Bean的方法**
package net.dw.boot;
import net.dw.boot.service.CustomService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ConfigDome03ApplicationTests {
@Autowired
private CustomService cs;
@Test
void contextLoads() {
}
@Test
public void testCustomService() {
cs.welcome();
}
}