當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
【Spring注解系列03】@Scope与@Lazy
生活随笔
收集整理的這篇文章主要介紹了
【Spring注解系列03】@Scope与@Lazy
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.@Scope與@Lazy
@Scope
對象實例作用域,默認是單實例的。
取值有四個: /*** @see ConfigurableBeanFactory#ConfigurableBeanFactory#SCOPE_PROTOTYPE * @see ConfigurableBeanFactory#SCOPE_SINGLETON * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST request* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION sesssion** @Scope:調整作用域* prototype:多實例的:ioc容器啟動并不會去調用方法創建對象放在容器中。* 每次獲取的時候才會調用方法創建對象;* singleton:單實例的(默認值):ioc容器啟動會調用方法創建對象放到ioc容器中。* 以后每次獲取就是直接從容器(map.get())中拿,* request:同一次請求創建一個實例* session:同一個session創建一個實例*/@Lazy
懶加載只針對單實例bean,對于單實例bean默認在容器啟動的時候創建對象;
標注@Lazy后,容器啟動不創建對象。第一次使用(獲取)Bean創建對象,并初始化;
?
lazy和scope等價于
<beans><bean id="person" class="com.java.model.Person" scope="singleton" lazy-init="false"><property name="id" value="11111"></property><property name="name" value="vhsj"></property></bean></beans>?
2.實例
配置類:
@Configuration public class BeanScopeLazyConfig {//單例注入@Scope@Beanpublic Person single(){return new Person("id111","初始化111");}//多實例@Scope("prototype")@Beanpublic Person many(){return new Person("id222","初始化222");}//----------------------測試@Lazy在單例注入中的作用@Scope@Beanpublic Person noLazy(){System.out.println("noLazy--->配置類中初始化.....");return new Person("id3","初始化3");}@Lazy@Scope@Beanpublic Person lazy(){System.out.println("lazy--->配置類中初始化.....");return new Person("id4","初始化4");} }測試類:
public class BeanScopeLazyConfigTest {public static void main(String[] args) {ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanScopeLazyConfig.class);System.out.println("env----->容器初始化完畢.......");System.out.println();System.out.println("----------------------------->驗證單例和多實例----------------------");Person single = (Person) applicationContext.getBean("single");Person single2 = (Person) applicationContext.getBean("single");System.out.println("single---->" + single);System.out.println("single2---->" + single2);System.out.println("single==single2---->" + (single == single2));Person many = (Person) applicationContext.getBean("many");Person many2 = (Person) applicationContext.getBean("many");System.out.println("many---->" + many);System.out.println("many2---->" + many2);System.out.println("many==many2---->" + (many == many2));System.out.println("----------------------------->驗證單例和多實例----------------------");System.out.println();System.out.println("----------------------------->驗證單例懶加載@Lazy----------------------");Person noLazy = (Person) applicationContext.getBean("noLazy");Person lazy = (Person) applicationContext.getBean("lazy");} }測試結果:
?我們可以看到,在容器初始化完成前就加載了非懶加載的實例noLazy,而標有@Lazy的類,在獲取時才加載。
對于單例和多實例,可以看到單例對象是相等的,而多實例中是不相等的
noLazy--->配置類中初始化..... env----->容器初始化完畢.......----------------------------->驗證單例和多實例---------------------- single---->Person{id='id111', name='初始化111'} single2---->Person{id='id111', name='初始化111'} single==single2---->true many---->Person{id='id222', name='初始化222'} many2---->Person{id='id222', name='初始化222'} many==many2---->false ----------------------------->驗證單例和多實例--------------------------------------------------->驗證單例懶加載@Lazy---------------------- lazy--->配置類中初始化.....?
總結
以上是生活随笔為你收集整理的【Spring注解系列03】@Scope与@Lazy的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Spring注解系列02】@Compe
- 下一篇: 【Spring注解系列04】@Condi