使用@PropertySource加载自定义配置文件
(一)创建Spring Boot Web项目ConfigDemo01
添加项目依赖
创建自定义配置文件
在resources下创建myconfig.properties文件
student.id=20190101
student.name=Howard
student.age=18
创建自定义配置类
在net.hw.lesson04包里创建配置类StudentConfig
package net.qyk.lesson04.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 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 +
'}';
}
}
编写测试方法
点开测试类ConfigDemo01ApplicationTests
package net.qyk.lesson04;
import net.qyk.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 Comfigdemo01ApplicationTests {
@Autowired
private StudentConfig studentConfig;
@Test
void contextLoads() {
}
@Test
public void testStudentConfig(){
System.out.println(studentConfig);
}
}
运行测试方法
使用@ImportResource加载XML配置文件
(一)创建创建Spring Boot Web项目ConfigDemo02
设置项目元数据
创建自定义服务类
在net.qyk.lesson04包里创建service子包,在子包里创建CustomService类
import org.springframework.stereotype.Service;
/**
* 功能:自定义服务类
* 作者:华卫
* 日期:2021年05月07日
*/
public class CustomService {
public void welcome() {
System.out.println("欢迎您访问泸州职业技术学院~");
}
}
创建Spring配置文件
在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 name="customService" class="net.qyk.lesson04.service.CustomService"/>
</beans>
在启动类上添加注解,加载自定义Spring配置文件
在启动类上添加注解@ImportResource(“classpath:spring-config.xml”)
package net.qyk.lesson04;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:spring-config.xml")
public class Configdemo02Application {
public static void main(String[] args) {
SpringApplication.run(Configdemo02Application.class, args);
}
}
打开测试类,编写测试方法
点开测试类ConfigDemo02ApplicationTests
package net.qyk.lesson04;
import net.qyk.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(){
customService.welcome();
}
}