欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot自動(dòng)裝配之@Import深入講解

 更新時(shí)間:2023年01月16日 09:04:44   作者:不死鳥(niǎo).亞歷山大.狼崽子  
由于最近的項(xiàng)目需求,需要在把配置類(lèi)導(dǎo)入到容器中,通過(guò)查詢(xún),使用@Import注解就能實(shí)現(xiàn)這個(gè)功能,@Import注解能夠幫我們吧普通配置類(lèi)(定義為Bean的類(lèi))導(dǎo)入到IOC容器中

@Enable*底層依賴(lài)于@Import注解導(dǎo)入一些類(lèi),使用@Import導(dǎo)入的類(lèi)會(huì)被Spring加載到IOC容器中。而@Import提供4中用法:

  • 導(dǎo)入Bean
  • 導(dǎo)入配置類(lèi)
  • 導(dǎo)入ImportSelector 實(shí)現(xiàn)類(lèi)。一般用于加載配置文件中的類(lèi)
  • 導(dǎo)入ImportBeanDefinitionRegistrar實(shí)現(xiàn)類(lèi)。

1 導(dǎo)入Bean

定義一個(gè)user實(shí)體類(lèi):

package com.enable.entity;
public class User {
}

測(cè)試功能:

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.UserConfig;
import com.enable.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import java.util.Map;
@SpringBootApplication
@Import(User.class)
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        User user = context.getBean(User.class);
        System.out.println(user);
        Map<String,User> map = context.getBeansOfType(User.class);
        System.out.println(map);
    }
}

結(jié)果如下:

2 導(dǎo)入配置類(lèi)

新建實(shí)體類(lèi):

package com.enable.entity;
public class Role {
}
package com.enable.entity;
public class User {
}

測(cè)試功能:

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.UserConfig;
import com.enable.entity.Role;
import com.enable.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import java.util.Map;
@SpringBootApplication
@Import(UserConfig.class)
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        User user = context.getBean(User.class);
        System.out.println(user);
        Role role = context.getBean(Role.class);
        System.out.println(role);
    }
}

測(cè)試結(jié)果:

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

新建實(shí)體類(lèi):

package com.enable.entity;
public class Role {
}
package com.enable.entity;
public class User {
}

編寫(xiě)ImportSelector:

package com.enable.config;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.enable.entity.Role","com.enable.entity.User"};
    }
}

測(cè)試功能:

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.MyImportSelector;
import com.enable.config.UserConfig;
import com.enable.entity.Role;
import com.enable.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import java.util.Map;
@SpringBootApplication
@Import(MyImportSelector.class)
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        User user = context.getBean(User.class);
        System.out.println(user);
        Role role = context.getBean(Role.class);
        System.out.println(role);
    }
}

結(jié)果如下:

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

新建實(shí)體類(lèi):

package com.enable.entity;
public class User {
}

ImportBeanDefinitionRegistrar實(shí)現(xiàn)類(lèi)

package com.enable.config;
import com.enable.entity.User;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();
        registry.registerBeanDefinition("user",beanDefinition);
    }
}

測(cè)試結(jié)果:

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.MyImportBeanDefinitionRegistrar;
import com.enable.config.MyImportSelector;
import com.enable.config.UserConfig;
import com.enable.entity.Role;
import com.enable.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import java.util.Map;
@SpringBootApplication
@Import(MyImportBeanDefinitionRegistrar.class)
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        Object user1 = context.getBean("user");
        System.out.println(user1);
        User user = context.getBean(User.class);
        System.out.println(user);
    }
}

結(jié)果如下:

到此這篇關(guān)于SpringBoot自動(dòng)裝配之@Import深入講解的文章就介紹到這了,更多相關(guān)SpringBoot @Import內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Sentinel Dashboard限流規(guī)則保存方式

    Sentinel Dashboard限流規(guī)則保存方式

    這篇文章主要介紹了Sentinel Dashboard限流規(guī)則保存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java編程實(shí)現(xiàn)用hash方法切割文件

    Java編程實(shí)現(xiàn)用hash方法切割文件

    這篇文章主要介紹了Java編程實(shí)現(xiàn)用hash方法切割文件,簡(jiǎn)單介紹了hash的概念,然后分享了使用方法示例,具有一定借鑒價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • spring boot整合redis實(shí)現(xiàn)shiro的分布式session共享的方法

    spring boot整合redis實(shí)現(xiàn)shiro的分布式session共享的方法

    本篇文章主要介紹了spring boot整合redis實(shí)現(xiàn)shiro的分布式session共享的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • 基于RabbitMQ幾種Exchange 模式詳解

    基于RabbitMQ幾種Exchange 模式詳解

    下面小編就為大家?guī)?lái)一篇基于RabbitMQ幾種Exchange 模式詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • Java 關(guān)于遞歸的調(diào)用機(jī)制精細(xì)解讀

    Java 關(guān)于遞歸的調(diào)用機(jī)制精細(xì)解讀

    關(guān)于遞歸是什么,簡(jiǎn)單的說(shuō): 遞歸就是方法自己調(diào)用自己,每次調(diào)用時(shí) 傳入不同的變量.遞歸有助于編程者解決復(fù)雜的問(wèn)題,同時(shí)可以讓代碼變得簡(jiǎn)潔
    2021-10-10
  • springboot文件上傳保存路徑的問(wèn)題

    springboot文件上傳保存路徑的問(wèn)題

    這篇文章主要介紹了springboot文件上傳保存路徑的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • springboot項(xiàng)目不輸出nohup.out日志的解決

    springboot項(xiàng)目不輸出nohup.out日志的解決

    這篇文章主要介紹了springboot項(xiàng)目不輸出nohup.out日志的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot + minio實(shí)現(xiàn)分片上傳、秒傳、續(xù)傳功能

    SpringBoot + minio實(shí)現(xiàn)分片上傳、秒傳、續(xù)傳功能

    MinIO是一個(gè)基于Go實(shí)現(xiàn)的高性能、兼容S3協(xié)議的對(duì)象存儲(chǔ),使用MinIO構(gòu)建用于機(jī)器學(xué)習(xí),分析和應(yīng)用程序數(shù)據(jù)工作負(fù)載的高性能基礎(chǔ)架構(gòu),這篇文章主要介紹了SpringBoot + minio實(shí)現(xiàn)分片上傳、秒傳、續(xù)傳,需要的朋友可以參考下
    2023-06-06
  • Java實(shí)現(xiàn)從字符串中找出數(shù)字字符串的方法小結(jié)

    Java實(shí)現(xiàn)從字符串中找出數(shù)字字符串的方法小結(jié)

    這篇文章主要介紹了Java實(shí)現(xiàn)從字符串中找出數(shù)字字符串的方法,結(jié)合實(shí)例形式總結(jié)分析了Java查找數(shù)字字符串的常用技巧,需要的朋友可以參考下
    2016-03-03
  • 詳解Java中Checked Exception與Runtime Exception 的區(qū)別

    詳解Java中Checked Exception與Runtime Exception 的區(qū)別

    這篇文章主要介紹了詳解Java中Checked Exception與Runtime Exception 的區(qū)別的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下
    2017-08-08

最新評(píng)論