我可以使用注释向上下文添加单个属性吗?

时间:2017-04-16 14:10:15

标签: java spring annotations

我可以使用

添加多个属性
@Configuration
@PropertySource("classpath:application.properties")
@ComponentScan
public class StarterAppConfig {
}

我可以直接从注释中明确添加一个或多个属性吗?

像这样:

@Configuration
@Property("user", "myuser")
@Property("password", "mypassword")
@ComponentScan
public class StarterAppConfig {
}

感谢。

更新

所以,我要写my own annotation SinglePropertyown processing for it

2 个答案:

答案 0 :(得分:0)

您可以使用@Value并为属性设置默认值,请参阅此处的示例:http://www.mkyong.com/spring3/spring-value-default-value/

答案 1 :(得分:0)

您可以在主课程中使用

Map<String,Object> defaultProperties = new HashMap<>();
defaultProperties.put("user", "myuser");

SpringApplication.setDefaultProperties(defaultProperties)

JavaDoc:https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/SpringApplication.html#setDefaultProperties-java.util.Map-