我的项目中创建了一个资源文件。我想从资源文件中注入值到spring bean中。我在applicacationContext.xml中定义了资源文件的占位符。弹簧注入在资源文件中找不到变量
<context:property-placeholder location="file:///${MRAPOR_PROPPATH}mRapor.properties" />
我可以注入值,这是宣布在applicationContext.xml如豆类:
<bean
id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean" >
<property
name="jndiName"
value="${database.jndiName}" />
<property
name="lookupOnStartup"
value="false" />
<property
name="cache"
value="true" />
<property
name="proxyInterface"
value="javax.sql.DataSource" />
</bean>
这种运作良好。但是,如果我用spring注释声明bean,我不能注入值。
@Component("SecurityFilter")
public class SecurityFilter implements Filter {
public static final String USER = "USER_SESSION_KEY";
public static final String CENTRIFY_USERNAME_KEY = "REMOTE_USERNAME";
@Value("${url.logout}")//I get error here !!!!
private String logoutUrl;
//proper setters and getters.
}
你有什么想法,为什么我不能访问使用注释声明的bean内部的值。
这里是我的例外
weblogic.application.ModuleException:
at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1510)
at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:482)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:425)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:119)
Truncated. see log file for complete stacktrace
Caused By: java.lang.IllegalArgumentException: Could not resolve placeholder 'url.logout' in string value [${url.logout}]
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:255)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:748)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:745)
我知道区别。不过,我已经在应用程序上下文中声明了我的资源文件。你知道任何方式通过@Value注释注入它吗? – erencan
看我的编辑和参考。 Spring表达语言:http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/new-in-3.html#new-feature-el –
不幸的是。 ı得到“找不到字段或属性”错误。 Spring试图在上下文中注入一些bean。然而,application.url.logout不是一个spring bean。 – erencan