当前位置: 首页 > news >正文

spring容器注入---@Import

上文介绍了通过xml配置、注解(@Component、@Controller、@Service、@Repository等)将指定的 bean 注入到 Spring 容器中,后面又介绍了通过 @Configuration@Bean 这两个注解配合使用来将原来配置在 xml 文件里的 Bean 通过 Java 代码的方式将 Bean 注入到 Spring 容器中。

今天,介绍第三种注入 spring 容器的方法:@Import

@Import的作用:

  1. @Import 用来导入 @Configuration 注解的配置类、注入普通类。
  2. 导入ImportSelector的实现类。
  3. 导入ImportBeanDefinitionRegistrar的实现类。

导入普通的POJO

1、新建类Stu1:

package impotTest.pojo;

public class Stu1 {
}

2、新建@Import注解修饰配置类MyImport

package impotTest.pojo.imports;
...

@Configuration
@Import({Stu1.class})
public class MyImport {
}

3、测试类:

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name + ":" + context.getBean(name));
        }
        System.out.println("++++++++++++++");

        MyImport myImport = (MyImport) context.getBean("myImport");
        System.out.println(myImport);

        Object bean1 = context.getBean("impotTest.pojo.Stu1");
        System.out.println(bean1);
    }
}

运行结果:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor: org.springframework.context.annotation.ConfigurationClassPostProcessor@17046283
org.springframework.context.annotation.internalAutowiredAnnotationProcessor: org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@5bd03f44
org.springframework.context.annotation.internalCommonAnnotationProcessor: org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@29626d54
org.springframework.context.event.internalEventListenerProcessor: org.springframework.context.event.EventListenerMethodProcessor@5a63f509
org.springframework.context.event.internalEventListenerFactory: org.springframework.context.event.DefaultEventListenerFactory@6e4784bc
org.springframework.aop.config.internalAutoProxyCreator: proxyTargetClass=false; optimize=false; opaque=false; exposeProxy=false; frozen=false
myImport: impotTest.pojo.imports.MyImport$$EnhancerBySpringCGLIB$$e2546f15@70e8f8e
impotTest.pojo.Stu1: impotTest.pojo.Stu1@34b7ac2f
++++++++++++++
impotTest.pojo.imports.MyImport$$EnhancerBySpringCGLIB$$e2546f15@70e8f8e
impotTest.pojo.Stu1@34b7ac2f

注入的类Stu1均是以全类名impotTest.pojo.Stu1)作为name注册到了容器中。

实现ImportSelector的方式

1、新建类Stu2:

package impotTest.pojo;

public class Stu2 {
}

2、新建MyImportSelector 实现ImportSelector

package impotTest.pojo.imports;
...

public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{Stu2.class.getName()};
    }
}

通过字符串数组的方式返回配置类的全限定名。

3、将MyImportSelector加入配置类MyImport

@Configuration
@Import({Stu1.class,MyImportSelector.class})
public class MyImport {
}

4、测试类:

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name + ": " + context.getBean(name));
        }
        System.out.println("++++++++++++++");

        MyImport myImport = (MyImport) context.getBean("myImport");
        System.out.println(myImport);

        Object bean1 = context.getBean("impotTest.pojo.Stu1");
        System.out.println(bean1);

        Object bean2 = context.getBean("impotTest.pojo.Stu2");
        System.out.println(bean2);

        System.out.println("+++++");
    }
}

运行结果:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor: org.springframework.context.annotation.ConfigurationClassPostProcessor@4b0b0854
org.springframework.context.annotation.internalAutowiredAnnotationProcessor: org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@19bb07ed
org.springframework.context.annotation.internalCommonAnnotationProcessor: org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@10e41621
org.springframework.context.event.internalEventListenerProcessor: org.springframework.context.event.EventListenerMethodProcessor@353d0772
org.springframework.context.event.internalEventListenerFactory: org.springframework.context.event.DefaultEventListenerFactory@2667f029
org.springframework.aop.config.internalAutoProxyCreator: proxyTargetClass=false; optimize=false; opaque=false; exposeProxy=false; frozen=false
myImport: impotTest.pojo.imports.MyImport$$EnhancerBySpringCGLIB$$49a6e31e@e056f20
impotTest.pojo.Stu1: impotTest.pojo.Stu1@67a20f67
impotTest.pojo.Stu2: impotTest.pojo.Stu2@57c758ac
++++++++++++++
impotTest.pojo.imports.MyImport$$EnhancerBySpringCGLIB$$49a6e31e@e056f20
impotTest.pojo.Stu1@67a20f67
impotTest.pojo.Stu2@57c758ac
+++++

