Spring系列:聊聊 @Scope 注解用法,你會了嗎?
今天給大家分享Spring中@Scope注解的用法,希望對大家能有所幫助!
1.@Scope 定義以及作用
@Scope注解主要作用是調(diào)節(jié)Ioc容器中的作用域,在Spring IoC容器中主要有以下五種作用域:基本作用域:singleton(單例)、prototype(多例);Web 作用域(reqeust、session、globalsession),自定義作用域。
2.@Scope 作用域類型
2.1 @Scope("singleton")
單實例屬于默認(rèn)作用域,IOC容器啟動的時候就會調(diào)用方法創(chuàng)建對象,以后每次獲取都是從Spring容器當(dāng)中拿同一個對象(map當(dāng)中)。
2.2 @Scope("prototype")
多實例,在IOC容器啟動創(chuàng)建的時候,并不會直接創(chuàng)建對象放在容器中去,當(dāng)你需要調(diào)用的時候,才會從容器當(dāng)中獲取該對象然后進(jìn)行創(chuàng)建。
2.3 @Scope("request")
同一個請求創(chuàng)建一個實例
2.4 @Scope("session")
同一個session創(chuàng)建一個實例
2.5 @Scope("globalsession")
同一個globalsession創(chuàng)建一個實例
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")
輸出結(jié)果:
容器添加Person對象...... Person{name='小孫', age='28', address='西安'} Person{name='小孫', age='28', address='西安'} 是同一個對象
4.2 @Scope("prototype")
輸出結(jié)果:
容器添加Person對象...... 容器添加Person對象...... Person{name='小孫', age='28', address='西安'} Person{name='小孫', age='28', address='西安'} 不是同一個對象
5.@Scope注解的使用場景
目前有90%以上的業(yè)務(wù)系統(tǒng)都使用singleton單實例,因此spring也默認(rèn)的類型也是singleton,singleton雖然保證了全局是一個實例,對性能有所提高,但是如果實例中有非靜態(tài)變量時,可能會導(dǎo)致線程安全、共享資源的競爭等問題。當(dāng)設(shè)置為prototype多實例時:每次連接請求,都會重新生成一個新的bean實例,這也會導(dǎo)致一個問題,當(dāng)請求數(shù)越多,性能會降低,因為頻繁創(chuàng)建的新的實例,會導(dǎo)致GC頻繁,GC回收時長增加。要根據(jù)實際情況選擇哪一種方式。