gpt4 book ai didi

java - @Value 注解和数据库 PropertyPlaceHolderConfigurer

转载 作者:行者123 更新时间:2023-12-01 09:16:55 25 4
gpt4 key购买 nike

我的 Spring XML 中有两个 PropertyPlaceHolderConfigurer。第一个从文件中获取应用程序属性。第二个从数据库获取用户属性,如下所示:

<myConfiguration>
<bean id="databaseProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="properties">

<bean class="org.apache.commons.configuration.ConfigurationConverter"
factory-method="getProperties">
<constructor-arg>
<ref bean="propertiesSource" />
</constructor-arg>
</bean>
</property>
</bean>

<bean id="propertiesSource" class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource" ref="tomcatDataSource" />
<constructor-arg value="application_properties" />
<constructor-arg value="PROPERTY_KEY" />
<constructor-arg value="PROPERTY_VALUE" />
</bean>

<bean id="propertiesService" class="com.xxx.PropertiesServiceImpl">
<property name="propertiesSource" ref="propertiesSource"></property>
</bean>
<myConfiguration>

它工作正常,我可以通过注入(inject)“propertiesService”来访问这些属性,例如:

@Autowired
private PropertiesService propertiesService;

这是:

public class PropertiesServiceImpl implements PropertiesService {

@Autowired
private DatabaseConfiguration propertiesSource;


private Properties properties;

@Override
public String getProperty(String key) {
if (properties == null) {
properties = ConfigurationConverter.getProperties(propertiesSource);
}
return properties.getProperty(key);
}


@Override
public Properties getProperties() {
if (properties == null) {
properties = ConfigurationConverter.getProperties(propertiesSource);
}
return properties;
}

问题是我喜欢使用@Value注释,但它不起作用。

我已经尝试过:

private @Value("#{propertiesService.helloWorld}") String helloWorld;

当然,该属性存在并且可以通过“propertiesService”访问,但它会导致下一个错误:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 18): Property or field 'helloWorld' cannot be found on object of type 'com.infraportal.model.properties.PropertiesServiceImpl' - maybe not public?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:46)
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:374)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161)

这让我认为 Spring 正在寻找“getHelloWorld()”方法,而不是使用“getProperty(String key)”

有什么建议吗?

最佳答案

您需要在 EL 中引用的 bean 上显式指定要调用哪个方法,并提供如下参数值

@Value("#{propertiesService.getProperty('helloWorld')}") 
private String helloWorld;

关于java - @Value 注解和数据库 PropertyPlaceHolderConfigurer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40483861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com