简体   繁体   中英

defining Spring request scope bean

For using spring request scope bean is this definition correct?

<bean id="shoppingCart" class="ShoppingCart" scope="request">
<!-- This requires CGLIB --> 
<aop:scoped-proxy/>
</bean> 

I modified this from a session scope bean example, and changed only the scope definition, not sure about the proxy thing

I took this example from this link, you can see the full xml:

http://wheelersoftware.com/articles/spring-session-scoped-beans-2.html

Generally - yes, it's correct.

If for every request You'd retrieve the request scoped bean directly from the BeanFactory , then You don't need the proxy.

But You need the proxy if You're going to use the request soped bean as a depenedncy to singleton scoped bean, for example like this:

@Controller
public class MyController {

    @Autowired
    private ShoppingCart shoppingCart;
}

See this reference page for more details about scoped beans.

As a side note I'd advise to use standard JDK interface-based poxies instead of CGLIB whenever possible. More about proxying with spring can be found in documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM