新开传奇网站180合击,有服务器了怎么做网站,WordPress防伪插件,国外的自建网站怎么做Import注解作用
理解springboot自动装配时#xff0c;发现SpringBootApplication注解下的EnableAutoConfiguration注解头上有一个Import注解。
关于这个注解的作用#xff0c;上网查找后发现理解的不是很明白#xff0c;于是写了下面的Demo去理解。
两个pojo类#xff1…Import注解作用
理解springboot自动装配时发现SpringBootApplication注解下的EnableAutoConfiguration注解头上有一个Import注解。
关于这个注解的作用上网查找后发现理解的不是很明白于是写了下面的Demo去理解。
两个pojo类
public class Person {
}public class Student {
}测试类
Configuration
Import({Student.class})
public class ImportConfig {Beanpublic Person person(){return new Person();}public static void main(String[] args) {AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext(ImportConfig.class,ApplicationTestConfig.class);String[] names context.getBeanDefinitionNames();Student student ApplicationTestConfig.getApplication(Student.class);System.out.println(student);for (String name : names) {System.out.println(name);}}}ApplicationTestConfig类如下
Configuration
public class ApplicationTestConfig implements ApplicationContextAware {private static ApplicationContext applicationContext;Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext applicationContext;}public static T T getApplication(ClassT clazz){return applicationContext.getBean(clazz);}}测试结果
com.yjh.pojo.Student649bec2e
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
importConfig
applicationTestConfig
com.yjh.pojo.Student
person综上其实Import注解的作用就是往当前类注入一个Bean方便当前类的后续方法调用该Bean跟Bean的作用差不多。
不过Import注解除了可以注入普通的Bean外也可以注入实现了ImportSelector接口和ImportBeanDefinitionRegistrar接口的类。