Spring系列:聊聊 @Scope 注解用法,你會了嗎?
今天給大家分享Spring中@Scope注解的用法,希望對大家能有所幫助!
1.@Scope 定義以及作用
@Scope注解主要作用是調節Ioc容器中的作用域,在Spring IoC容器中主要有以下五種作用域:基本作用域:singleton(單例)、prototype(多例);Web 作用域(reqeust、session、globalsession),自定義作用域。
2.@Scope 作用域類型
2.1 @Scope("singleton")
單實例屬于默認作用域,IOC容器啟動的時候就會調用方法創建對象,以后每次獲取都是從Spring容器當中拿同一個對象(map當中)。
2.2 @Scope("prototype")
多實例,在IOC容器啟動創建的時候,并不會直接創建對象放在容器中去,當你需要調用的時候,才會從容器當中獲取該對象然后進行創建。
2.3 @Scope("request")
同一個請求創建一個實例
2.4 @Scope("session")
同一個session創建一個實例
2.5 @Scope("globalsession")
同一個globalsession創建一個實例
3.示例演示
3.1 新建Person.java
package 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.java
package 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回收時長增加。要根據實際情況選擇哪一種方式。