1

Based on Spring manual,

The constructor of your proxied object will be called twice. This is a natural consequence of the CGLIB proxy model whereby a subclass is generated for each proxied object. For each proxied instance, two objects are created: the actual proxied object and an instance of the subclass that implements the advice.

I have a class which is session scope. I observed the constructor is called first time during application startup, even there is no HTTPSession. The second time is during a HTTPSession is created. How could I delay the bean instantiation with ScopedProxyMode.TARGET_CLASS only if there is a session? I have tried annotated with @Lazy but has no effect.

@Component
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class WorkspaceImpl implements Workspace, Serializable {
Lee Chee Kiam
  • 11,450
  • 10
  • 65
  • 87

1 Answers1

1

Either you take the side effect of CGLIB proxy, or change to ScopedProxyMode.INTERFACE by making the target class to implement an interface (if you own the class), which uses JDK dynamic proxy. See the difference between JDK dynamic proxy and CGLib at https://stackoverflow.com/a/10664208/418439

Community
  • 1
  • 1
Lee Chee Kiam
  • 11,450
  • 10
  • 65
  • 87