【发布时间】:2014-10-21 11:55:44
【问题描述】:
我正在使用 Spring security AuthenticationSuccessHandler 进行登录验证。
@Component
public class LoginAuthenticationHandler implements AuthenticationSuccessHandler {
….
我在LoginAuthenticationHandler中注入UserData,如下
@Autowired
UserData userData;
UserData 应该是原型。
当我在 LoginAuthenticationHandler 中为不同的用户 [在用户登录时] 打印 userData 的哈希码时,哈希码是相同的。它告诉我 UserData bean 不能作为原型工作。
这是 spring-security.xml 中的 LoginAuthenticationHandler 定义
<beans:bean id="authenticationSuccessHandler" class="com.org.login.handler.LoginAuthenticationHandler" scope="prototype">
</beans:bean>
这是 UserData bean 类
@Service
@Scope("prototype")
public class UserDataImpl implements UserData {
使 UserData 成为“真实”原型的选项是什么
【问题讨论】:
-
实际上它是作为原型工作的。每次请求新实例时,您都会获得一个新实例。但是,唯一创建的新实例是在启动时。将原型注入单例会为该单例 bean 创建一个特定实例。如果您将
UserData注入 2 个不同的 bean,您将获得 2 个实例。简而言之,如果您希望它的行为像原型一样,请不要自动装配,请进行查找,或者改用请求范围的 bean。它仍然提供相同的哈希码,但随后用于动态代理。
标签: java spring dependency-injection spring-security