You learned in the previous article about @Configuration, which allows you to register components. In addition, many annotations are also available, just as they were when we learned spring. For example, @bean, @Component, @Controller, @Service, @repository, etc.
This article introduces another way to add components to a container: the @import annotation, which imports components into the container.
@import Imports components
usage
The use of @import is simple and can be placed in any configuration class or component.
Also, it’s an array, so you can import multiple components. Such as:
@Import({User.class, DBHelper.class}) @Configuration(proxyBeanMethods = false) public class MyConfig { @Bean("user1") public User user01(){ User pingguo = new User("pingguo",20); pingguo.setPet(tomcatPet()); return pingguo; } @Bean("pet1") public Pet tomcatPet(){ return new Pet("tomcat"); }}Copy the code
There are two components imported here, user. class and dbHelper. class.
Where user. class is my custom class, and dbhelper. class is a random third party package class.
Once written, @import can automatically create the two types of components in the container.
validation
Verify that the above two components are present in the container.
The code is still added to the main method, put all the code out for later viewing.
// Mark this as a SpringBoot application, this class is the main program class, @springBootApplication (scanBasePackages = "com.pingguo") public Class MainApplication {public static void The main (String [] args) {/ / return the IOC container final ConfigurableApplicationContext run = SpringApplication. Run (MainApplication. Class, args); / / check container components final String [] beanDefinitionNames = run. GetBeanDefinitionNames (); for (String name: beanDefinitionNames) { System.out.println(name); } // Get components from the container MyConfig bean = run.getBean(myconfig.class); System.out.println(" Config class is also component: "+ bean); User user1 = bean.user01(); User user2 = bean.user01(); System.out.println(user1 == user2); // dependencies User user01 = run.getBean("user1", user.class); Pet pet1 = run.getBean("pet1", Pet.class); System.out.println(" dependency: "+ (user01.getpet () == pet1)); [] beanNamesForType = run.getBeannamesForType (user.class); // @import component String[] beanNamesForType = run.getBeannamesForType (user.class); System.out.println("==== get bean of type User ===="); for (String s : beanNamesForType) { System.out.println(s); } system.out. println("==== get DBHelper type bean ===="); DBHelper bean1 = run.getBean(DBHelper.class); System.out.println(bean1); }}Copy the code
Look at the component-related code at the end of // @import.
Because we registered multiple components of type User in our previous study, we are going through the printout.
Run results, as you can see there are two User type of bean, respectively is user1 and com. The pingguo. Boot. Bean. The User.
For user1, it was previously registered using @bean.
For the com. Pingguo. Boot. Bean. The User, is, this time using the Import Import into the default component name is the name of the class.
Finally a ch. Qos. Logback. Core. The DBHelper @ 4943 defe, this is the import of third-party packages, successfully registered the components in the container.