我們在使用Spring框架中,特別是框架級的功能,經常看到有@Import匯入功能,

?
我就介紹下它能匯入什么,首先宣告下@Import是注解,匯入型別可分為三類:
1. 匯入配置 @Configuration,類似于spring早期版本2.5的import xml檔案一樣,
\n \n\n "}">\n \n <?xml version="1.0" encoding="UTF-8"?> <beans xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <import resource="cms-validator-service.xml"/> <import resource="cms-validator-dao.xml"/> </beans>
只是現在注解搶了風頭,但目的一樣,用于使用所有標有@configuration注解的配置,
下面我就寫個小例子,怎么建java專案就略了
先建java主包com.spring, 然后分別建子包
com.spring.service, com.spring.service.impl, com.spring.config, com.spring.test 1.1 建立服務介面
package com.spring.service;
/**
*
* @author dgm
* @describe "日志服務介面"
*/
public interface LogService {
void print(String message);
} 1.2 建立服務實作類,分三種情況,控制臺、檔案和資料庫mysql
package com.spring.service.impl;
import org.springframework.stereotype.Component;
import com.spring.service.LogService;
/**
* @author dgm
* @describe "日志到控制臺"
*/
@Component
public class StdOutLogServiceImpl implements LogService {
@Override
public void print(String message) {
System.out.println(message);
System.out.println("寫日志到控制臺!");
}
}
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.springframework.stereotype.Component;
import com.spring.service.LogService;
/**
*
* @author dgm
* @describe "日志到檔案"
*/
@Component
public class FileLogServiceImpl implements LogService {
private static final String FILE_NAME="d://LogService.txt";
@Override
public void print(String message) {
try {
File file = new File(FILE_NAME);
FileWriter fw = null;
// true:表示是追加的標志
fw = new FileWriter(file, true);
fw.write(message+"\n");
fw.close();
System.out.println(message);
System.out.println("寫日志入檔案!");
} catch (IOException e) {
}
}
}
/**
* @author dgm
* @describe "寫日志入mysql資料庫"
*/
@Component
public class MysqlLogServiceImpl implements LogService {
@Override
public void print(String message) {
System.out.println(message);
System.out.println("寫日志入資料庫");
}
}
1.3 寫配置類,三個服務實作類對應三個@Configuration
package com.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.spring.service.LogService;
import com.spring.service.impl.StdOutLogServiceImpl;
@Configuration
public class StdOutConfig {
@Bean(name="stdOutLogServiceImpl")
public LogService stdOutLogServiceImpl(){
return new StdOutLogServiceImpl();
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.spring.service.LogService;
import com.spring.service.impl.FileLogServiceImpl;
@Configuration
public class FileLogConfig {
@Bean(name="fileLogServiceImpl")
public LogService fileLogServiceImpl(){
return new FileLogServiceImpl();
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.spring.service.LogService;
import com.spring.service.impl.MysqlLogServiceImpl;
@Configuration
public class MysqlLogConfig {
@Bean(name="mysqlLogServiceImpl")
public LogService mysqlLogServiceImpl(){
return new MysqlLogServiceImpl();
}
}
然后@Import注解登場了
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({StdOutConfig.class, FileLogConfig.class, MysqlLogConfig.class})
public class LogParentConfig {
} 1.4 建立測驗類看效果
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.spring.config.LogParentConfig;
import com.spring.service.*;
/**
* @author dgm
* @describe "java configuration bean"
*/
public class LogConfigurationAppTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
LogParentConfig.class);
//控制臺
LogService obj = (LogService) context.getBean("stdOutLogServiceImpl");
System.out.println(obj);
obj.print("控制臺輸出");
//file
obj = (LogService) context.getBean("fileLogServiceImpl");
System.out.println(obj);
obj.print("檔案輸出");
//mysql
obj = (LogService) context.getBean("mysqlLogServiceImpl");
System.out.println(obj);
obj.print("資料庫mysql");
context.close();
}
} 輸出效果
?
2. 匯入實作ImportSelector介面或子介面DeferredImportSelector的類
@Import annotation can also be configured with an ImportSelector implementation to select @Configuration classes programmatically, based on some selection criteria.
下面我也演示下,這個很重要,框架里和spring擴展開發用的多,先建立備用子包com.spring.bean和com.spring.importSelector,然后建立組態檔目錄conf
2.1 實作了ImportSelector
2.1.1 建立輔助類ApplicationProperties.java和外置組態檔myapp.properties
package com.spring.bean;
public class ApplicationProperties {
private String connectionUrl;
private String connectionName;
public String getConnectionUrl() {
return connectionUrl;
}
public void setConnectionUrl(String connectionUrl) {
this.connectionUrl = connectionUrl;
}
public String getConnectionName() {
return connectionName;
}
public void setConnectionName(String connectionName) {
this.connectionName = connectionName;
}
@Override
public String toString() {
return "ApplicationProperties [connectionUrl=" + connectionUrl
+ ", connectionName=" + connectionName + "]";
}
} 然后在conf目錄下建立組態檔myapp.properties,內容如下:
app.url=https://github.com/dongguangming
app.name=dongguangming 2.1.2 建立@Configuration配置類
@Configuration
@PropertySource("classpath:conf/myapp.properties")
public class AppConfig {
@Autowired
ConfigurableEnvironment environment;
@Bean
ApplicationProperties appProperties() {
ApplicationProperties bean = new ApplicationProperties();
bean.setConnectionUrl(environment.getProperty("app.url"));
bean.setConnectionName(environment.getProperty("app.name"));
return bean;
}
} 2.1.3 建立實作了ImportSelector介面的匯入類,回傳串列里的值是有標志@Configuration
public class LogImportSelector implements ImportSelector{
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.spring.config.AppConfig","com.spring.config.LogParentConfig"};
}
} 2.1.4 建立有@import功能的配置類,匯入2.1.3的實作類
package com.spring.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.spring.importSelector.LogImportSelector;
@Configuration
@Import(LogImportSelector.class)
public class LogImportSelectorConfig {
} 2.1.5 撰寫測驗類
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.spring.bean.ApplicationProperties;
import com.spring.config.LogImportSelectorConfig;
import com.spring.service.*;
/**
* @author dgm
* @describe "java configuration bean"
*/
public class LogImportSelectorConfigurationAppTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
LogImportSelectorConfig.class);
// 控制臺
LogService obj = (LogService) context.getBean("stdOutLogServiceImpl");
System.out.println(obj);
obj.print("控制臺輸出");
// file
obj = (LogService) context.getBean("fileLogServiceImpl");
System.out.println(obj);
obj.print("檔案輸出");
// mysql
obj = (LogService) context.getBean("mysqlLogServiceImpl");
System.out.println(obj);
obj.print("資料庫mysql");
//
ApplicationProperties ap = context.getBean(ApplicationProperties.class);
System.out.println(ap);
context.close();
}
} 輸出效果:
?
效果不錯,也能完成bean的注冊
還有一種基于注解的變體,我也示例下,先建個子包com.spring.annotation
建立自定義注解:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(LogImportSelector.class)
/**
* @author dgm
* @describe "自定義Enable功能"
*/
public @interface EnableLogService {
//默認日志輸出到控制臺
String logType() default "stdout";
@AliasFor("value")
String[] basePackages() default {};
@AliasFor("basePackages")
String[] value() default {};
} 然后修改匯入選擇器實作類,根據啟用日志功能時傳的引數絕對加載哪個bean
AnnotationAttributes attributes = AnnotationAttributes
.fromMap(importingClassMetadata.getAnnotationAttributes(
EnableLogService.class.getName(), false));
System.out.println(attributes);
//根據日志型別確定加載bean
String logType = attributes.getString("logType");
if (logType.equalsIgnoreCase("StdOut")) {
return new String[] { "com.spring.config.AppConfig",
"com.spring.config.StdOutConfig" };
} else if (logType.equalsIgnoreCase("File")) {
return new String[] { "com.spring.config.AppConfig",
"com.spring.config.FileLogConfig" };
} else if (logType.equalsIgnoreCase("Mysql")) {
return new String[] { "com.spring.config.AppConfig",
"com.spring.config.MysqlLogConfig" };
} else {
return new String[] { "com.spring.config.AppConfig",
"com.spring.config.LogParentConfig" };
} 修改配置類,追加自定義注解@EnableLogService,并設定引數為file(可選stdout,file,mysql)
@Configuration
//@Import(LogImportSelector.class)
@EnableLogService(logType="file")
public class LogImportSelectorConfig {
} 修改測驗類,此時不再是三種日志實作的bean都加載,按配置引數加載
LogService obj = (LogService) context.getBean("fileLogServiceImpl");
System.out.println(obj);
obj.print("檔案輸出"); 
?
就因為配置了@EnableLogService(logType="file"),只加載了一個日志實作bean
2.2 實作了 DeferredImportSelector
public interface DeferredImportSelector extends ImportSelector {
} 可是看出它是2.1的子介面
The configuration class directly registered with the application context given preference over imported one. That means a bean of type T, configured in the main configuration will be used instead of a bean of the same type T from imported configuration. That applies to ImportSelector as well. On the other hand, DeferredImportSelector applies after all other configuration beans have been processed.
我們可以比較下實作兩種介面的區別
在主選擇器的配置類LogImportSelectorConfig.java中增加
@Bean
LogBean logBean() {
return new LogBean();
}
@Bean(name = "fileLogServiceImpl")
public LogService fileLogServiceImpl() {
return new FileLogServiceImpl(" 來自LogImportSelectorConfig ");
}
在檔案配置類FileLogConfig.java中修改為
@Bean(name="fileLogServiceImpl")
public LogService fileLogServiceImpl(){
return new FileLogServiceImpl("來自 FileLogConfig");
} 選擇器實作類還是
public class LogImportSelector implements ImportSelector {,,,} 執行測驗代碼
LogBean bean = context.getBean(LogBean.class);
bean.printMessage(); 
?
此時修改選擇器實作的介面改為DeferredImportSelector,其它不改
public class LogImportSelector implements DeferredImportSelector {,,,}
再次執行測驗

