Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序侦听器未接收事件通知_Java_Spring_Spring Mvc_Spring Security_Listener - Fatal编程技术网

Java 应用程序侦听器未接收事件通知

Java 应用程序侦听器未接收事件通知,java,spring,spring-mvc,spring-security,listener,Java,Spring,Spring Mvc,Spring Security,Listener,我正在开发一个SpringMVC应用程序,使用SpringSecurity进行验证。我想在登录时做一些工作,并在多个地方看到了使用应用程序侦听器实现这一点的建议(包括)。我已经着手实施了它: @Named public class AccountLoginListener implements ApplicationListener<AuthenticationSuccessEvent> { @Inject AccountService accountService

我正在开发一个SpringMVC应用程序,使用SpringSecurity进行验证。我想在登录时做一些工作,并在多个地方看到了使用应用程序侦听器实现这一点的建议(包括)。我已经着手实施了它:

@Named
public class AccountLoginListener implements ApplicationListener<AuthenticationSuccessEvent> {

    @Inject
    AccountService accountService;

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        Account account = (Account) event.getAuthentication().getDetails();
        ...
        accountService.saveAccount(account);
    }

}
@Named
公共类AccountLoginListener实现ApplicationListener{
@注入
会计服务;
@凌驾
ApplicationEvent上的公共无效(AuthenticationSuccessEvent事件){
帐户=(帐户)事件。getAuthentication().getDetails();
...
accountService.saveAccount(account);
}
}
不幸的是,AuthenticationSuccessEvent似乎没有被捕获,当我调试时,onApplicationEvent函数从未被调用。我没有在xml文件中进行任何额外的配置,但我认为没有必要这样做。我是否缺少一些配置,或者我是否做错了其他事情

谢谢


idbentley

如果您使用的是Spring Security 3,则应该实现,然后在应用程序上下文中配置此bean。

如果您使用的是Spring Security 3,则应该实现,然后在应用程序上下文中配置此bean。

公共类AuthenticationSuccessListener
public class AuthenticationSuccessListener
    implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {

    @Inject
    AccountService accountService;

    @Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
        Account account = (Account) event.getAuthentication().getDetails();
        accountService.saveAccount(account);
    }
}
实现ApplicationListener{ @注入 会计服务; @凌驾 ApplicationEvent上的公共无效(InteractiveAuthenticationSuccessEvent事件){ 帐户=(帐户)事件。getAuthentication().getDetails(); accountService.saveAccount(account); } }
公共类身份验证SuccessListener
实现ApplicationListener{
@注入
会计服务;
@凌驾
ApplicationEvent上的公共无效(InteractiveAuthenticationSuccessEvent事件){
帐户=(帐户)事件。getAuthentication().getDetails();
accountService.saveAccount(account);
}
}

您是如何创建实例化这个bean/listener的?我的spring上下文有一个
,这个类在它的基本包中。名为
@的
注释是什么类型的?它是一个jsr330注释,在功能上是等价的(我相信)到
@Component
如何创建实例化这个bean/侦听器?我的spring上下文有一个
,这个类在它的基本包中。名为
@的
注释是什么类型的?它是一个jsr330注释,在功能上是等效的(我相信)对于
@Component
,感谢您的帮助,David-我来看看有什么不同。我的问题最终是因为我们应用程序中的web上下文与spring上下文是不同的上下文。谢谢David,我来看看它们的区别。我的问题最终是因为我们应用程序中的web上下文与spring上下文是不同的上下文。