user962206
user962206

Reputation: 16147

Struts2 Dependency Injection

I am currently creating web application using Struts2 with Struts2-spring plugin.

here is a snippet of my applicationContext.xml

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

Here is my AccountAuthenticationManager class

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

As You can see on our mapping we are injecting the authenticateLoginService inside the AccountAuthenticationManager class. we've even provided setters and getters for authenticateLoginService but as you can see when we run the
authenticate(Authentication auth) method the authenticationLoginService is returning null we have no idea why this is happening. please note that AccountAuthenticationManager is not a Struts Action

we are currently using struts2-spring plugin and spring security.

Upvotes: 1

Views: 773

Answers (1)

spiritwalker
spiritwalker

Reputation: 2257

StackOverflow doesn't like to have a long comments list, So I will continue here. Ok, so there are two different instances of AccountAuthenticationManager in your system. Let's say the one created by Spring at startup time is called instanceA and the unknown one called instanceB. If the instanceB is not created by Spring container, then there is no way that instanceB can resolve it's dependency(AuthenticateLoginService). If you can debug into the system, you might want to look into the thread dump and figure out when and where the instanceB is created and created by whom?

Upvotes: 3

Related Questions