Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring AuthenticationSuccessHandler自动连线服务/DAO为空_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Spring AuthenticationSuccessHandler自动连线服务/DAO为空

Spring AuthenticationSuccessHandler自动连线服务/DAO为空,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我有一个AuthenticationSuccessHandler,它应该包含一个UserService。我的问题是,我在这一行的userService上得到一个NullPointerException: logger.debug(userService.getAllUsers().toString()); AuthenticationSuccessHandlerImpl: @Repository public class AuthenticationSuccessHandlerImpl imp

我有一个AuthenticationSuccessHandler,它应该包含一个UserService。我的问题是,我在这一行的userService上得到一个NullPointerException:

logger.debug(userService.getAllUsers().toString());
AuthenticationSuccessHandlerImpl:

@Repository
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
    private static final Logger logger = LoggerFactory.getLogger(AuthenticationSuccessHandlerImpl.class);

    @Autowired
    private UserService userService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {         
        logger.debug(userService.getAllUsers().toString());

        response.setStatus(HttpServletResponse.SC_OK);  
        response.sendRedirect("index"); 
    }
}
我也尝试过@Service和@Component注释,但都出现了相同的错误

它在我的项目中的所有其他服务和控制器中工作,但不在此处理程序中工作

我的配置中还有这行代码:

<context:component-scan base-package="com.net4you.*" />

<tx:annotation-driven />

编辑: AuthenticationSuccessHandlerImpl不是使用new创建的,而是使用:

   <security:form-login login-page="/login" default-target-url="/index" authentication-failure-url="/fail2login" 
authentication-success-handler-ref="customAuthenticationSuccessHandler"/>

<beans:bean id="customAuthenticationSuccessHandler" class="com.net4you.slamanagement.helper.AuthenticationSuccessHandlerImpl"/>

项目结构:

调试器: 1)你不需要

<context:component-scan base-package="com.net4you.*" />
2) 第二种方法是在AuthenticationSuccessHandlerImpl中创建userservice的setter,手动在xml中创建userservice bean,然后使用xml中AuthenticationSuccessHandlerImpl的setter属性直接注入它,这个解决方案更复杂,我建议您使用第一种解决方案

您的实现不起作用,因为您在xml中定义了一个bean,但没有为它设置userservice(如果您手动定义bean,@autowired不起作用),然后您将此bean注入到表单login中(您不需要)。*

<context:component-scan base-package="com.net4you.*" />
2) 第二种方法是在AuthenticationSuccessHandlerImpl中创建userservice的setter,手动在xml中创建userservice bean,然后使用xml中AuthenticationSuccessHandlerImpl的setter属性直接注入它,这个解决方案更复杂,我建议您使用第一种解决方案


您的实现不起作用,因为您在xml中定义了一个bean,但没有为它设置userservice(如果您手动定义bean,@autowired则不起作用),然后将此bean注入表单login中

您可能正在使用
new
创建AuthenticationSuccessHandlerImpl对象。您可以发布安全配置。请发布您的项目/包结构以及配置。更新了所有必要信息的发布您可能正在使用
new
创建AuthenticationSuccessHandlerImpl对象。你能发布安全配置吗?发布你的项目/包结构,同时发布configs.updated post和所有必要的信息我从com.net4you.*更改为com.net4you,但仍然是相同的错误。“logger.debug(userService.toString());”这行代码还提供了一个nullpointerexception,在我的控制器和服务中为userService.getAllUsers()工作正常,不返回空值。@zFr3eak使用调试器检查userService是否正确连接->如果是,请确保在控制器中正确自动连接服务。如何调试?我正在使用Spring工具套件。更新了第一篇文章。用户服务定义为空!我从com.net4you.*更改为com.net4you,但仍然是相同的错误。“logger.debug(userService.toString());”这行代码还提供了一个nullpointerexception,在我的控制器和服务中为userService.getAllUsers()工作正常,不返回空值。@zFr3eak使用调试器检查userService是否正确连接->如果是,请确保在控制器中正确自动连接服务。如何调试?我正在使用Spring工具套件。更新了第一篇文章。用户服务定义为空!
<beans:bean id="customAuthenticationSuccessHandler" class="com.net4you.slamanagement.helper.AuthenticationSuccessHandlerImpl"/>
@Component("customAuthenticationSuccessHandler")
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
    private static final Logger logger = LoggerFactory.getLogger(AuthenticationSuccessHandlerImpl.class);

    @Autowired
    private UserService userService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {         
        logger.debug(userService.getAllUsers().toString());

        response.setStatus(HttpServletResponse.SC_OK);  
        response.sendRedirect("index"); 
    }
}