resources
下创建myconfig.properties
文件student.id=20210101
student.name=张三丰
student.gender=男
student.age=18
net.shuai.boot
包里创建配置类config.StudentConfig
package nei.shuai.boot.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 String gender;
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
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 + '\'' +
", gender=" + gender + '\'' +
", age=" + age +
'}';
}
}
ConfigDemo01ApplicationTests
testStudentConfig()
测试方法,在里面输出学生配置实体信息package nei.shuai.boot;
import nei.shuai.boot.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);
}
}
StudentConfig
名称不必是studentConfig
,在Spring Boot 2.4.5里,StudentConfig的注解@Component
默认是单例的,因此不会因为注入名称是studentConfig1
而产生的两个StudentConfig
实例。controlle
r子包,在子包里控制器StudentConfigController
package nei.shuai.boot.controller;
import nei.shuai.boot.config.StudentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class StudentConfigController {
@Autowired // 自动装配学生配置实体
private StudentConfig studentConfig;
@RequestMapping("/student")
@ResponseBody
public String student() {
return studentConfig.toString();
}
}
package nei.shuai.boot.controller;
import nei.shuai.boot.config.StudentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class StudentConfigController {
@Autowired // 自动装配学生配置实体
private StudentConfig studentConfig;
@RequestMapping("/student")
@ResponseBody
public StudentConfig student() {
return studentConfig;
}
}
package nei.shuai.boot.controller;
import nei.shuai.boot.config.StudentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class StudentConfigController {
@Autowired // 自动装配学生配置实体
private StudentConfig studentConfig;
@RequestMapping("/student")
@ResponseBody
public String student() {
return "学号:" + studentConfig.getId() + "
" +
"姓名:" + studentConfig.getName() + "
" +
"性别:" + studentConfig.getGender() + "
" +
"年龄:" + studentConfig.getAge();
}
}
net.shuai.boot
包里创建service
子包,在子包里创建CustomService
类package net.shuai.boot.service;
/**
* 功能:自定义服务类
*/
public class CustomService {
public void welcome() {
System.out.println("欢迎您访问泸州职业技术学院~");
}
}
<bean id="customService" class="net.shuai.boot.service.CustomService"/>
@ImportResource("classpath:config/spring-config.xml")
package net.shuai.boot;
import net.shuai.boot.service.CustomService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Currency;
@SpringBootTest
class ConfigDemo02ApplicationTests {
@Autowired // 注入自定义的服务实体类
private CustomService customService;
@Test
public void testCustomService() {
// 调用自定义服务实体的方法
customService.welcome();
}
}
net.shuai.boot
包里创建service
子包,在子包里创建CustomService
类package net.shuai.boot.service;
/**
* 功能:自定义服务类
*/
public class CustomService {
public void welcome() {
System.out.println("欢迎您访问泸州职业技术学院~");
}
}
net.shuai.boot
包里创建config
子包,在子包里创建自定义配置类CustomConfig
@Configuration
,指定配置类Bean
的方法getCustomService()
package net.shuai.boot.config;
import net.shuai.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();
}
}
ConfigDemo03ApplicationTests
CustomConfig
配置类里定义的Bean
,创建测试方法testCustomService()
,然后调用自定义Bean
的方法package net.shuai.boot;
import net.shuai.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 ConfigDemo03ApplicationTests {
@Autowired // 注入自定义服务类
private CustomService customService;
@Test
public void testCustomService() {
customService.welcome();
}
}