preface

As mentioned in the previous article, @Bean annotations are often used in conjunction with @Configuration, the former for methods and the latter for classes; In combination, the Bean is registered in the container. In fact, as it literally means, this annotation not only registers beans, it covers almost everything related to configuration.

The source code

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    @AliasFor( annotation = Component.class )
    String value(a) default "";

    boolean proxyBeanMethods(a) default true;
}
Copy the code

As you can see from the source code, it is also essentially an @Component annotation, so the annotation tag’s class itself will be registered as a Bean

use

It’s used in a way that we’ve talked about before

@Configuration
public class AppConfig {
    @Bean
    public MyBean myBean(a) {
        // instance, configure and return bean}}Copy the code

It instructs the class to generate one or more @Bean methods, which can be handled by the Spring container, generating BeanDefination and service requests for these beans at run time.