Spring - 将依赖项注入到 ServletContextListener 中
2022-09-01 15:25:58
我想将依赖项注入到.但是,我的方法不起作用。我可以看到Spring正在调用我的setter方法,但是稍后调用时,属性是。ServletContextListener
contextInitialized
null
这是我的设置:
The ServletContextListener:
public class MyListener implements ServletContextListener{
private String prop;
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("Initialising listener...");
System.out.println(prop);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
public void setProp(String val) {
System.out.println("set prop to " + prop);
prop = val;
}
}
web.xml:(这是文件中的最后一个侦听器)
<listener>
<listener-class>MyListener</listener-class>
</listener>
应用上下文.xml:
<bean id="listener" class="MyListener">
<property name="prop" value="HELLO" />
</bean>
输出:
set prop to HELLO
Initialising listener...
null
实现此目的的正确方法是什么?