1

I have a resource file created in my project. I want to inject values from resource file into spring bean. i defined the place holder for resource file in the applicacationContext.xml.

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

I can inject values to beans which is declared in the applicationContext.xml like :

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

This works well. However, i can not inject values if i declare beans with spring annotations.

@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.
}

Do you have any idea why i can not access values inside the beans declared using annotations.

Here is my exception

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)
erencan
  • 3,725
  • 5
  • 32
  • 50

2 Answers2

0

The @Value is processed by the java compiler whereas the the XML is parsed by Spring Bean Processor - these are two very different things... Why do you assume that should work in the same manner?

Edit: I read up on it and it seems to actually be possible using the Spring EL, you only have to prefix with # instead of $:

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

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • i know the difference. however, i have already declared my resource file in application context. do you know any way to inject it via @Value annotation ? – erencan Oct 16 '12 at 08:34
  • See my edit and cf. Spring Expression Language: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/new-in-3.html#new-feature-el – Anders R. Bystrup Oct 16 '12 at 09:05
  • unfortunately. ı get "field or property not found" error. Spring tries to inject some a bean in the context. however, application.url.logout is not a spring bean. – erencan Oct 16 '12 at 10:00
  • Have you read the link - you need to provide the properties on the form `` - the `id="application"` prefix shall match your `@Value` annotation. – Anders R. Bystrup Oct 16 '12 at 10:17
  • To elaborate, the `your-application.properties` should probably have a line like `url.logout=/.../logout.do` – Anders R. Bystrup Oct 16 '12 at 10:20
  • exactly, i have an entry named url.logout=.... I use context:property-placeholder tag to read properties file instead util:property. i replaced property:placeholder with util:property. the same still doen not work. – erencan Oct 16 '12 at 10:34
  • It would help if you copied the entire stack trace for us to see, also could you try changing `url.logout` to `url_logout` both in the property file and the annotation? – Anders R. Bystrup Oct 16 '12 at 10:40
  • OK, I'm almost out of ideas here... The error suggests that it is the `application` that isn't found, but if you use ` – Anders R. Bystrup Oct 16 '12 at 11:13
  • Sorry for that. i updated my codes accordingly. i still have error. ı Think the reason of the error is the filter itself since i declare it in web.xml. the servlet container loaded before spring container which means that there is no property file in the environment. just an idea. if so. ı really do not know if i can solve the problem. – erencan Oct 16 '12 at 11:22
  • That sounds very plausible - have a look at this article then: http://www.baeldung.com/2011/10/20/bootstraping-a-web-application-with-spring-3-1-and-java-based-configuration-part-1/ – Anders R. Bystrup Oct 16 '12 at 11:49
0

Are you sure that the instance of SecurityFilter that actually filters requests is managed by Spring?

By default Filters declared in web.xml are instantiated by servlet container, therefore they are not managed by Spring and Spring annotations such as @Value won't work in them.

However, Spring provides special support for your use case - you can delegate filtering to a component managed by Spring using DelegatingFilterProxy. Declare it in web.xml as follows:

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

<filter-mapping>...</filter-mapping>

DelegatingFilterProxy will delegate request filtering to a bean named SecurityFilter (as in your @Component).

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • Yes, i have a delegating filter entry as you suggest in my web.xml. so my security filter is managed by Spring. – erencan Oct 16 '12 at 09:52