自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring系列:聊聊 @Scope 注解用法,你會了嗎?

開發(fā) 架構(gòu)
在Spring IoC容器中主要有以下五種作用域:基本作用域:singleton(單例)、prototype(多例);Web 作用域(reqeust、session、globalsession),自定義作用域。

今天給大家分享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ù)實際情況選擇哪一種方式。

責(zé)任編輯:武曉燕 來源: IT技術(shù)分享社區(qū)
相關(guān)推薦

2022-04-13 09:01:45

SASSCSS處理器

2022-07-11 09:00:37

依賴配置文件Mybati

2024-03-04 07:41:18

SpringAOPOOP?

2022-05-06 08:26:32

JavaSPI機制

2023-01-29 08:08:34

并發(fā)庫conc通用庫

2023-07-10 08:36:21

工具pptword

2022-07-13 08:16:49

RocketMQRPC日志

2024-06-12 08:36:25

2022-12-26 07:48:04

敏捷項目

2024-03-05 10:09:16

restfulHTTPAPI

2024-01-18 09:38:00

Java注解JDK5

2023-03-07 07:50:15

Transactio事務(wù)代碼

2024-11-08 08:56:01

2022-09-26 08:49:11

Java架構(gòu)CPU

2022-12-08 10:49:43

2024-08-19 10:24:14

2022-12-06 08:37:43

2022-12-22 08:14:54

2023-06-05 08:36:04

SQL函數(shù)RANK()

2024-10-29 08:08:44

點贊
收藏

51CTO技術(shù)棧公眾號