03-Scope注解

@Scope设置组件作用域

总结

  • singleton: ioc容器创建的时候调用bean方法创建bean对象
  • prototype: 每次bean对象被调用的时候创建
  • 使用 @Lazy 可以使得bean对象在被调用的时候才加载

验证

prototype模式

package com.xiong.config;

import com.xiong.bean.Person;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class PersonConfig {

    @Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
    @Bean
    Person person() {
        System.out.println("为ioc容器添加person对象");
        Person person = new Person();
        person.setName("person");
        person.setAge(20);
        return person;
    }
}
package com.xiong;


import com.xiong.bean.Person;
import com.xiong.config.PersonConfig;
import com.xiong.service.PersonService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
    @Test
    public void prototypeTest() {
        ApplicationContext context = new AnnotationConfigApplicationContext(PersonConfig.class);
        System.out.println("====================bean对象调用后=======================");
        Person personA = context.getBean(Person.class);
        Person personB = context.getBean(Person.class);
        System.out.println(personA == personB);
    }
}

image-20221118093945406

singleton模式

package com.xiong.config;

import com.xiong.bean.Person;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class PersonConfig {

    @Scope(value = BeanDefinition.SCOPE_SINGLETON)
    @Bean
    Person person() {
        System.out.println("为ioc容器添加person对象");
        Person person = new Person();
        person.setName("person");
        person.setAge(20);
        return person;
    }
}
package com.xiong;


import com.xiong.bean.Person;
import com.xiong.config.PersonConfig;
import com.xiong.service.PersonService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
    @Test
    public void singletonTest(){
        ApplicationContext context = new AnnotationConfigApplicationContext(PersonConfig.class);
        System.out.println("====================bean对象调用前后=======================");
        Person personA = context.getBean(Person.class);
        Person personB = context.getBean(Person.class);
        System.out.println(personA == personB);
    }
}

image-20221118094019002

@Lazy注解

package com.xiong.config;

import com.xiong.bean.Person;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class PersonConfig {

    @Lazy
    @Scope(value = BeanDefinition.SCOPE_SINGLETON)
    @Bean
    Person person() {
        System.out.println("为ioc容器添加person对象");
        Person person = new Person();
        person.setName("person");
        person.setAge(20);
        return person;
    }
}
package com.xiong;


import com.xiong.bean.Person;
import com.xiong.config.PersonConfig;
import com.xiong.service.PersonService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
    @Test
    public void singletonTest(){
        ApplicationContext context = new AnnotationConfigApplicationContext(PersonConfig.class);
        System.out.println("====================bean对象调用前后=======================");
        Person personA = context.getBean(Person.class);
        Person personB = context.getBean(Person.class);
        System.out.println(personA == personB);
    }
}

image-20221118094435144


   转载规则


《03-Scope注解》 熊水斌 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
02-ComponentScan注解 02-ComponentScan注解
@ComponentScan包扫描配置文件方式 在配置文件中配置包扫描 测试 通过包扫描自动注入@Configuration配置类组件 在@Configuration组件中创建bean对象 通过配置文件启动spring程序, 从容器中获取@
2023-05-13
下一篇 
04-Conditional注解 04-Conditional注解
@Conditional条件注入不使用条件注入前package com.xiong.config; import com.xiong.bean.Person; import org.springframework.beans.factor
2023-05-13
  目录