?
3 匯入實作了ImportBeanDefinitionRegistrar介面的類
可以先瞄下介面的如何定義和定義了什么
public interface ImportBeanDefinitionRegistrar {
public void registerBeanDefinitions(
AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry);
} This Interface is to be implemented by types that register additional bean definitions when processing @Configuration classes.
具體可參考還記得我以前寫的博文Spring Bean注冊的幾種方式https://blog.csdn.net/dong19891210/article/details/105798650嗎,詳細看第5.2小節,這里就不再重復啰嗦寫了,
想了幾天還是花點時間寫上,畢竟放到個人電腦上不安全,我就一步一步開始完善
3.1 建立自定義組件注解標識和掃描包注解
@Documented
@Indexed
//@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
/**
* @author dgm
* @describe "自定義組件注解標識"
* @date 2020年5月27日
*/
public @interface CustomComponent {
String value() default "";
} 然后再建掃描包注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
@Import(CustomImportBeanDefinitionRegistrar.class)
public @interface CustomComponentScan {
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
} 重點看@Import(CustomImportBeanDefinitionRegistrar.class)
3.2 定義實作了ImportBeanDefinitionRegistrar介面的類
public class CustomImportBeanDefinitionRegistrar implements
ImportBeanDefinitionRegistrar, ResourceLoaderAware {
private ResourceLoader resourceLoader;
@Autowired
ConfigurableEnvironment environment;
@Autowired
ApplicationProperties applicationProperties;
@Override
public void registerBeanDefinitions(
AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
AnnotationAttributes annotationAttributes = AnnotationAttributes
.fromMap(importingClassMetadata.getAnnotationAttributes(
CustomComponentScan.class.getName(), false));
//System.out.println(annotationAttributes);
String[] basePackages = (String[]) annotationAttributes
.get("basePackages");
System.err.println("要掃描的包是:" + Arrays.asList(basePackages));
if (basePackages == null || basePackages.length == 0) {
String basePackage = null;
try {
basePackage = Class
.forName(importingClassMetadata.getClassName())
.getPackage().getName();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
basePackages = new String[] { basePackage };
}
MapperBeanDefinitionScanner scanner = new MapperBeanDefinitionScanner(
registry, false);
scanner.setResourceLoader(resourceLoader);
// scanner.registerFilters();
scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class));
scanner.doScan(basePackages);
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public class MapperBeanDefinitionScanner extends
ClassPathBeanDefinitionScanner {
public MapperBeanDefinitionScanner(BeanDefinitionRegistry registry,
boolean useDefaultFilters) {
super(registry, useDefaultFilters);
}
protected void registerFilters() {
addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class));
}
@Override
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
return super.doScan(basePackages);
}
}
} 3.3 定義配置類,預設組件掃描的包是com.spring.mapper
@Configuration
@CustomComponentScan(basePackages= { "com.spring.mapper" })
public class CustomComponentConfiguration {
} 3.4 建立自定義組件掃描的包com.spring.mapper,略
然后在自定義包下建立自定義組件,注意類上有自定義組件標識@CustomComponent
@CustomComponent
public class CustomDataMapper {
public List printData() throws SQLException {
List<String> list = new ArrayList<>();
list.add("dongguangming");
list.add("張三");
list.add("李四");
return list;
}
} 3.5 撰寫測驗
/**
* @author dgm
* @describe "測驗ImportBeanDefinitionRegistrar,自定義組件"
*/
public class CustomImportBeanDefinitionRegistrarTest {
public static void main(String[] args) throws SQLException {
//引入兩個配置類MysqlDatabaseConfiguration和CustomComponentConfiguration(由于MysqlDatabaseConfiguration關聯代碼多,所以沒有在文章里寫,測驗時可去掉)
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
MysqlDatabaseConfiguration.class, CustomComponentConfiguration.class);
//就是我剛才自定義的組件類
CustomDataMapper data = https://www.cnblogs.com/dongguangming/p/applicationContext.getBean(CustomDataMapper);
System.out.println(data);
System.out.println(data.printData());
//從資料庫里查詢用戶串列
UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
System.out.println(userMapper);
userMapper.queryMysql();
applicationContext.close();
}
} 注意:引入兩個配置類MysqlDatabaseConfiguration和CustomComponentConfiguration(由于MysqlDatabaseConfiguration關聯代碼多,所以沒有在文章里寫,測驗時可去掉不引入MysqlDatabaseConfiguration.class)
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
MysqlDatabaseConfiguration.class, CustomComponentConfiguration.class);
輸出效果:
其實你搞懂了Bean,spring本身、及衍生的第三方擴展, 99.99%的問題都不再是問題了!!!
小結:一圖

?
務必掌握好2和3,寫擴展很有用,甚至spring本身都在大量使用,如下



spring圍繞著bean運轉的,注冊的幾種方式,每種注冊方式的條件性選擇
最后請慢慢學會忘記xml格式的組態檔,現在或往后都是注解式了,雖然xml配置并不影響功能!

?
附部分注解圖一張:

?
參考:
0. @Import Annotation in Spring Framework
https://javabeat.net/use-import-importing-javaconfig-files-spring-projects/
1. Spring向容器注冊Bean的高級應用 https://cloud.tencent.com/developer/article/1497795
2. how spring import annotation parse(要翻墻)??????? https://laptrinhx.com/spring-import-annotation-source-parsing-3074679655/
注意我說的墻不是下面這樣的墻
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/181221.html
標籤:Java
上一篇:【JavaSE】反射的總結
