0

I have 2 classes

 @Component
    @Scope(proxyMode=ScopedProxyMode.TARGET_CLASS,value="session")
    public class Child extends Base{
    }

    @Component
    @Scope(proxyMode=ScopedProxyMode.TARGET_CLASS,value="session")
    public class Base{
    private UserVO user;

    public UserVO getUser(){
    return user;
    }
    public void setUser(UserVO usr){
    this.user = usr;
    }
    }

    I call the following method
  public class SomeClass{
  @autowired
  private Child child;
    public void someMethod(){
    child.setUser(new UserVO());
    System.out.println(child.getUser());
    }
 }

The above system.out prints null.Where am I going wrong.Pls help. Is this the way cglib works?If so what is the workaround for this Thanks

Srini
  • 420
  • 1
  • 5
  • 17

1 Answers1

1

Is (very) posible that you are getting null because you don't have a HttpSession when calling the someMethod.

This is the expected behavior of an aop-scoped-proxy. Note that an aop-scoped-proxy is really a singleton that try to retrieve the taget object from the configured scope or create a new if none was found, in every method call.

So if the HttpSession don't exist the aop-scoped-proxy will redirect all calls to a new created object.

ie:

aopScopedProxy.setUser -- > new User().setUser()
aopScopedProxy.getUser ---> new User().getUser()

That seem to be your problem.

Jose Luis Martin
  • 10,459
  • 1
  • 37
  • 38
  • Sorry,am unable to understand,does it mean i cant set the user VO with out httpsession?Pls find my edit.am actually autowiring the session scoped bean in a class and then using this reference to set userVO. – Srini Feb 01 '13 at 07:27
  • Well, you need the session to use an aop scoped proxy bound to session. For example, you could check in someMethod() if ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getSessionId() throws an Exception... – Jose Luis Martin Feb 01 '13 at 13:29