是否可以使用Spring @Value将属性文件中的值映射到HashMap.
目前我有类似的东西,映射一个值不是问题.但我需要在HashMap过期中映射自定义值.这样的事情可能吗?
@Service
@PropertySource(value = "classpath:my_service.properties")
public class SomeServiceImpl implements SomeService {
@Value("#{conf['service.cache']}")
private final boolean useCache = false;
@Value("#{conf['service.expiration.[<custom name>]']}")
private final HashMap<String, String> expirations = new HashMap<String, String>();
Run Code Online (Sandbox Code Playgroud)
属性文件:'my_service.properties'
service.cache=true
service.expiration.name1=100
service.expiration.name2=20
Run Code Online (Sandbox Code Playgroud)
像这个键映射是否可行:值集
name1 = 100
name2 = 20
如何使用@Value注释注入字符串值列表.我正在使用Spring 4.1.2.
我试过了:
@Value(value = "top, person, organizationalPerson, user")
private List<String> userObjectClasses
Run Code Online (Sandbox Code Playgroud)
然后根据内联列表的spring el文档:
@Value(value = "{top, person, organizationalPerson, user}")
private List<String> userObjectClasses
Run Code Online (Sandbox Code Playgroud)
这些尝试仅注入作为列表中唯一元素给出的值的字符串文字.
编辑
在这种情况下,我希望能够简单地对值进行硬编码,而不是从属性文件中读取.这种方法看起来有点不如:
private List<String> userObjectClasses = Arrays.asList(new String[] {"top", "person", "organizationalPerson", "user"});
Run Code Online (Sandbox Code Playgroud)
在过去,我使用spring的XML配置来连接bean,在这种情况下我可以做(假设bean有一个userObjectClasses属性的公共setter ):
<property value="userObjectClass">
<list>
<value>top</value>
<value>person</value>
<value>organizationalPerson</value>
<value>user</value>
</list>
</property>
Run Code Online (Sandbox Code Playgroud)
使用基于注释的配置时是否有类似的功能?
我通过以下方式从 Spring Boot 应用程序中的某些 .yaml 读取的地图中注入了属性:
@Value("#{${app.map}}")
private Map<String, String> indexesMap = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
但两者都没有
app:
map: {Countries: 'countries.xlsx', CurrencyRates: 'rates.xlsx'}
//note values in single quotes
nor
app:
map: {Countries: "countries.xlsx", CurrencyRates: "rates.xlsx"}
Run Code Online (Sandbox Code Playgroud)
(如https://www.baeldung.com/spring-value-annotation中所述)
也不
app:
map:
"[Countries]": countries.xslx
"[CurrencyRates]": rates.xlsx
Run Code Online (Sandbox Code Playgroud)
(如/sf/answers/3622578641/所建议)
有效 - 我不断收到消息“自动装配依赖项注入失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析占位符'
同时这也有效:
@Value("#{{Countries: 'countries.xlsx', CurrencyRates: 'rates.xlsx'}}")
private Map<String, String> indexesMap = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
但我想将属性外部化
spring yaml dependency-injection spring-annotations spring-boot
我试过了,不行,我哪里出错了?
application.properties(工作正常)
document-contact={name:'joe',email:'joe.bloggs@gmail.com'}
Run Code Online (Sandbox Code Playgroud)
application.yml(不起作用;下面的堆栈跟踪)
document-contact:
name: 'joe'
email: 'joe.bloggs@gmail.com'
Run Code Online (Sandbox Code Playgroud)
爪哇:
@Value("#{${document-contact}}")
private Map<String, String> contact;
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consolidatedSwaggerDocumentationController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'document-contact' in value "#{${document-contact}}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:403) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
Run Code Online (Sandbox Code Playgroud) 有人可以提供一些想法来注入属性文件中的所有动态键和值,并将其传递Map给DBConstants使用Setter Injection with Collection的类。
密钥事先未知,可能会有所不同。
// Example Property File that stores all db related details
// db.properties
db.username.admin=root
db.password.admin=password12
db.username.user=admin
db.password.user=password13
Run Code Online (Sandbox Code Playgroud)
DBConstants 包含映射dbConstants,需要为其注入所有键和值。
请提供bean定义以将所有键和值注入Map dbConstants。
public class DBConstants {
private Map<String,String> dbConstants;
public Map<String, String> getDbConstants() {
return dbConstants;
}
public void setDbConstants(Map<String, String> dbConstants) {
this.dbConstants = dbConstants;
}
}
Run Code Online (Sandbox Code Playgroud)