This is solution of problem when we want to inject a bean which is in session scope inside
request scope , singleton scope, prototype
This is most common use case When session is injected inside the controller
How we can overcome this issue ???
@Controller
@Scope("request")
public class AdminReportController {
@Autowired
@Qualifier("UserSession")
private UserSession userSession;
}
/** * Enumerates the various scoped-proxy options. * * <p>For a fuller discussion of exactly what a scoped-proxy is, see that * section of the Spring reference documentation entitled 'Scoped beans as * dependencies'. * * @author Mark Fisher * @since 2.5 * @see ScopeMetadata */ public enum ScopedProxyMode { /** * Default typically equals {@link #NO}, unless a different default * has been configured at the component-scan instruction level. */ DEFAULT, /** * Do not create a scoped proxy. * <p>This proxy-mode is not typically useful when used with a * non-singleton scoped instance, which should favor the use of the * {@link #INTERFACES} or {@link #TARGET_CLASS} proxy-modes instead if it * is to be used as a dependency. */ NO, /** * Create a JDK dynamic proxy implementing <i>all</i> interfaces exposed by * the class of the target object. */ INTERFACES, /** * Create a class-based proxy (requires CGLIB). */ TARGET_CLASS }
@Component("UserSession")
@Scope(value="session",proxyMode=ScopedProxyMode.TARGET_CLASS)
public class UserSession {
private String username;
private String role;
private String email;
private String loginActive;
}
XML Based.................
Use a <aop:scoped-proxy/>
<!-- an HTTP Session-scoped bean exposed as a proxy -->
<
bean
id
=
"shoppingCart"
class
=
"com.nky.ShoppingCart"
scope
=
"session"
>
<!-- this next element effects the proxying of the surrounding bean -->
<
aop:scoped-proxy
/>
</
bean
>
Default is class based proxy
<aop:scoped-proxy proxy-target-class="true"/>
No comments:
Post a Comment