Spring 设置Bean的作用域及懒加载

一、准备工作

1、导入spring-context依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.22.RELEASE</version>
</dependency>

2、创建实体类Person

public class Person {

    private int age;
    private String name;

    public Person() {
        System.out.println("创建Person完成");
    }

    public Person(int age, String name) {
        this();
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
    
}

二、scope参数

1、通过xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--  scope:singleton单例模式,在spring初始化时创建,只会存在一个实例,默认值  -->
    <!--  scope:prototype多例模式,每次获取bean时创建,存在多个实例  -->
    <bean id="person" class="Person" scope="singleton">
        <property name="age" value="20"/>
        <property name="name" value="zhangsan"/>
    </bean>

</beans>
    public static void main(String[] args) {
        testXml();
    }

    public static void testXml(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person p1 = (Person) context.getBean("person");
        Person p2 = (Person) context.getBean("person");
        System.out.println(p1==p2);
        // scope:singleton,单例模式下,结果为true
        // scope:prototype,多例模式下,结果为false
    }

2、通过注解配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class BeansConfig {

    /*
    *  @Scope("singleton"),单例模式,默认值
    *  @Scope("prototype"),多例模式
    * */
    @Scope("singleton")
    @Bean
    public Person person(){
        return new Person(20,"zhangsan");
    }

}
public static void main(String[] args) {
    testAnnotation();
}

public static void testAnnotation(){
    ApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
    Person p1 = (Person) context.getBean("person");
    Person p2 = (Person) context.getBean("person");
    System.out.println(p1==p2);
    // @Scope("singleton"),单例模式下,结果为true
    // @Scope("prototype"),多例模式下,结果为false
}

三、scope:singleton模式下设置懒加载

1、通过xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- lazy-init="true" 表示开启懒加载 -->
    <bean id="person" class="Person" lazy-init="true" >
        <property name="age" value="20"/>
        <property name="name" value="zhangsan"/>
    </bean>

</beans>
    public static void main(String[] args) {
        testXml();
    }

    public static void testXml(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        System.out.println("创建Spring完成");
        Person person = (Person) context.getBean("person");
        /**
         * 未开启懒加载,打印顺序:
         * 创建Person完成
         * 创建Spring完成
         *
         * 开启懒加载,打印顺序:
         * 创建Spring完成
         * 创建Person完成
         */
    }

2、通过注解配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

@Configuration
public class BeansConfig {

    @Lazy
    @Bean
    public Person person(){
        return new Person(20,"zhangsan");
    }

}
    public static void main(String[] args) {
        testAnnotation();
    }

    public static void testAnnotation(){
                ApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
        System.out.println("创建Spring完成");
        Person person = (Person) context.getBean("person");
        /**
         * 未开启懒加载,打印顺序:
         * 创建Person对象
         * 创建Spring完成
         *
         * 开启懒加载,打印顺序:
         * 创建Spring完成
         * 创建Person对象
         */
    }

四、总结分析

本章主要学习了,如何设置Spring Bean作用域及懒加载。《参考资料》

猜你喜欢

转载自blog.csdn.net/weixin_40968009/article/details/129757947
今日推荐