做网站是什么样的工作,中国建筑官网测评,低价做营销企业网站,凡科免费建站怎么样bean作用域介绍
Spring框架提供了不同的作用域来管理Bean的生命周期和可见性#xff0c;这对于控制不同类型的组件和处理并发请求尤其重要。 singleton#xff08;默认#xff09;#xff1a; 每个Spring IoC容器只有一个bean实例。当容器创建bean后#xff0c;它会被缓存…bean作用域介绍
Spring框架提供了不同的作用域来管理Bean的生命周期和可见性这对于控制不同类型的组件和处理并发请求尤其重要。 singleton默认 每个Spring IoC容器只有一个bean实例。当容器创建bean后它会被缓存起来后续请求将返回同一个实例。这是默认的作用域适用于无状态的服务层bean。 prototype 每次请求都会创建一个新的bean实例。每当客户端请求该bean时Spring容器都会创建一个新的实例。这对于那些需要保持独立状态的bean特别有用比如基于注解的控制器。 request 每个HTTP请求都有其自己的bean实例。主要用于Web应用确保每个HTTP请求都有一个新的bean实例。这对于有状态的会话bean特别有用。 session 在同一个HTTP Session中每个Session拥有一个bean实例。这意味着在用户会话期间bean实例是共享的。对于需要在整个会话期间保存状态的bean这是理想的选择。 global-session 类似于session作用域但专门用于portlet应用中的全局会话。在portlet应用中全局会话是跨所有portlet的会话范围。 application 这个作用域在Web应用中提供一个bean实例类似于Servlet的application作用域。该bean在整个Web应用的生命周期内存在即从应用启动到停止。
除了这些内置作用域Spring还允许你定义自定义作用域这通常通过实现org.springframework.beans.factory.config.Scope接口来完成。自定义作用域可以让你根据特定的应用需求来管理bean的生命周期。
常用的
取值含义创建对象的时机singleton默认在IOC容器中这个bean的对象始终为单实例IOC容器初始化时prototype这个bean在IOC容器中有多个实例获取bean时
案例演示
public class User {private Integer id;private String username;private String password;private Integer age;public User() {System.out.println(生命周期1、创建对象);}public User(Integer id, String username, String password, Integer age) {this.id id;this.username username;this.password password;this.age age;}public Integer getId() {return id;}public void setId(Integer id) {System.out.println(生命周期2、依赖注入);this.id id;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public void initMethod(){System.out.println(生命周期3、初始化);}public void destroyMethod(){System.out.println(生命周期5、销毁);}Overridepublic String toString() {return User{ id id , username username \ , password password \ , age age };}}我们创建一个spring-scope.xml演示作用域
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!-- scope属性取值singleton默认值bean在IOC容器中只有一个实例IOC容器初始化时创建
对象 --!-- scope属性取值prototypebean在IOC容器中可以有多个实例getBean()时创建对象 --bean idstudentOne classcom.miaow.spring.bean.Student scopeprototypeproperty nameid value1/propertyproperty namename valuemiaow/propertyproperty nameaddress value湖南娄底/propertyproperty namebirthday value1999/propertyproperty nameemail value2958467385qq.com/propertyproperty namephone value15975145237/propertyproperty nameschool value知行合一/propertyproperty namesexvalue男/value/property/bean
/beansJava测试代码
Test
public void ScopeTest(){ApplicationContext context new ClassPathXmlApplicationContext(spring-scope.xml);Student student (Student) context.getBean(studentOne);Student student1 (Student) context.getBean(studentOne);System.out.println(student student1);
}在上述代码中我们需要注意的是xml配置文件中的singleton和prototype当我们配置singleton的时候你试一下结果如何我给出prototype的时候的结果如下图所示 注意线程安全性如果Bean是有状态的并且在多线程环境中使用需要确保Bean的线程安全性。在多线程环境中最好使用原型作用域或每次请求创建新的Bean实例。 注意内存消耗使用原型作用域时需要注意内存消耗。如果原型Bean被频繁创建可能会导致内存占用过高。在这种情况下可以考虑使用对象池或其他缓存机制来管理Bean的创建和销毁。 注意作用域的选择根据应用程序的需求选择适当的作用域。如果Bean的状态不会改变并且需要在整个应用程序中共享可以使用单例作用域。如果需要每次请求或会话创建新的实例可以使用请求或会话作用域