@Import annotated notes of spring used in three ways

@

1, @ Import notes Notes

. 1, @Import only with class , @ Import achieved by way of examples of the rapid introduction of the spring IOC vessel was added

 

2, to join the IOC container are many ways, @ Import notes the relatively very leather, @Import annotation can be used to import third-party packages , of course @Bean annotation can be, but the way @Import notes quickly import more convenient

 


3, @ Import notes there are three usage

2, @ Import of three uses

Three uses @Import include:

1, direct way to fill an array of class
2, ImportSelector way [focus]
3, ImportBeanDefinitionRegistrar way

2.1, the first usage: directly fill an array of class

Direct filling array corresponding class, class 0 can have a plurality of arrays.

The syntax is as follows:

@Import({ 类名.class , 类名.class... })
public class TestDemo {

}

Corresponding import are to be added to the spring bean container, the bean class names in the container is in the full class name , class name such com.yc.

2.2, the second usage: ImportSelector way [emphasis]

The premise of this approach is that a class to implement the interface ImportSelector, if I want to use this method, the target object is Myclass this class analysis as follows:

Creating Myclass class and implement the interface ImportSelector

public class Myclass implements ImportSelector {
//既然是接口肯定要实现这个接口的方法
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[0];
    }
}

Analysis selectImports interface method implementation of:

  • 1, the return value: what we actually want to import components into a container full class name [ focus ]
  • 2, parameter: AnnotationMetadata represents the currently @Import annotated notes to label all the information is not the point []

Note that selectImports method returns an empty array but can not return null, otherwise it will be reported null pointer exception!

After completion of the above analysis, the specific use of the following steps:

Step 1: Create Myclass class and implement ImportSelector interface is used here to demonstrate a full class name is added to the return value

public class Myclass implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{"com.yc.Test.TestDemo3"};
    }
}

Step Two: Write TestDemo class, and marked the way of using ImportSelector Myclass class

@Import({TestDemo2.class,Myclass.class})
public class TestDemo {
        @Bean
        public AccountDao2 accountDao2(){
            return new AccountDao2();
        }

}

As can be seen, Yichun deliberately picked a cameo role @Bean notes, if notes are not very clear on @Bean shoes can refer to the vernacular to explain the Spring of @bean comment

The third step: preparation of test printing component class container

/**
 * 打印容器中的组件测试
 */
public class AnnotationTestDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(TestDemo.class);  //这里的参数代表要做操作的类

        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : beanDefinitionNames){
            System.out.println(name);
        }

    }
}

Step Four: Run results
Here Insert Picture Description

2.3, the third usage: ImportBeanDefinitionRegistrar way

It is also an interface similar to the second ImportSelector usage, 80% similarity, but this usage is relatively Custom Registration, as follows:

Step 1: Create Myclass2 class and implement the interface ImportBeanDefinitionRegistrar

public class Myclass2 implements ImportBeanDefinitionRegistrar {
//该实现方法默认为空
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
      
    }
}

Parametric analysis:

  • The first argument: annotationMetadata and parameters are the same as before ImportSelector information indicates that all notes currently @Import to comment marked
  • The second parameter indicates a register defines a bean

Step two: writing code, custom registration bean

public class Myclass2 implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        //指定bean定义信息(包括bean的类型、作用域...)
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(TestDemo4.class);
        //注册一个bean指定bean名字(id)
        beanDefinitionRegistry.registerBeanDefinition("TestDemo4444",rootBeanDefinition);
    }
}

Step Three: Write TestDemo class, and marked on the use ImportBeanDefinitionRegistrar way Myclass2 class

@Import({TestDemo2.class,Myclass.class,Myclass2.class})
public class TestDemo {

        @Bean
        public AccountDao2 accountDao222(){
            return new AccountDao2();
        }

}

Step Four: Run results
Here Insert Picture Description

3, three different ways @ Import annotated summary

The first use of: @Import(a container to be imported {components}): the component container is automatically registered, ID is fully qualified class name of the default

 

The second usage: ImportSelector: Returns the name of the component to be imported whole array of classes, springboot bottom with a particularly large number [ emphasis ]

 

The third usage: ImportBeanDefinitionRegistrar: manually register the bean container

These three ways Jieke usage mix in a @Import particular attention first and second are registered in a manner full class name, and the third can customize the way.

@Import annotation itself in many springboot used, especially where a particularly large number of second usage ImportSelector method used in springboot in particular to master!

If this article there is a little bit of help to you, then please point a chant praise, thank you ~

Finally, if there is insufficient or is not correct, please correct me criticism, grateful! If you have questions please leave a message, the absolute first time to reply!

I welcome you to focus on the public number, there are some java learning materials and a large wave of java e-books, such as Zhou Zhiming teacher depth java virtual machine, java programming ideas, the core technology volume, Westward design patterns, java concurrent programming combat ... .. is a java Bible, do not say fast car on Tomcat, ye who go! The main thing is to explore technology, yearning technology, the pursuit of technology, said good pots Friends is coming Oh ...

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/yichunguo/p/12122598.html