1

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
user962206
  • 15,637
  • 61
  • 177
  • 270
  • i think we need to use – PSR Feb 02 '13 at 09:01
  • how do you get the reference of AccountAuthenticationManager? do you get the instance of AccountAuthenticationManager through Spring Injection? or you just use "new" key to create one? I cannot see any issue in your configuration file, and spring will warn you at startup time if it failed to lookup/inject a dependency. Since you did not mention any exception thrown from Spring, I would like to know how do you get the instance of AccountAuthenticationManager. – spiritwalker Feb 02 '13 at 09:05
  • @spiritwalker I let spring AccountAuthenticationManager get created through spring injection itself. I using spring-security with this. but – user962206 Feb 02 '13 at 09:08
  • are u facing the problem with only authenticateLoginService or with other objects also – PSR Feb 02 '13 at 09:10
  • only with authenticateLoginService – user962206 Feb 02 '13 at 09:12
  • check by injecting other classes to AccountAuthenticationManager.Just for testing only – PSR Feb 02 '13 at 09:15
  • it is injecting other objects its even injecting the authenticateLoginService on startup but when we run the authenticate it becomes null – user962206 Feb 02 '13 at 09:21
  • how you can say it is injecting the authenticateLoginService on startup – PSR Feb 02 '13 at 09:22
  • because it is singleton scoped bean, spring will instantiate all singleton beans at startup time by default, unless you explicitly specify lazy injection. – spiritwalker Feb 02 '13 at 09:26
  • @sambapenugonda because I've added a system.out.println("MY MESSAGE HERE") on the setter of aunthenticateLoginService. and I can see that message on startup. – user962206 Feb 02 '13 at 09:29
  • I reckon you add some debugging code into your AccountAuthenticationManager, like print this.hashCode(); in just make sure it is the same instance when "it is injecting other objects" and "it returns null" – spiritwalker Feb 02 '13 at 09:30
  • @spiritwalker how do I specify lazy injection? – user962206 Feb 02 '13 at 09:30
  • @user962206, yes, like I initially said, spring will raise complain if it failed to inject a dependency. So it is really less likely that there is anything wrong in the Spring config you posted. I'm bit concern when the authenticate() is invoked, is it the same instance as the one created by Spring. – spiritwalker Feb 02 '13 at 09:32
  • @user962206 you can force Spring to lazy instantiate a singleton bean . http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-lazy-init – spiritwalker Feb 02 '13 at 09:35
  • @spiritwalker I am currently working with Security, I already tested what you have suggested, it turns out that they both different instances. they both different hashcode – user962206 Feb 02 '13 at 09:35

1 Answers1

3

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
spiritwalker
  • 2,257
  • 14
  • 9
  • how to look at the thread dump? and who created who? using eclipse – user962206 Feb 02 '13 at 09:48
  • if you can debug into your system, when it stops at breakpoint you would see the thread trace in debug console. you need to figure out who(which class) created instanceB – spiritwalker Feb 02 '13 at 10:01