Spring Struts2依赖注入
我目前正在使用Struts2和Struts2 spring插件创建web应用程序 下面是我的applicationContext.xml的一个片段Spring Struts2依赖注入,spring,spring-security,dependency-injection,struts2,struts2-spring-plugin,Spring,Spring Security,Dependency Injection,Struts2,Struts2 Spring Plugin,我目前正在使用Struts2和Struts2 spring插件创建web应用程序 下面是我的applicationContext.xml的一个片段 <bean id="sessionFactory" scope="singleton" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocation" value="
<bean id="sessionFactory" scope="singleton"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<!-- Springs Hibernate Transaction Manager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
<!-- Create DAO Objects -->
<bean id = "userDao" class = "org.hitplay.users.dao.UserDao" scope = "singleton">
<property name ="sessionFactory" ref = "sessionFactory" />
</bean>
<bean id = "adminDao" class = "org.hitplay.admin.dao.AdminDao" scope = "singleton">
<property name ="sessionFactory" ref = "sessionFactory" />
</bean>
<bean id="authenticateLoginService" class="org.hitplay.services.AuthenticateLoginService" scope="singleton">
<property name="userDao" ref="userDao" />
<property name="adminDao" ref="adminDao" />
</bean>
<bean id="accountAuthenticationManager" class="org.hitplay.authentication.manager.AccountAuthenticationManager" scope="singleton">
<property name="authenticateLoginService" ref="authenticateLoginService" />
</bean>
正如您在映射中看到的,我们正在AccountAuthenticationManager
类中注入authenticateLoginService
。我们甚至为authenticateLoginService
提供了setter和getter,但是当我们运行authenticate(Authentication auth)
方法authenticationLoginService
返回null,我们不知道为什么会发生这种情况。请注意,AccountAuthenticationManager不是Struts操作
我们目前正在使用struts2 spring插件和spring安全性 StackOverflow不喜欢有一个很长的评论列表,所以我将继续这里的内容。 好的,那么在您的系统中有两个不同的
AccountAuthenticationManager
实例。假设Spring在启动时创建的一个名为instanceA
,未知的一个名为instanceB
。如果instanceB
不是由Spring容器创建的,则instanceB
无法解决它的依赖关系(AuthenticateLoginService
)。
如果您可以在系统中调试,您可能希望查看线程转储,并找出
实例b
是由谁创建的,在何时何地创建的?我认为我们需要使用如何获取AccountAuthenticationManager的引用?您是否通过Spring注入获得AccountAuthenticationManager实例?或者你只是用“新建”键来创建一个?我在您的配置文件中看不到任何问题,如果spring无法查找/注入依赖项,它将在启动时警告您。由于您没有提到Spring引发的任何异常,我想知道如何获得AccountAuthenticationManager实例。@我让Spring AccountAuthenticationManager通过Spring注入本身创建。我正在使用spring security处理此问题。但是,您是否只使用authenticateLoginService或其他对象(仅使用authenticateLoginService)面临问题?如何查看线程转储?谁创造了谁?使用eclipseif您可以调试到您的系统中,当它在断点处停止时,您将在调试控制台中看到线程跟踪。您需要找出是谁(哪个类)创建了instanceB
@Transactional
public class AccountAuthenticationManager implements AuthenticationManager {
protected static Logger logger = Logger.getLogger("service");
// Our custom DAO layer
private AuthenticateLoginService authenticateLoginService;
public AuthenticateLoginService getAuthenticateLoginService() {
return authenticateLoginService;
}
public void setAuthenticateLoginService(
AuthenticateLoginService authenticateLoginService) {
this.authenticateLoginService = authenticateLoginService;
}
public Authentication authenticate(Authentication auth) throws AuthenticationException {
System.out.println(authenticateLoginService);
//Some more codes here
}