【问题标题】:Injecting static properties in ServletContextListener using Spring使用 Spring 在 ServletContextListener 中注入静态属性
【发布时间】:2012-11-30 09:27:45
【问题描述】:

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

首先我检查了这个 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;
}

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>

在modelContext文件中

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

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

【问题讨论】:

  • 请帮帮我

标签: spring dependency-injection static-members servlet-listeners


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 2020-11-30
    相关资源
    最近更新 更多