【问题标题】:Spring Injection can not find variable in resource fileSpring Injection 在资源文件中找不到变量
【发布时间】:2012-10-16 08:05:02
【问题描述】:

我在我的项目中创建了一个资源文件。我想将资源文件中的值注入spring bean。我在 applicacationContext.xml 中定义了资源文件的占位符。

<context:property-placeholder  location="file:///${MRAPOR_PROPPATH}mRapor.properties" />

我可以将值注入到 applicationContext.xml 中声明的 bean,例如:

<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)

【问题讨论】:

    标签: spring configuration resource-files


    【解决方案1】:

    您确定实际过滤请求的SecurityFilter 的实例是由Spring 管理的吗?

    默认情况下,web.xml 中声明的 Filters 由 servlet 容器实例化,因此它们不受 Spring 管理,并且 @Value 等 Spring 注释在它们中不起作用。

    不过,Spring 为您的用例提供了特殊支持 - 您可以使用 DelegatingFilterProxy 将过滤委托给 Spring 管理的组件。在web.xml中声明如下:

    <filter>
        <filter-name>SecurityFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    
    <filter-mapping>...</filter-mapping>
    

    DelegatingFilterProxy 会将请求过滤委托给名为 SecurityFilter 的 bean(就像在您的 @Component 中一样)。

    【讨论】:

    • 是的,我有一个委托过滤器条目,正如您在我的 web.xml 中所建议的那样。所以我的安全过滤器由 Spring 管理。
    【解决方案2】:

    @Value 由 java 编译器处理,而 XML 由 Spring Bean 处理器解析 - 这是两个非常不同的东西......为什么你认为应该以相同的方式工作?

    编辑:我阅读了它,它似乎实际上可以使用 Spring EL,你只需要前缀 # 而不是 $

    private @Value( "#{application.url.logout}" ) String logoutUrl;
    

    干杯,

    【讨论】:

    • 我知道区别。但是,我已经在应用程序上下文中声明了我的资源文件。你知道通过@Value 注解注入它的任何方法吗?
    • 查看我的编辑和cf。 Spring 表达式语言:static.springsource.org/spring/docs/3.0.x/…
    • 不幸的是。我得到“找不到字段或属性”错误。 Spring 尝试在上下文中注入一些 bean。但是,application.url.logout 不是 spring bean。
    • 您是否阅读了链接 - 您需要在 &lt;util:properties id="application" location="classpath:your/package/your-application.properties"/&gt; 表单上提供属性 - id="application" 前缀应与您的 @Value 注释匹配。
    • 详细来说,your-application.properties 应该有类似url.logout=/.../logout.do 这样的行
    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 2017-05-01
    • 1970-01-01
    • 2019-05-31
    • 2020-05-20
    • 1970-01-01
    • 2011-07-12
    相关资源
    最近更新 更多