The definition of spring learning bean

Keywords: Java Spring xml

In addition to @ bean defining beans, spring provides other forms of definition.

@Component, @ Repository, @ Service, @ Controller and @ Configuration

@Component is a general annotation of spring definition bean s, @ Repository, @ Service and @ Controller, all of which inherit @ component.
@Repository is used for persistence layer, @ Service is used for Service layer, @ Controller is used for control layer.
If it's just to define bean s, you can use any one of them, but in order to distinguish which scenario to use and the future expansion of spring, you still need to use the annotation of the corresponding scenario.
@The main Configuration scenario is to use @ Configuration as the Configuration file to introduce bean s, such as the previous test code.
The default is the first lowercase class name. If you want to customize it, you can define the name you want after brackets, such as @ Service("abc").
After MyConfig and ComponentScan annotation, it is the path of the scan package.

@Configuration
@ComponentScan(value="com.learn.annotation")
public class MyConfig {

}

MyComponent

@Component
public class MyComponent {
}

The other notes are the same. We will not repeat them here.
Test code:

@Test
public void test(){
    ApplicationContext app =  new AnnotationConfigApplicationContext(MyConfig.class);
    System.out.println(app.getBean("myConfig"));
    System.out.println(app.getBean("myDao"));
    System.out.println(app.getBean("myComponent"));
    System.out.println(app.getBean("myService"));
    System.out.println(app.getBean("myController"));
}

Operation result:

@import

In the XML configuration file, the < import > tag is used. In the annotation, the @ import annotation is used. You can import normal beans or configuration beans.
This approach simplifies container instantiation because you only need to deal with one class without having to remember a large number of @ Configuration classes during construction.
MyConfig

@Configuration
@Import({MyComponent.class,MyService.class,MyDao.class,MyController.class})
public class MyConfig {

}

Test method:

@Test
public void test(){
    ApplicationContext app =  new AnnotationConfigApplicationContext(MyConfig.class);
    String[] names = app.getBeanDefinitionNames();
    for (String name : names) {
        System.out.println(name);
    }
}

Operation result:

Although annotationconfigpplicationcontext only introduces MyConfig.class, there are multiple classes import ed in MyConfig, so they are printed out. Unlike @ bean, the bean id is the full class name.

ImportSelector

In addition to the @ import annotation, you can also introduce classes by implementing the ImportSelector interface.
MyConfig

@Configuration
@Import(MyImportSelector.class)
public class MyConfig {

}

MyImportSelector

public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{"com.learn.annotation.MyComponent","com.learn.annotation.MyService"};
    }
}
@Test
public void test(){
    ApplicationContext app =  new AnnotationConfigApplicationContext(MyConfig.class);
    String[] names = app.getBeanDefinitionNames();
    for (String name : names) {
        System.out.println(name);
    }
}

The operation results are as follows:

Note that MyImportSelector is not in the container.

@Conditional

When we instantiate a bean according to different scenarios, such as whether or not to instantiate a bean according to different environment variables, we can use @ Conditional annotation.
Let's simulate a simple example. If the bean name is one, it will be instantiated unless it is not.
MyConditional

public class MyConditional implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        StandardMethodMetadata standardMethodMetadata = ((StandardMethodMetadata) annotatedTypeMetadata);
        return standardMethodMetadata.getMethodName().equals("one");
    }
}

MyConfig

@Configuration
public class MyConfig {
    @Bean()
    @Conditional(MyConditional.class)
    public One one(){
        return new One();
    }

    @Bean()
    @Conditional(MyConditional.class)
    public Two two(){
        return new Two();
    }
}

Test code

@Test
public void test(){
    ApplicationContext app =  new AnnotationConfigApplicationContext(MyConfig.class);
    String[] names = app.getBeanDefinitionNames();
    for (String name : names) {
        System.out.println(name);
    }
}

The operation results are as follows:

As you can see, the two bean is not printed out.

Posted by malcx on Mon, 21 Oct 2019 22:22:17 -0700