新聞中心
1.@Scope 定義以及作用
@Scope注解主要作用是調(diào)節(jié)Ioc容器中的作用域,在Spring IoC容器中主要有以下五種作用域:基本作用域:singleton(單例)、prototype(多例);Web 作用域(reqeust、session、globalsession),自定義作用域。

10年的沈河網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營(yíng)銷網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整沈河建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“沈河網(wǎng)站設(shè)計(jì)”,“沈河網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
2.@Scope 作用域類型
2.1 @Scope("singleton")
單實(shí)例屬于默認(rèn)作用域,IOC容器啟動(dòng)的時(shí)候就會(huì)調(diào)用方法創(chuàng)建對(duì)象,以后每次獲取都是從Spring容器當(dāng)中拿同一個(gè)對(duì)象(map當(dāng)中)。
2.2 @Scope("prototype")
多實(shí)例,在IOC容器啟動(dòng)創(chuàng)建的時(shí)候,并不會(huì)直接創(chuàng)建對(duì)象放在容器中去,當(dāng)你需要調(diào)用的時(shí)候,才會(huì)從容器當(dāng)中獲取該對(duì)象然后進(jìn)行創(chuàng)建。
2.3 @Scope("request")
同一個(gè)請(qǐng)求創(chuàng)建一個(gè)實(shí)例
2.4 @Scope("session")
同一個(gè)session創(chuàng)建一個(gè)實(shí)例
2.5 @Scope("globalsession")
同一個(gè)globalsession創(chuàng)建一個(gè)實(shí)例
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對(duì)象......");
return new Person("小孫", 28, "西安");
}
}
3.3 新建測(cè)試類 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("是同一個(gè)對(duì)象");
} else {
System.out.println("不是同一個(gè)對(duì)象");
}
}
}
4.輸出效果
4.1 @Scope("prototype")
輸出結(jié)果:
容器添加Person對(duì)象...... Person{name='小孫', age='28', address='西安'} Person{name='小孫', age='28', address='西安'} 是同一個(gè)對(duì)象
4.2 @Scope("prototype")
輸出結(jié)果:
容器添加Person對(duì)象...... 容器添加Person對(duì)象...... Person{name='小孫', age='28', address='西安'} Person{name='小孫', age='28', address='西安'} 不是同一個(gè)對(duì)象
5.@Scope注解的使用場(chǎng)景
目前有90%以上的業(yè)務(wù)系統(tǒng)都使用singleton單實(shí)例,因此spring也默認(rèn)的類型也是singleton,singleton雖然保證了全局是一個(gè)實(shí)例,對(duì)性能有所提高,但是如果實(shí)例中有非靜態(tài)變量時(shí),可能會(huì)導(dǎo)致線程安全、共享資源的競(jìng)爭(zhēng)等問題。當(dāng)設(shè)置為prototype多實(shí)例時(shí):每次連接請(qǐng)求,都會(huì)重新生成一個(gè)新的bean實(shí)例,這也會(huì)導(dǎo)致一個(gè)問題,當(dāng)請(qǐng)求數(shù)越多,性能會(huì)降低,因?yàn)轭l繁創(chuàng)建的新的實(shí)例,會(huì)導(dǎo)致GC頻繁,GC回收時(shí)長(zhǎng)增加。要根據(jù)實(shí)際情況選擇哪一種方式。
網(wǎng)站標(biāo)題:Spring系列:聊聊@Scope注解用法,你會(huì)了嗎?
當(dāng)前鏈接:http://www.5511xx.com/article/ccegisd.html


咨詢
建站咨詢
