@Configuration注解

@Configuration注解 与 Full模式和Lite模式

@Configuration 该注解可以声明该类是一个配置类

当实体类之间有依赖关系时
Full模式:配置类的proxyBeanMethods的值为true时,每个@Bean方法被调用多少次返回的组件都是单实例的;
Lite模式:配置类的proxyBeanMethods的值为false时,每个@Bean方法被调用多少次返回的组件都是新创建的;

配置类组件之间有依赖关系建议用Full模式保证使用的是容器中的组件

配置类组件之间无依赖关系建议用Lite模式加速容器启动过程,减少判断

系统要求

Java 8+

Maven 3.6.6 +

创建Maven项目工程

引入 pom.xml 依赖
    <!--1.导入父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
    </parent>

    <!--2.导入springBoot的Web场景-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
 <build>
        <!--
        SpringBoot项目默认打成jar包
            传统web项目我们需要打成war包,放入tomcat中运行,springBoot项目我们可以导入一个插件,
            在项目打成jar包的同时,还会顺带打包运行环境,所以只要直接运行jar包也可以访问项目
        -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
创建实体类 pojo.User 类
package com.xiang.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Created by IntelliJ IDEA.
 * User: xiang
 * Date: 2021/10/12 11:04
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private int age;
    private String gender;
    private double money;
    
    //    添加属性
    private  Dog dog;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", money=" + money +
                '}';
    }
}

创建实体类 pojo.Dao 类
package com.xiang.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Created by IntelliJ IDEA.
 * User: xiang
 * Date: 2021/10/12 11:02
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dog {
    private String name;
    private int age;
    private String gender;

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

创建配置类 MyConfig 类
package com.xiang.config;

import com.xiang.pojo.Dog;
import com.xiang.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by IntelliJ IDEA.
 * User: xiang
 * Date: 2021/10/12 11:07
 */
//@Configuration//该注解表明这个类是一个配置类,效果等同于配置文件

@Configuration(proxyBeanMethods = false)

/**
 * 当实体类之间有依赖关系时
 * 	Full模式:配置类的proxyBeanMethods的值为true时,每个@Bean方法被调用多少次返回的组件都是单实例的;
 * 	Lite模式:配置类的proxyBeanMethods的值为false时,每个@Bean方法被调用多少次返回的组件都是新创建的;
 */

public class MyConfig {
    /**
     * private int id;
     * private String name;
     * private int age;
     * private String gender;
     * private double money;
     *
     * @return
     */
    @Bean
    //在spring容器中添加组件;方法名为组件的id;返回值类型为组件类型;返回值就是组件具体的实例
    public User user() {
//        return new User(1, "向向", 20, "男", 10000.00);
        return new User(1, "向向", 20, "男", 10000.00, dog());
    }

    /**
     * private String name;
     * private int age;
     * private String gender;
     *
     * @return
     */
    @Bean("dog")
    //id名也可以自定义,将id由dog改为erHa
    public Dog dog() {
        return new Dog("二哈", 2, "女");
    }
}

主程序进行测试 MainApplication 类
package com.xiang;

import com.xiang.config.MyConfig;
import com.xiang.pojo.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * Created by IntelliJ IDEA.
 * User: xiang
 * Date: 2021/10/12 11:00
 */
/*这是主程序类,类名可以随意起,需要注意的是该类必须与其他类同包*/
@SpringBootApplication//说明这是一个springBoot应用
public class MainApplication {
    //main方法是所有程序的入口
    public static void main(String[] args) {
        //将主程序跑起来,需要调用SpringApplication类的run方法
        //参数:说明要将哪个主程序跑起来,传入参数
        //返回值:IOC容器
        ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
        //遍历容器中的所有组件
        //getBeanDefinitionNames():返回容器中的所有组件
        String[] names = context.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println("遍历容器中的所有组件:----->"+name);
        }

        System.out.println("/**************************************************************/");


        //获取单个组件,获取的实例是单例的
        User user = context.getBean("user", User.class);
        User user2 = context.getBean("user", User.class);
        System.out.println("获取单个组件,获取的实例是单例的:----->"+user);
        System.out.println("获取单个组件,获取的实例是单例的:----->"+user2);
        System.out.println("获取单个组件,获取的实例是单例的(user == user2):----->"+(user == user2));
        System.out.println("获取单个组件,获取的实例是单例的user.equals(user2):----->"+user.equals(user2));
        System.out.println("/**************************************************************/");



        //配置类本身就是一个组件,获取的实例是单例的
        MyConfig config = context.getBean(MyConfig.class);
        System.out.println(config);
        User user1 = config.user();
        User user3 = config.user();
        System.out.println("配置类本身就是一个组件,获取的实例是单例的:----->"+user1);
        System.out.println("配置类本身就是一个组件,获取的实例是单例的:----->"+user3);
        System.out.println("配置类本身就是一个组件,获取的实例是单例的(user1==user3):----->"+(user1==user3));
        System.out.println("配置类本身就是一个组件,获取的实例是单例的(user1.equals(user3)):----->"+(user1.equals(user3)));
        
         System.out.println("/**************************************************************/");
        User user4 = context.getBean("user", User.class);
        Dog dog = context.getBean("dog", Dog.class);
        System.out.println(user4);
        System.out.println(dog);
        System.out.println("user4.getDog()==dog:----->"+(user4.getDog()==dog));
        System.out.println("user4.getDog().equals(dog):----->"+user4.getDog().equals(dog));

    }
}

运行结果
遍历容器中的所有组件:----->org.springframework.context.annotation.internalConfigurationAnnotationProcessor
遍历容器中的所有组件:----->org.springframework.context.annotation.internalAutowiredAnnotationProcessor
遍历容器中的所有组件:----->org.springframework.context.annotation.internalCommonAnnotationProcessor
遍历容器中的所有组件:----->org.springframework.context.event.internalEventListenerProcessor
遍历容器中的所有组件:----->org.springframework.context.event.internalEventListenerFactory
遍历容器中的所有组件:----->mainApplication
遍历容器中的所有组件:----->org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
遍历容器中的所有组件:----->myConfig
遍历容器中的所有组件:----->user
遍历容器中的所有组件:----->erHa
......
/**************************************************************/
获取单个组件,获取的实例是单例的:----->User{id=1, name='向向', age=20, gender='男', money=10000.0}
获取单个组件,获取的实例是单例的:----->User{id=1, name='向向', age=20, gender='男', money=10000.0}
获取单个组件,获取的实例是单例的(user == user2):----->true
获取单个组件,获取的实例是单例的user.equals(user2):----->true
/**************************************************************/
com.xiang.config.MyConfig$$EnhancerBySpringCGLIB$$121cfb3f@69eb86b4
配置类本身就是一个组件,获取的实例是单例的:----->User{id=1, name='向向', age=20, gender='男', money=10000.0}
配置类本身就是一个组件,获取的实例是单例的:----->User{id=1, name='向向', age=20, gender='男', money=10000.0}
配置类本身就是一个组件,获取的实例是单例的(user1==user3):----->true
配置类本身就是一个组件,获取的实例是单例的(user1.equals(user3)):----->true
/**************************************************************/
User{id=1, name='向向', age=20, gender='男', money=10000.0}
Dog{name='二哈', age=2, gender='女'}
user4.getDog()==dog:----->false
user4.getDog().equals(dog):----->true

posted @ 2021-10-12 13:16  阿向向  阅读(2026)  评论(0编辑  收藏  举报