Loading

Full @Configuration 和“lite” @Bean mode 详解

一、官方文档

官方文档对于Full @Configuration和 lit mode的描述:

The @Bean methods in a regular Spring component are processed differently than their counterparts inside a Spring @Configuration class. The difference is that @Component classes are not enhanced with CGLIB to intercept the invocation of methods and fields. CGLIB proxying is the means by which invoking methods or fields within @Bean methods in @Configuration classes creates bean metadata references to collaborating objects. Such methods are not invoked with normal Java semantics but rather go through the container in order to provide the usual lifecycle management and proxying of Spring beans, even when referring to other beans through programmatic calls to @Bean methods. In contrast, invoking a method or field in a @Bean method within a plain @Component class has standard Java semantics, with no special CGLIB processing or other constraints applying.

机译:常规Spring组件中的@Bean方法与Spring @Configuration类中的对应方法处理方式不同。不同之处在于,@Component类没有使用CGLIB增强来拦截方法和字段的调用。CGLIB代理是通过调用@Configuration类中的@Bean方法中的方法或字段来创建对协作对象的bean元数据引用的方法。这些方法不是用普通的Java语义调用的,而是通过容器来提供Spring bean通常的生命周期管理和代理,即使在通过对@Bean方法的编程调用引用其他bean时也是如此。相反,在普通@Component类中调用@Bean方法中的方法或字段具有标准的Java语义,不需要使用特殊的CGLIB处理或其他约束。

When @Bean methods are declared within classes that are not annotated with @Configuration, they are referred to as being processed in a “lite” mode. Bean methods declared in a @Component or even in a plain old class are considered to be “lite”, with a different primary purpose of the containing class and a @Bean method being a sort of bonus there. For example, service components may expose management views to the container through an additional @Bean method on each applicable component class. In such scenarios, @Bean methods are a general-purpose factory method mechanism.

Unlike full @Configuration, lite @Bean methods cannot declare inter-bean dependencies. Instead, they operate on their containing component’s internal state and, optionally, on arguments that they may declare. Such a @Bean method should therefore not invoke other @Bean methods. Each such method is literally only a factory method for a particular bean reference, without any special runtime semantics. The positive side-effect here is that no CGLIB subclassing has to be applied at runtime, so there are no limitations in terms of class design (that is, the containing class may be final and so forth).

In common scenarios, @Bean methods are to be declared within @Configuration classes, ensuring that “full” mode is always used and that cross-method references therefore get redirected to the container’s lifecycle management. This prevents the same @Bean method from accidentally being invoked through a regular Java call, which helps to reduce subtle bugs that can be hard to track down when operating in “lite” mode.

机译:当@Bean方法在没有使用@Configuration注释的类中声明时,它们被称为在“lite”模式下处理。在@Component中声明的Bean方法,甚至在普通的旧类中声明的Bean方法,都被认为是“lite”,包含类的主要目的不同,而@Bean方法在这里是一种附加功能。例如,服务组件可以通过每个适用组件类上的附加@Bean方法向容器公开管理视图。在这种情况下,@Bean方法是一种通用的工厂方法机制。

与Full @Configuration不同,Lite @Bean方法无法声明bean之间的依赖关系。相反,它们在其包含组件的内部状态上运行,并且可选地在他们可能声明的参数上运行。因此,这种@Bean方法不应调用其他@Bean方法。每个此类方法实际上只是特定Bean参考的工厂方法,而没有任何特殊的运行时语义。这里的积极副作用是在运行时不需要应用CGLIB子类,所以在类设计方面没有限制(也就是说,包含的类可能是final类,等等)。

在常见的场景中,@Bean方法将在@Configuration类中声明,以确保始终使用“full”模式,并因此将交叉方法引用重定向到容器的生命周期管理。这可以防止通过常规Java调用意外调用相同的@Bean方法,这有助于减少在“lite”模式下操作时难以跟踪的细微bug。

二、定义

  • lite @Bean mode :当@Bean方法在没有使用@Configuration注解的类中声明时称之为lite @Bean mode
  • Full @Configuration:如果@Bean方法在使用@Configuration注解的类中声明时称之为Full @Configuration

更直观点:

  • lite @Bean mode :Spring创建的Bean对象就是实际对象(不会呗CGLIB代理)
  • Full @Configuration:Spring创建的Bean对象是一个CGLIB代理对象

Spring 5.2 之后

@Configuration注解新增了proxyBeanMethods属性,因此只有当属性值为true(默认)时,才表示为Full Bean,显示声明为false时表示为Lite Bean

三、演示

1、Full @Configuration

// proxyBeanMethods的默认值为true,可以去掉显示声明
@Configuration(proxyBeanMethods = true)
public class MyConfiguration {

    @Bean
    public User user() {
        User user = new User();
        System.out.println(user);
        return user;
    }

    @Bean
    public User user1(User user) {
        System.out.println("=======user1=======");
        System.out.println(user);
        System.out.println(user());
        System.out.println("=======user1=======");
        return user;
    }
}

@Test
public full() {
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfiguration.class);
        User user1 = applicationContext.getBean("user1", User.class);
}

结果:

image-20220123184431906

分析:

@Bean是在使用了@Configuration注解的类上被声明的,属于Full @Configuration@Configuration类中的@Bean地方会被CGLIB进行代理。Spring会拦截该方法的执行,在默认单例情况下,容器中只有一个Bean,所以我们多次调用user()方法,获取的都是同一个对象。

注意:被CGLIB的方法是不能被声明为privatefinal,因为CGLIB是通过生成子类来实现代理的,privatefinal方法是不能被子类Override的,也就是说,Full @Configuration模式下,@Bean的方法是不能不能被声明为privatefinal,不然在启动时Spring会直接报错。

2、Lite mode

现在将proxyBeanMethods属性的值显示声明为false

@Configuration(proxyBeanMethods = false)
public class MyConfiguration {

    @Bean
    public User user() {
        User user = new User();
        System.out.println(user);
        return user;
    }

    @Bean
    public User user1(User user) {
        System.out.println("=======user1=======");
        System.out.println(user);
        System.out.println(user());
        System.out.println("=======user1=======");
        return user;
    }
}

@Test
public lite() {
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfiguration.class);
        User user1 = applicationContext.getBean("user1", User.class);
}

结果:

image-20220123185508916

分析:在lite @Bean mode模式下, @Bean方法不会被CGLIB代理,所以多次调用user()会生成多个user对象。

posted @ 2022-01-23 19:00  OMaster  阅读(242)  评论(0编辑  收藏  举报