实现ImportBeanDefinitionRegistrar的方式

1、新建类Stu3:

package impotTest.pojo;

public class Stu3 {
}

2、新建MyImportBeanDefinitionRegistrar实现ImportBeanDefinitionRegistrar

package impotTest.pojo.imports;
...

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        registry.registerBeanDefinition("stu3", new RootBeanDefinition(Stu3.class));
    }
}

registerBeanDefinition方法第一个参数可以指定注入容器的bean名称。本例中指定bean名称为 “stu3

3、将MyImportBeanDefinitionRegistrar加入配置类MyImport

@Configuration
@Import({Stu1.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
public class MyImport {
}

4、测试类:

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name + ": " + context.getBean(name));
        }
        System.out.println("++++++++++++++");

        MyImport myImport = (MyImport) context.getBean("myImport");
        System.out.println(myImport);

        Object bean1 = context.getBean("impotTest.pojo.Stu1");
        System.out.println(bean1);

        Object bean2 = context.getBean("impotTest.pojo.Stu2");
        System.out.println(bean2);

        Object bean3 = context.getBean("stu3");
        System.out.println(bean3);

        System.out.println("+++++");
    }
}

运行结果:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor: org.springframework.context.annotation.ConfigurationClassPostProcessor@67a20f67
org.springframework.context.annotation.internalAutowiredAnnotationProcessor: org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@57c758ac
org.springframework.context.annotation.internalCommonAnnotationProcessor: org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@a9cd3b1
org.springframework.context.event.internalEventListenerProcessor: org.springframework.context.event.EventListenerMethodProcessor@13e39c73
org.springframework.context.event.internalEventListenerFactory: org.springframework.context.event.DefaultEventListenerFactory@64cd705f
org.springframework.aop.config.internalAutoProxyCreator: proxyTargetClass=false; optimize=false; opaque=false; exposeProxy=false; frozen=false
myImport: impotTest.pojo.imports.MyImport$$EnhancerBySpringCGLIB$$649a2b62@2667f029
impotTest.pojo.Stu1: impotTest.pojo.Stu1@9225652
impotTest.pojo.Stu2: impotTest.pojo.Stu2@654f0d9c
stu3: impotTest.pojo.Stu3@6a400542
++++++++++++++
impotTest.pojo.imports.MyImport$$EnhancerBySpringCGLIB$$649a2b62@2667f029
impotTest.pojo.Stu1@9225652
impotTest.pojo.Stu2@654f0d9c
impotTest.pojo.Stu3@6a400542
+++++

小结

spring容器注入三种方式:

  1. 通过xml配置、注解
  2. 通过 @Configuration 与 @Bean 这两个注解配合
  3. @Import 方式注入

相关文章:

  • 怎样创建网站收益/做网络推广
  • wordpress弹窗评论/qq推广链接
  • 网站推广方式和手段/看广告收益最高的软件
  • 建设网站网站/网络宣传方式有哪些
  • wordpress 自定义登陆页面/百度seo关键词优化推荐
  • 百度云域名怎么做网站/seo搜索引擎优化总结报告
  • day20-网络编程
  • PCL点云处理之NDT正态分布变换配准实验(附完整代码,参数设置,效果展示)(九十二)
  • Open3D 点云法向量重定向(基于连续切平面,Python版本)
  • weblogic反序列化之T3协议
  • solidity 安全 未初始化指针的风险——天上不会掉馅饼
  • 代码整洁提升方案
  • 【Linux】Linux命令行git和Linux调试器-gdb的使用
  • 字节青训营——前端小计day1
  • 一次线上事故,我顿悟了MongoDB的精髓
  • 【链表经典题目】总结篇
  • 浅显易懂的三次握手与四次挥手
  • 【C++11】—— lambda表达式