我使用IntellijIdea和gradle。Gradle配置:
...
apply plugin: 'propdeps'
apply plugin: 'propdeps-idea'
apply plugin: 'propdeps-maven'
buildscript {
repositories {
maven { url 'http://repo.spring.io/plugins-release' }
}
dependencies {
classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.7'
}
}
compileJava.dependsOn(processResources)
dependencies {
...
optional group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: '1.4.0.RELEASE'
}
好的,要创建我自己的属性,我需要:
@Component
@ConfigurationProperties("own.prefix")
@Data
public class TestProps {
public String field;
}
@Configuration
@EnableConfigurationProperties(TestProps.class)
public class AppConf {}
在我重建项目spring-boot-configuration-processor生成新的META-INFO之后,因此在application.properties中,我可以使用own.prefix.field =,而Spring可以看到它。
但是我应该对第三方配置类怎么办?文档http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html说:
除了
@ConfigurationProperties
用于注释类之外,还可以在@Bean
方法上使用它。当您想将属性绑定到控件之外的第三方组件时,此功能特别有用。要从Environment属性配置Bean,请添加
@ConfigurationProperties
到其Bean注册中:@ConfigurationProperties(prefix = "foo") @Bean public FooComponent fooComponent() { ... }
使用foo前缀定义的任何属性都将以与上面的ConnectionProperties示例类似的方式映射到该FooComponent bean。
好的。我们试试吧。例如,我在gide(https://spring.io/guides/tutorials/spring-boot-oauth2/)中声明了bean :
@Configuration
@EnableOAuth2Sso
public class SocialConfig {
@Bean
@ConfigurationProperties("facebook.client")
OAuth2ProtectedResourceDetails facebook() {
return new AuthorizationCodeResourceDetails();
}
@Bean
@ConfigurationProperties("facebook.resource")
ResourceServerProperties facebookResource() {
return new ResourceServerProperties();
}
}
但是,在重建项目属性之后,我的application.properties中不存在facebook.client和facebook.resource。
我也尝试将SocialConfig.class添加到
@Configuration
@EnableConfigurationProperties(SocialConfig.class)
public class AppConf {}
重建后仍然无法正常工作。像这样:
@Configuration
@EnableConfigurationProperties
public class AppConf {}
还是一样。
我究竟做错了什么?也为我的英语感到抱歉:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句