问题
How can I set up a bean which will created once per request.
I Tried to do like this :
@Component
@Scope(value = "request")
public class TestBean {
@PostConstruct
public void init() {
System.out.println("start request");
}
@PreDestroy
public void onDestroy() {
System.out.println("ends request");
}
}
Thanks.
回答1:
Try this
@Scope(value="request", proxyMode= ScopedProxyMode.TARGET_CLASS)
For more details see this blog post.
回答2:
You can set your bean to request scope by xml configuration as
<bean id="testBean" class="com.test.TestBean" scope="request">
<aop:scoped-proxy/>
</bean>
Tag aop:scoped-proxy will be used to inject your bean using proxy. This is xml based way to set your bean to request scope.
来源:https://stackoverflow.com/questions/14731092/spring-request-scope-bean