相关jar包
<!--提示配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>3.2.1</version>
</dependency>
<!--自动配置注解-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.2.1</version>
</dependency>
<!--提示配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>3.2.1</version>
</dependency>
<!--自动配置注解-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.2.1</version>
</dependency>
项目模块
freedom-springboot-starter模块
实体配置类
@ConfigurationProperties(prefix = "freedom")
public class MyProperties {
private String name ;
private int age ;
private String address ;
public String getName () {
return name;
}
public int getAge () {
return age;
}
public void setName (String name) {
this.name = name;
}
public void setAge (int age) {
this.age = age;
}
public String getAddress () {
return address;
}
public void setAddress (String address) {
this.address = address;
}
}
config配置
@Configuration
@EnableConfigurationProperties(value = MyProperties.class)
public class FreedomConfig {
private final MyProperties myProperties ;
public FreedomConfig (MyProperties myProperties) {
this.myProperties = myProperties;
}
@Bean
public DemoService initBean(){
System.out.println("myProperties = " + myProperties.getName());
return new DemoService(myProperties);
}
}
service注入类
public class DemoService {
private MyProperties myProperties ;
public DemoService (MyProperties myProperties) {
this.myProperties = myProperties;
}
public String getName(){
return myProperties.getName();
}
}
实体配置类
@ConfigurationProperties(prefix = "freedom")
public class MyProperties {
private String name ;
private int age ;
private String address ;
public String getName () {
return name;
}
public int getAge () {
return age;
}
public void setName (String name) {
this.name = name;
}
public void setAge (int age) {
this.age = age;
}
public String getAddress () {
return address;
}
public void setAddress (String address) {
this.address = address;
}
}
config配置
@Configuration
@EnableConfigurationProperties(value = MyProperties.class)
public class FreedomConfig {
private final MyProperties myProperties ;
public FreedomConfig (MyProperties myProperties) {
this.myProperties = myProperties;
}
@Bean
public DemoService initBean(){
System.out.println("myProperties = " + myProperties.getName());
return new DemoService(myProperties);
}
}
service注入类
public class DemoService {
private MyProperties myProperties ;
public DemoService (MyProperties myProperties) {
this.myProperties = myProperties;
}
public String getName(){
return myProperties.getName();
}
}
自动配置
resources包下
META_INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
加入需要配置的配置类
springboot-starter-test模块
>在test中测试引用
public static void main (String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringbootStarterTestApplication.class,args);
DemoService bean = run.getBean(DemoService.class);
String name = bean.getName();
System.out.println("name = " + name);
}
yml文件配置
标签:myProperties,String,自定义,MyProperties,DemoService,name,2.7,public,springboot
From: https://www.cnblogs.com/freedomlog/p/18014146
public static void main (String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringbootStarterTestApplication.class,args);
DemoService bean = run.getBean(DemoService.class);
String name = bean.getName();
System.out.println("name = " + name);
}
yml文件配置