相关疑难解决方法(0)

Spring - 将依赖项注入ServletContextListener

我想将一个依赖注入一个ServletContextListener.但是,我的方法不起作用.我可以看到Spring正在调用我的setter方法,但是稍后在contextInitialized调用时,属性是null.

这是我的设置:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

web.xml :(这是文件中的最后一个监听器)

<listener>
  <listener-class>MyListener</listener-class>
</listener> 
Run Code Online (Sandbox Code Playgroud)

applicationContext.xml中:

<bean id="listener" class="MyListener">
  <property name="prop" value="HELLO" />
</bean>  
Run Code Online (Sandbox Code Playgroud)

输出:

set prop to HELLO
Initialising listener...
null
Run Code Online (Sandbox Code Playgroud)

实现这一目标的正确方法是什么?

java spring tomcat servlets dependency-injection

27
推荐指数
3
解决办法
4万
查看次数

我如何从ApplicationListener方法获取会话对象

我想HttpSession在成功进行用户身份验证后添加对象.请不要建议解决方案,SavedRequestAwareAuthenticationSuccessHandler因为在这个应用程序由于某种原因应用程序正在进行原始请求.

public class AuthenticationSuccessListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
    @Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent e) {
        //adding object to HttpSession
    }
} 
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-security

4
推荐指数
1
解决办法
6603
查看次数