【问题标题】:Struts2 Dependency InjectionStruts2 依赖注入
【发布时间】:2013-01-17 14:05:45
【问题描述】:

我目前正在使用带有 Struts2-spring 插件的 Struts2 创建 Web 应用程序。

这是我的 applicationContext.xml 的 sn-p

<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 类

@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
}

正如您在我们的映射中看到的,我们将authenticateLoginService 注入到AccountAuthenticationManager 类中。我们甚至为authenticateLoginService 提供了setter 和getter,但是当我们运行
authenticate(Authentication auth) 方法时,您可以看到authenticationLoginService 返回null,我们不知道为什么会这样。请注意,AccountAuthenticationManager 不是 Struts Action

我们目前正在使用 struts2-spring 插件和 spring security。

【问题讨论】:

  • 我认为我们需要使用
  • 如何获取AccountAuthenticationManager的引用?您是否通过 Spring Injection 获得 AccountAuthenticationManager 的实例?或者你只是使用“新”键来创建一个?我在你的配置文件中看不到任何问题,如果它未能查找/注入依赖项,spring 会在启动时警告你。由于您没有提到 Spring 抛出的任何异常,我想知道您如何获取 AccountAuthenticationManager 的实例。
  • @spiritwalker 我让 spring AccountAuthenticationManager 通过 spring 注入本身创建。我用这个弹簧安全。但是
  • 您是否面临仅使用 authenticateLoginService 或其他对象的问题
  • 仅使用 authenticateLoginService

标签: spring spring-security dependency-injection struts2 struts2-spring-plugin


【解决方案1】:

StackOverflow 不喜欢有很长的 cmets 列表,所以我会在这里继续。 好的,所以您的系统中有两个不同的 AccountAuthenticationManager 实例。假设 Spring 在启动时创建的名为instanceA,未知的名为instanceB。如果instanceB不是由Spring容器创建的,那么instanceB就无法解决它的依赖(AuthenticateLoginService)。 如果您可以调试系统,您可能需要查看线程转储并找出instanceB 的创建时间和位置以及由谁创建?

【讨论】:

  • 如何查看线程转储?谁创造了谁?使用日食
  • 如果您可以调试到您的系统,当它在断点处停止时,您会在调试控制台中看到线程跟踪。你需要弄清楚谁(哪个类)创建了实例B
猜你喜欢
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
  • 2014-06-12
  • 2013-04-30
  • 2021-11-19
相关资源
最近更新 更多