Question

I need to inject static properties into one of my listener class

First I checked this Spring - Injecting a dependency into a ServletContextListener .However it's not working for static properties. Then I mixed with this http://planproof-fool.blogspot.be/2010/03/spring-setting-static-fields.html still unable to get the injection.

Here's my brief code

public class MyListener implements ServletContextListener {

    private static Logger logger = Logger.getLogger(MyListener.class);
    private static ServletContext context = null;
    @Autowired
    private static Repository repository;
}

ApplicationContext.xml

<import resource="classpath*:spring/modelContextDump.xml" />

    <!-- Scan for @Autowired annotations -->
    <context:annotation-config />

    <bean id="propertiesUtil"
        class="com.my.utils.PropertiesUtil">
        <property name="locations">
            <list>
                <value>classpath:config.properties</value>
            </list>
        </property>
    </bean>

and in modelContext file

<bean id="repository"
    class="com.my.repository.RepositoryImpl"
    parent="abstractRepository">
</bean>

Functionally, this listener class will run forever (infinte-time) and access the repository properties.

Was it helpful?

Solution

As suggested by http://planproof-fool.blogspot.be/2010/03/spring-setting-static-fields.html

Is this not working for you??

private static Repository repository;

@Autowired(required = true)
private setStaticRepo(Repository localRepo ) {
    repository = localRepo;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top