0

我需要将静态属性注入我的侦听器类之一

首先我检查了这个 Spring - Injecting a dependency into a ServletContextListener。但是它不适用于静态属性。然后我与这个http://planproof-fool.blogspot.be/2010/03/spring-setting-static-fields.html混合仍然无法获得注射。

这是我的简短代码

public class MyListener implements ServletContextListener {

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

应用程序上下文.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>

并在 modelContext 文件中

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

从功能上讲,此侦听器类将永远运行(无限时间)并访问存储库属性。

4

1 回答 1

1

正如http://planproof-fool.blogspot.be/2010/03/spring-setting-static-fields.html所建议的那样

这不适合你吗?

private static Repository repository;

@Autowired(required = true)
private setStaticRepo(Repository localRepo ) {
    repository = localRepo;
}
于 2012-12-14T09:35:20.963 回答