企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
1. singleton 2. prototype The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding 需要释放资源 bean post-processor,  如果单例bean需要获取prototype, 不要使用注入方式。用方法获取。 3.request 4.session 5.application 6.websocket  使用Spring Web MVC, DispatcherServlet  针对以上四种不需要特别处理。 如果使用Servlet 2.5 web container, ,不用DispatcherServlet 的话,需要注册org.springframework.web.context.request.RequestContextListener <web-app> ... <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> ... </web-app> 或者有问题, 可以使用如下方式: <web-app> ... <filter> <filter-name>requestContextFilter</filter-name> <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> </filter> <filter-mapping> <filter-name>requestContextFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ... </web-app> <bean id="loginAction" class="com.foo.LoginAction" scope="request"/> @RequestScope @Component public class LoginAction { // ... } } @SessionScope @Component public class UserPreferences { // ... } } @ApplicationScope @Component public class AppPreferences { // ... } } <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- an HTTP Session-scoped bean exposed as a proxy --> <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"> <!-- instructs the container to proxy the surrounding bean --> <aop:scoped-proxy/> </bean> <!-- a singleton-scoped bean injected with a proxy to the above bean --> <bean id="userService" class="com.foo.SimpleUserService"> <!-- a reference to the proxied userPreferences bean --> <property name="userPreferences" ref="userPreferences"/> </bean> </beans> ----- <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/> <bean id="userManager" class="com.foo.UserManager"> <property name="userPreferences" ref="userPreferences"/> </bean> 这里需要在userPreferences下面加上<aop:scoped-proxy/>, 如果不加的话,userManager是singleton的, 这样的话userPreferences也是singleton的, 是最初初始化的那个。 当将寿命较短的scoped bean注入一个寿命较长的scoped bean时的处理, 代理 以下的用法是对的: <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"> <aop:scoped-proxy/> </bean> <bean id="userManager" class="com.foo.UserManager"> <property name="userPreferences" ref="userPreferences"/> </bean> 默认使用CGLIB-based的代理, 如果要使用JDK本身的代理, 需要设置proxy-target-class="false", 类似 <!-- DefaultUserPreferences implements the UserPreferences interface --> <bean id="userPreferences" class="com.foo.DefaultUserPreferences" scope="session"> <aop:scoped-proxy proxy-target-class="false"/> </bean> <bean id="userManager" class="com.foo.UserManager"> <property name="userPreferences" ref="userPreferences"/> </bean> 1.5.5 Custom scopes-客制化的范围 实现 org.springframework.beans.factory.config.Scope接口。 void registerScope(String scopeName, Scope scope); 例子: Scope threadScope = new SimpleThreadScope(); b beanFactory.registerScope("thread", threadScope); <bean id="..." class="..." scope="thread"> 也可以使用定义的方式: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"> <property name="scopes"> <map> <entry key="thread"> <bean class="org.springframework.context.support.SimpleThreadScope"/> </entry> </map> </property> </bean> <bean id="bar" class="x.y.Bar" scope="thread"> <property name="name" value="Rick"/> <aop:scoped-proxy/> </bean> <bean id="foo" class="x.y.Foo"> <property name="bar" ref="bar"/> </bean> </beans> When you place <aop:scoped-proxy/> in a FactoryBean implementation, it is the factory bean itself that is scoped, not the object returned from getObject(). 使用<aop:scoped-proxy/>的话, 使用 getObject是得不到对象的。