真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

spring容器注入---@Import-創(chuàng)新互聯(lián)

上文介紹了通過xml配置、注解(@Component、@Controller、@Service、@Repository等)將指定的 bean 注入到 Spring 容器中,后面又介紹了通過@Configuration@Bean這兩個注解配合使用來將原來配置在 xml 文件里的 Bean 通過 Java 代碼的方式將 Bean 注入到 Spring 容器中。

成都網(wǎng)站設計、網(wǎng)站制作介紹好的網(wǎng)站是理念、設計和技術的結合。創(chuàng)新互聯(lián)擁有的網(wǎng)站設計理念、多方位的設計風格、經(jīng)驗豐富的設計團隊。提供PC端+手機端網(wǎng)站建設,用營銷思維進行網(wǎng)站設計、采用先進技術開源代碼、注重用戶體驗與SEO基礎,將技術與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺化效果。

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

@Import的作用:

  1. @Import 用來導入 @Configuration 注解的配置類、注入普通類。
  2. 導入ImportSelector的實現(xiàn)類。
  3. 導入ImportBeanDefinitionRegistrar的實現(xiàn)類。
導入普通的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注冊到了容器中。

實現(xiàn)ImportSelector的方式

1、新建類Stu2:

package impotTest.pojo;

public class Stu2 {}

2、新建MyImportSelector實現(xiàn)ImportSelector

package impotTest.pojo.imports;
...

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

通過字符串數(shù)組的方式返回配置類的全限定名。

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
+++++
實現(xiàn)ImportBeanDefinitionRegistrar的方式

1、新建類Stu3:

package impotTest.pojo;

public class Stu3 {}

2、新建MyImportBeanDefinitionRegistrar實現(xiàn)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方法第一個參數(shù)可以指定注入容器的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 方式注入

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧


本文標題:spring容器注入---@Import-創(chuàng)新互聯(lián)
本文地址:http://weahome.cn/article/gcddh.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部