几种方法
- 使用@PropertySource加载配置文件
- 使用@ImportResource加载XML配置文件
- 使用@Configuration编写自定义配置类
使用@PropertySource加载自定义配置文件
一、创建项目ConfigDemo01


二、创建自定义配置文件-myconfig.properties


- 编辑属性文件值的时候不允许出现user.name,会与系统属性冲突。如果属性值含有中文,必须采用Unicode编码,否则乱码。
转码: 打开Terminal面板,输入以下命令,将转码结果粘贴至你创建的properties文件中即可(事先IDE编码为utf-8)
cd src/main/resources
native2ascii -encoding utf8 配置文件名.properties
三、创建学生配置类-StudentConfig
- 创建config子包

package net.lj.lesson4.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* 学生配置类
*/
@Component//交给Spring容器管理
@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 +
'}';
}
}
四、打开测试类编辑测试方法

package net.lj.lesson4;
import net.lj.lesson4.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
public void testStudentConfig(){
//输出学生配置实体信息
System.out.println(studentConfig);
}
}
运行测试

将结果输出到网页界面
- 创建ConfigDemoController.java

package net.lj.lesson4.controller;
import net.lj.lesson4.config.StudentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
//@Controller //使用此注释必须同时加上@ResponseBody注释
public class ConfigDemoController {
@Autowired
private StudentConfig studentConfig;
// @ResponseBody
@GetMapping("/student")
public String student() {
return studentConfig.toString();
}
}
- 运行项目,在浏览器地址栏输入http://localhost:8080/student

使用@ImportResource加载XML配置文件
一、创建项目ConfigDemo02


二、创建自定义服务类
- 创建service子包

package net.lj.lesson04.service;
/**
* 自定义服务类
*/
public class CustomService {
public void welcome() {
System.out.println("欢迎来到Spring Boot世界");
}
}
三、创建自定义Bean配置文件

- 定义一个bean并指明路径
<bean name="customService" class="net.lj.lesson04.service.CustomService"></bean>
四、在启动类上加载自定义Bean配置文件的注释
- 使Spring容器中自动实例化一个名为customService的Bean

五、编写测试类
package net.lj.lesson04;
import net.lj.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
public void testCustomService(){
customService.welcome();
}
}
运行测试方法

使用@Configuration编写自定义配置类
一、创建项目ConfigDemo03


二、创建自定义服务类
- 创建service子包

package net.lj.lesson04.service;
/**
* 自定义服务类
*/
public class CustomService {
public void welcome() {
System.out.println("欢迎来到Spring Boot世界");
}
}
三、创建自定义配置类CustomConfig
- 创建config子包

package net.lj.lesson04.config;
/*
*自定义配置类
*/
import net.lj.lesson04.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();
}
}
四、编写测试方法
package net.lj.lesson04;
import net.lj.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 customService;
@Test
public void testCustomService(){
customService.welcome();
}
}
运行测试方法

本文介绍了三种在Spring Boot中加载自定义配置的方法:@PropertySource加载properties文件,@ImportResource加载XML配置文件,以及使用@Configuration编写自定义配置类。通过详细步骤展示了如何创建项目、配置文件,以及编写测试类进行验证。
1187

被折叠的 条评论
为什么被折叠?



