最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Spring中@Import注解的作用和使用

 更新時(shí)間:2020年05月28日 11:41:50   作者:潘超博客  
這篇文章主要介紹了淺談Spring中@Import注解的作用和使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

@Import用來(lái)導(dǎo)入@Configuration注解的配置類(lèi)、聲明@Bean注解的bean方法、導(dǎo)入ImportSelector的實(shí)現(xiàn)類(lèi)或?qū)隝mportBeanDefinitionRegistrar的實(shí)現(xiàn)類(lèi)。

@Import注解的作用

查看Import注解源碼

/**
 * Indicates one or more {@link Configuration @Configuration} classes to import.
 *
 * <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML.
 * Only supported for classes annotated with {@code @Configuration} or declaring at least
 * one {@link Bean @Bean} method, as well as {@link ImportSelector} and
 * {@link ImportBeanDefinitionRegistrar} implementations.
 *
 * <p>{@code @Bean} definitions declared in imported {@code @Configuration} classes
 * should be accessed by using {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
 * injection. Either the bean itself can be autowired, or the configuration class instance
 * declaring the bean can be autowired. The latter approach allows for explicit,
 * IDE-friendly navigation between {@code @Configuration} class methods.
 *
 * <p>May be declared at the class level or as a meta-annotation.
 *
 * <p>If XML or other non-{@code @Configuration} bean definition resources need to be
 * imported, use {@link ImportResource @ImportResource}
 *
 * @author Chris Beams
 * @since 3.0
 * @see Configuration
 * @see ImportSelector
 * @see ImportResource
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {

 /**
 * The @{@link Configuration}, {@link ImportSelector} and/or
 * {@link ImportBeanDefinitionRegistrar} classes to import.
 */
 Class<?>[] value();
}

分析類(lèi)注釋得出結(jié)論:

  • 聲明一個(gè)bean
  • 導(dǎo)入@Configuration注解的配置類(lèi)
  • 導(dǎo)入ImportSelector的實(shí)現(xiàn)類(lèi)
  • 導(dǎo)入ImportBeanDefinitionRegistrar的實(shí)現(xiàn)類(lèi)

@Import注解的使用

聲明一個(gè)bean

package com.example.demo.bean;

public class TestBean1 {
}

package com.example.demo;

import com.example.demo.bean.TestBean1;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import({TestBean1.class})
@Configuration
public class AppConfig {
}

導(dǎo)入@Configuration注解的配置類(lèi)

package com.example.demo.bean;

public class TestBean2 {
}

package com.example.demo.bean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TestConfig {
  @Bean
  public TestBean2 getTestBean2(){
    return new TestBean2();
  }
}

package com.example.demo;

import com.example.demo.bean.TestBean1;
import com.example.demo.bean.TestConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import({TestBean1.class,TestConfig.class})
@Configuration
public class AppConfig {
}

導(dǎo)入ImportSelector的實(shí)現(xiàn)類(lèi)

package com.example.demo.bean;

public class TestBean3 {
}

package com.example.demo.bean;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

public class TestImportSelector implements ImportSelector {
  @Override
  public String[] selectImports(AnnotationMetadata importingClassMetadata) {
    return new String[]{"com.example.demo.bean.TestBean3"};
  }
}

package com.example.demo;

import com.example.demo.bean.TestBean1;
import com.example.demo.bean.TestConfig;
import com.example.demo.bean.TestImportSelector;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import({TestBean1.class,TestConfig.class,TestImportSelector.class})
@Configuration
public class AppConfig {
}

導(dǎo)入ImportBeanDefinitionRegistrar的實(shí)現(xiàn)類(lèi)

package com.example.demo.bean;

public class TestBean4 {
}

package com.example.demo.bean;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class TestImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
  @Override
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(TestBean4.class);
    registry.registerBeanDefinition("TestBean4", rootBeanDefinition);
  }
}

package com.example.demo;

import com.example.demo.bean.TestBean1;
import com.example.demo.bean.TestConfig;
import com.example.demo.bean.TestImportBeanDefinitionRegistrar;
import com.example.demo.bean.TestImportSelector;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import({TestBean1.class,TestConfig.class,TestImportSelector.class,TestImportBeanDefinitionRegistrar.class})
@Configuration
public class AppConfig {
}

最后,我們來(lái)看下導(dǎo)入結(jié)果:

package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

  @Test
  public void test() {
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
    String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
    System.out.println("--------------------------------------------------------");
    for (String beanDefinitionName: beanDefinitionNames) {
      System.out.println(beanDefinitionName);
    }
    System.out.println("--------------------------------------------------------");
  }
}

