当前位置:首页 > 应用开发

Spring系列:聊聊 @Scope 注解用法,你会了吗?

今天给大家分享Spring中@Scope注解的系列用法,希望对大家能有所帮助!

1.@Scope 定义以及作用

@Scope注解主要作用是聊聊调节Ioc容器中的作用域,在Spring IoC容器中主要有以下五种作用域:基本作用域:singleton(单例)、解用prototype(多例);Web 作用域(reqeust、系列session、聊聊globalsession),解用自定义作用域。系列

Spring系列:聊聊 @Scope 注解用法,你会了吗?

2.@Scope 作用域类型

2.1 @Scope("singleton")

单实例属于默认作用域,聊聊IOC容器启动的解用时候就会调用方法创建对象,以后每次获取都是系列从Spring容器当中拿同一个对象(map当中)。

Spring系列:聊聊 @Scope 注解用法,你会了吗?

2.2 @Scope("prototype")

多实例,聊聊在IOC容器启动创建的解用时候,香港云服务器并不会直接创建对象放在容器中去,系列当你需要调用的聊聊时候,才会从容器当中获取该对象然后进行创建。解用

Spring系列:聊聊 @Scope 注解用法,你会了吗?

2.3 @Scope("request")

同一个请求创建一个实例

2.4 @Scope("session")

同一个session创建一个实例

2.5 @Scope("globalsession")

同一个globalsession创建一个实例

3.示例演示

3.1 新建Person.javapackage com.spring.bean;

public class Person {

private String name;

private Integer age;

private String address;

public Person(String name, Integer age, String address) {

this.name = name;

this.age = age;

this.address = address;

}

public Person() {

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

@Override

public String toString() {

return "Person{ " +

"name=" + name + \ +

", age=" + age + \ +

", address=" + address + \ +

};

}

}

3.2 新建配置类 TestScopeConfig.java

package com.spring.config;

import com.spring.bean.Person;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Scope;

@Configuration

public class TestScopeConfig {

@Bean

@Scope("singleton")

//@Scope("prototype")

public Person person() {

System.out.println("容器添加Person对象......");

return new Person("小孙", 28, "西安");

}

}3.3 新建测试类 TestScope.javapackage com.spring.test;

import com.spring.bean.Person;

import com.spring.config.TestBeanConfig;

import com.spring.config.TestScopeConfig;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestScope {

public static void main(String[] args) {

//配置文件方式

AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(TestScopeConfig.class);

Object person1 = annotationContext.getBean("person");

Object person2 = annotationContext.getBean("person");

System.out.println(person1);

System.out.println(person2);

boolean flag = person1 == person2;

if (flag) {

System.out.println("是同一个对象");

} else {

System.out.println("不是同一个对象");

}

}

}

4.输出效果

4.1 @Scope("prototype")

输出结果:

容器添加Person对象...... Person{ name=小孙, age=28, address=西安} Person{ name=小孙, age=28, address=西安} 是同一个对象

4.2 @Scope("prototype")

输出结果:

容器添加Person对象...... 容器添加Person对象...... Person{ name=小孙, age=28, address=西安} Person{ name=小孙, age=28, address=西安} 不是同一个对象

5.@Scope注解的使用场景

目前有90%以上的业务系统都使用singleton单实例,因此spring也默认的类型也是singleton,singleton虽然保证了全局是一个实例,对性能有所提高,源码下载但是如果实例中有非静态变量时,可能会导致线程安全、共享资源的竞争等问题。当设置为prototype多实例时:每次连接请求,都会重新生成一个新的bean实例,这也会导致一个问题,当请求数越多,性能会降低,因为频繁创建的新的实例,会导致GC频繁,GC回收时长增加。要根据实际情况选择哪一种方式。网站模板

分享到:

滇ICP备2023006006号-16