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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.