@Import注解作用

@Import注解作用

理解springboot自动装配时,发现@SpringBootApplication注解下的@EnableAutoConfiguration注解头上有一个@Import注解。

关于这个注解的作用,上网查找后发现理解的不是很明白,于是写了下面的Demo去理解。

  • 两个pojo类:
public class Person {
}

public class Student {
}
  • 测试类
@Configuration
@Import({Student.class})
public class ImportConfig {

    @Bean
    public 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;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static <T> T getApplication(Class<T> clazz){
        return applicationContext.getBean(clazz);
    }

}
  • 测试结果
====com.yjh.pojo.Student@649bec2e
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接口的类。

相关推荐

  1. @Import注解作用

    2024-03-15 01:22:01       46 阅读
  2. Spring---@Import注解

    2024-03-15 01:22:01       48 阅读
  3. @PostConstruct注解作用

    2024-03-15 01:22:01       45 阅读
  4. @JsonFormat注解作用

    2024-03-15 01:22:01       19 阅读
  5. SpringBoot中@PostConstruct注解作用

    2024-03-15 01:22:01       55 阅读
  6. 39.@Autowired 注解有什么作用

    2024-03-15 01:22:01       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 01:22:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 01:22:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 01:22:01       82 阅读
  4. Python语言-面向对象

    2024-03-15 01:22:01       91 阅读

热门阅读

  1. virsh管理虚拟机的命令行工具

    2024-03-15 01:22:01       44 阅读
  2. 新手如何学习Kubernetes【入门篇】

    2024-03-15 01:22:01       46 阅读
  3. 模块化(理解)

    2024-03-15 01:22:01       37 阅读
  4. 深入理解 MySQL 中的 CASE 语句:从基础到实战

    2024-03-15 01:22:01       39 阅读
  5. 配置 conda为国内源

    2024-03-15 01:22:01       42 阅读
  6. git 批量clone,pull 项目

    2024-03-15 01:22:01       43 阅读
  7. python--运算符和字符串

    2024-03-15 01:22:01       45 阅读