================================
©Copyright 蕃薯耀 2021-12-22
蕃薯耀的博客_CSDN博客
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "lqy.java.auto", ignoreUnknownFields = true)
public class MyProperties {
private Integer id;
private String name;
private Map map;
private String from;
private boolean enable;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
@Override
public String toString() {
return "MyProperties [id=" + id + ", name=" + name + ", map=" + map + ", from=" + from + "]";
}
}
#lqy.java.auto.enable=false
lqy.java.auto.id=10
lqy.java.auto.name=张三aa
lqy.java.auto.map.a=a-一
lqy.java.auto.map.b=b-二
需要引入MyProperties:
@EnableConfigurationProperties(value = MyProperties.class)
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import lqy.java.validation.hibernate.bean.User;
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(value = MyProperties.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass(User.class)//User类在类路径中才生效
@ConditionalOnProperty(prefix = "lqy.java.auto", value="enable", havingValue = "true", matchIfMissing = true)
public class MyPropertiesAutoConfiguration {
@Bean
public User getUser(MyProperties myProperties) {
System.out.println("myProperties = " + myProperties);
User u = new User();
u.setId(myProperties.getId());
u.setName(myProperties.getName());
System.out.println("User u = " + u);
return u;
}
}
在src/main/resources/META-INF/的路径增加spring.factories文件,没有META-INF则新建
src/main/resources/META-INF/spring.factories
spring.factories文件的内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
lqy.java.validation.hibernate.auto.MyPropertiesAutoConfiguration
反斜杠(\)表示接下一行,即可以换行
springboot @ConfigurationProperties 中文乱码解决方案见:springboot @ConfigurationProperties 中文乱码解决方案_蕃薯耀的博客-CSDN博客================================©Copyright 蕃薯耀2021-12-22蕃薯耀的博客_CSDN博客一、springboot @ConfigurationProperties 中文乱码问题1、application.properties文件:lqy.java.auto.id=10lqy.java.auto.name=张三aalqy.java.auto.map.a=a-一lqy.java.auto.map.b=b-二2、...https://blog.csdn.net/w995223851/article/details/122077774?spm=1001.2014.3001.5501
(时间宝贵,分享不易,捐赠回馈,^_^)
================================
©Copyright 蕃薯耀 2021-12-22
蕃薯耀的博客_CSDN博客