SpringBoot Configuration Full 模式和 Lite 模式

Full 模式和 Lite 模式:

  • 最佳实践:

    • 配置类组件之间无依赖关系用 Lite 模式加速容器启动过程,减少判断
    • 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用 Full 模式
package com.atguigu.boot.bean.config;

import com.atguigu.boot.bean.Pet;
import com.atguigu.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类里面使用 @Bean 标注在方法上给容器注册组件,默认也是单例的
 */

@Configuration(proxyBeanMethods = false)  // 告诉 SpringBoot 这是一个配置类 = 配置文件
public class MyConfig {

    @Bean
    public User user01() {
        return new User("harrytsz", 20);
    }

    @Bean
    public Pet cat() {
        return new Pet("辛巴");
    }
}
com.atguigu.boot.bean.config.MyConfig@15f193b8
false

package com.atguigu.boot.bean.config;

import com.atguigu.boot.bean.Pet;
import com.atguigu.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类里面使用 @Bean 标注在方法上给容器注册组件,默认也是单例的
 */

@Configuration(proxyBeanMethods = true)  // 告诉 SpringBoot 这是一个配置类 = 配置文件
public class MyConfig {

    @Bean
    public User user01() {
        return new User("harrytsz", 20);
    }

    @Bean
    public Pet cat() {
        return new Pet("辛巴");
    }
}
com.atguigu.boot.bean.config.MyConfig$$EnhancerBySpringCGLIB$$1f31f892@bf71cec
true
Last modification:July 15, 2021
如果觉得我的文章对你有用,请随意赞赏