文章目录
一、使用@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();
}
}