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 如何在Spring AuthenticationSuccessHandler实现中注入原型bean_Java_Spring_Dependency Injection_Spring Security - Fatal编程技术网

Java 如何在Spring AuthenticationSuccessHandler实现中注入原型bean

Java 如何在Spring AuthenticationSuccessHandler实现中注入原型bean,java,spring,dependency-injection,spring-security,Java,Spring,Dependency Injection,Spring Security,我正在使用Spring security AuthenticationSuccessHandler进行登录验证 @Component public class LoginAuthenticationHandler implements AuthenticationSuccessHandler { …. 我正在LoginAuthenticationHandler中注入用户数据,如下所示 @Autowired UserData userData; 用户数据应该是原型 当我在LoginAuthe

我正在使用Spring security AuthenticationSuccessHandler进行登录验证

@Component
public class LoginAuthenticationHandler  implements AuthenticationSuccessHandler {
….
我正在LoginAuthenticationHandler中注入用户数据,如下所示

@Autowired
UserData userData;
用户数据应该是原型

当我在LoginAuthenticationHandler中为不同的用户[在用户登录时]打印userData的哈希代码时,哈希代码是相同的。它告诉我UserDataBean不能作为原型工作

这是spring-security.xml中的LoginAuthenticationHandler定义

<beans:bean id="authenticationSuccessHandler" class="com.org.login.handler.LoginAuthenticationHandler" scope="prototype">
</beans:bean> 

让UserData成为“真实”原型的选项是什么?显然,你们都在用
@Component
注释您的
LoginAuthenticationHandler
,使其成为一个自动发现的Springbean,并在XML配置中声明它。通过注释创建的bean将是一个单例,而从XML创建的bean将是一个原型。您还没有展示如何实际使用该bean,但系统的其余部分使用单例版本是有道理的。当然,在创建单例时,该版本只分配了一次
UserData
的单个实例


若要首先向前移动,请先声明
LoginAuthenticationHandler

,您已经定义了两次LoginAuthenticationHandler bean(注释和XML),不要这样做

当您想在单例bean中使用原型bean时,必须使用代理。更改范围注释:

@Scope(value=“prototype”,proxyMode=ScopedProxyMode.INTERFACES)

实际上它是一个原型。每次请求一个新实例时,都会得到一个新实例。但是,唯一创建的新实例是在启动时。将原型注入到单例bean中会为该单例bean创建一个特定实例。如果您将
UserData
注入到两个不同的bean中,您将得到两个实例。总之,如果您想让它像原型一样运行,请不要自动连线进行查找,或者使用请求范围的bean。这仍然会给出相同的哈希代码,但对于动态代理。
@Service
@Scope("prototype")
public class UserDataImpl implements UserData {