Spring request scope bean

社会主义新天地 提交于 2019-11-27 03:14:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!