1 @ConfigurationProperties注解
1.1 作用
引入配置,作用与@Value类似
1.2 如何使用
第一步:建立.yml配置文件
wechat:
mp:
appId: "wx0679118707e3b4a1"
secret: "2683b0f6f22443b579cd0b6f7e8a4df0"
token: "shaocheng"
第二步:在类名上加@ConfigurationProperties(prefix = "wechat.mp")
,还需要加上@Component
,不然使用@Autowired
无法完成自动注入
@ConfigurationProperties(prefix = "wechat.mp")
@Component
@Getter//lombok插件
@Setter//lombok插件
public class WechatMpProperties {
/**
* 设置微信公众号的appid
*/
private String appId;
/**
* 设置微信公众号的app secret
*/
private String secret;
/**
* 设置微信公众号的token
*/
private String token;
}
第三步:在controller中直接使用@Autowired注解注入
@RestController
@RequestMapping("/wechat/mp")
public class WechatController {
@Autowired
private WechatMpProperties wechatMpProperties;
@GetMapping(produces = "text/plain;charset=utf-8")
public String getPropeties(){
return wechatMpProperties.toString();
}
}
2 @Bean注解
@Bean注解在方法上,返回值是一个类的实例,并声明这个返回值(返回一个对象)是spring容器管理一个bean。
@Bean
public WxMpConfigStorage configStorage() {
WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
configStorage.setAppId(this.properties.getAppId());
configStorage.setSecret(this.properties.getSecret());
configStorage.setToken(this.properties.getToken());
configStorage.setAesKey(this.properties.getAesKey());
return configStorage;
}
上面方法中的返回值WxMpConfigStorage即为spring管理的Bean。成为了spring容器管理的bean实体就不用@Component
和@ConfigurationProperties
注解了。
3 @Configuration注解
这是一个配置类,与@Service、@Component的效果类似。spring会扫描到这个类,@Bean才会生效。
被@Configuration注解标识的类,通常作为一个配置类,这就类似于一个xml文件,表示在该类中将配置Bean元数据,其作用类似于Spring里面application-context.xml的配置文件,而@Bean标签,则类似于该xml文件中,声明的一个bean实例。
4 @EnableConfigurationProperties注解
此注解,没太明白具体有啥子用?贴出官方解释:
The @EnableConfigurationProperties annotation is also automatically applied to your project so that any existing bean annotated with @ConfigurationProperties will be configured from the Environment.
最近又捣鼓了下这个注解,明白了其用法,分享下。
首先是引入配置信息的类WechatMpProperties,上文中加上了@Component,之前也说过如果不加@Component,是无法在其他地方完成@Autowired自动注入的,但是在需要引用的类上加上@EnableConfigurationProperties注解就可以完成自动注入了
代码如下:
//该类中不加@Component注解
@ConfigurationProperties(prefix = "wechat.mp")
@Getter//lombok插件
@Setter//lombok插件
public class WechatMpProperties {
private String appId;
private String secret;
private String token;
}
@Configuration
@ConditionalOnClass(WxMpService.class)
@EnableConfigurationProperties(WechatMpProperties.class)
public class WechatMpConfiguration {
@Autowired
private WechatMpProperties properties;
...
}
WechatMpConfiguration类中,加上@EnableConfigurationProperties(WechatMpProperties.class)注解即可完成WechatMpProperties的自动注入。
5 @ConditionalOnClass
表示存在对应的Class文件时才会去解析该注解所修饰的类,否则直接跳过不解析。
@Configuration
@ConditionalOnClass(WxMpService.class)
@EnableConfigurationProperties(WechatMpProperties.class)
public class WechatMpConfiguration {
@Autowired
protected LogHandler logHandler;
......
}
上面表示存在WxMpService.class文件时才解析WechatMpConfiguration,否则直接跳过不解析。
6 @ConditionalOnMissingBean
如果存在指定的bean,则该注解标注的bean不创建。
@Configuration
@ConditionalOnClass(WxMpService.class)
@EnableConfigurationProperties(WechatMpProperties.class)
public class WechatMpConfiguration {
@Autowired
protected LogHandler logHandler;
...
@Bean
@ConditionalOnMissingBean
public WxMpConfigStorage configStorage() {
WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
configStorage.setAppId(this.properties.getAppId());
configStorage.setSecret(this.properties.getSecret());
configStorage.setToken(this.properties.getToken());
configStorage.setAesKey(this.properties.getAesKey());
return configStorage;
}
...
}
如果存在WxMpConfigStorage对象,那个该注解标注的Bean就不创建。