打印結(jié)果如下:

--------------------------------------------------------
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
appConfig
com.example.demo.bean.TestBean1
com.example.demo.bean.TestConfig
getTestBean2
com.example.demo.bean.TestBean3
TestBean4
--------------------------------------------------------

可以看出TestBean1,TestBean2,TestBean3,TestBean4通過(guò)不同的4種導(dǎo)入方法被導(dǎo)入SpringIOC容器中。 

到此這篇關(guān)于淺談Spring中@Import注解的作用和使用的文章就介紹到這了,更多相關(guān)Spring @Import注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot和VUE源碼直接整合打包成jar的踩坑記錄

    SpringBoot和VUE源碼直接整合打包成jar的踩坑記錄

    這篇文章主要介紹了SpringBoot和VUE源碼直接整合打包成jar的踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • java自動(dòng)裝箱拆箱深入剖析

    java自動(dòng)裝箱拆箱深入剖析

    基本數(shù)據(jù)(Primitive)類(lèi)型的自動(dòng)裝箱(autoboxing)、拆箱(unboxing)是自J2SE 5.0開(kāi)始提供的功能。java語(yǔ)言規(guī)范中說(shuō)道:在許多情況下包裝與解包裝是由編譯器自行完成的(在這種情況下包裝成為裝箱,解包裝稱(chēng)為拆箱)
    2012-11-11
  • Java中的升序和降序問(wèn)題

    Java中的升序和降序問(wèn)題

    這篇文章主要介紹了Java中的升序和降序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 如何基于ssm框架實(shí)現(xiàn)springmvc攔截器

    如何基于ssm框架實(shí)現(xiàn)springmvc攔截器

    這篇文章主要介紹了如何基于ssm框架實(shí)現(xiàn)springmvc攔截器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Spring5+SpringMvc+Hibernate5整合的實(shí)現(xiàn)

    Spring5+SpringMvc+Hibernate5整合的實(shí)現(xiàn)

    這篇文章主要介紹了Spring5+SpringMvc+Hibernate5整合的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • java中orElse和orElseGet方法區(qū)別小結(jié)

    java中orElse和orElseGet方法區(qū)別小結(jié)

    這篇文章主要給大家介紹了關(guān)于java中orElse和orElseGet方法區(qū)別的相關(guān)資料,兩者之間的區(qū)別細(xì)微,但是卻在某些場(chǎng)景下顯的很重要,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • Spring中的模塊與應(yīng)用場(chǎng)景詳解

    Spring中的模塊與應(yīng)用場(chǎng)景詳解

    這篇文章主要介紹了Spring中的模塊與應(yīng)用場(chǎng)景詳解,Spring 框架可以為 Java 應(yīng)用程序開(kāi)發(fā)提供全面的基礎(chǔ)設(shè)施支持,它是現(xiàn)在非常流行的 Java 開(kāi)源框架,對(duì)于一個(gè) Java 開(kāi)發(fā)人員來(lái)說(shuō),熟練掌握 Spring 是必不可少的,需要的朋友可以參考下
    2023-09-09
  • 通過(guò)實(shí)例解析spring環(huán)繞通知原理及用法

    通過(guò)實(shí)例解析spring環(huán)繞通知原理及用法

    這篇文章主要介紹了通過(guò)實(shí)例解析spring環(huán)繞通知原理及用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • SpringBoot快速搭建實(shí)現(xiàn)三步驟解析

    SpringBoot快速搭建實(shí)現(xiàn)三步驟解析

    這篇文章主要介紹了SpringBoot快速搭建實(shí)現(xiàn)三步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • MyBatis 多表查詢(xún)?nèi)N最常見(jiàn)的寫(xiě)法

    MyBatis 多表查詢(xún)?nèi)N最常見(jiàn)的寫(xiě)法

    這篇文章主要介紹了MyBatis 多表查詢(xún)?nèi)N最常見(jiàn)的寫(xiě)法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2025-04-04

最新評(píng)論

金门县| 扶沟县| 金平| 华安县| 贺州市| 永靖县| 大庆市| 牟定县| 峨眉山市| 双流县| 方城县| 彩票| 昭觉县| 杭州市| 儋州市| 抚松县| 大洼县| 潼南县| 富裕县| 贺州市| 清水县| 繁昌县| 泰顺县| 白河县| 德惠市| 株洲市| 陆川县| 赣榆县| 乌兰县| 汉源县| 金山区| 昭通市| 新巴尔虎左旗| 营山县| 太原市| 静宁县| 迭部县| 东至县| 乡城县| 大同县| 遂溪县|