一、用法
- 1.指定普通类
- 2.指定配置类
- 3. 指定实现了ImportSelector接口的类,通过ImportSelector可以一次性注册多个,返回一个string[]数组中,数组中每一个值是类的完整路径
- 4.通过实现了ImportBeanDefinitionRegistrar接口的类,可以一次性注册多个,通过BeanDefinitionRegistry来动态注册BeanDefinition
- 5.通过实现了DeferredImportSelector(ImportSelector的子类)可以一次性注册多个,返回一个string[]每一个值是完整类路径,与ImpoertSelector的区别:顺序靠后,SpringBoot用的一种方式
二、代码示例
2.1 指定普通类
package com.learning.beanimport;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @Author wangyouhui
* @Description 配置类
**/
@Configuration
@ComponentScan
@Import(UserService.class)
public class ImportConfig {
}
package com.learning.beanimport;
/**
* @Author wangyouhui
* @Description 业务类
**/
public class UserService {
public void print(String hello) {
System.out.println(hello);
}
}
package com.learning.beanimport;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author wangyouhui
* @Description 应用类
**/
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(ImportConfig.class);
UserService userService = (UserService)annotationConfigApplicationContext.getBean("userService");
userService.print("hello");
}
}
2.2 指定配置类
package com.learning.beanimport.configclass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @Author wangyouhui
* @Description 配置类
**/
@Configuration
@ComponentScan
public class Config {
@Bean
public UserService userService(){
return new UserService();
}
}
package com.learning.beanimport.configclass;
import org.springframework.context.annotation.Import;
/**
* @Author wangyouhui
* @Description 配置类
**/
@Import(Config.class)
public class ImportConfig {
}
package com.learning.beanimport.configclass;
/**
* @Author wangyouhui
* @Description 业务类
**/
public class UserService {
public void print(String hello) {
System.out.println(hello);
}
}
package com.learning.beanimport.configclass;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author wangyouhui
* @Description 应用类
**/
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(ImportConfig.class);
UserService userService = (UserService)annotationConfigApplicationContext.getBean("userService");
userService.print("hello");
}
}
2.3 指定实现了ImportSelector接口的类
package com.learning.beanimport.importselector;
/**
* @Author wangyouhui
* @Description 业务类
**/
public class UserService {
public void print(String hello) {
System.out.println(hello);
}
}
package com.learning.beanimport.importselector;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @Author wangyouhui
* @Description 配置类
**/
@ComponentScan
@Configuration
@Import(ImportSelector.class)
public class ImportConfig {
}
package com.learning.beanimport.importselector;
import org.springframework.core.type.AnnotationMetadata;
/**
* @Author wangyouhui
* @Description 实现ImportSelector接口的类
**/
public class ImportSelector implements org.springframework.context.annotation.ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
// return new String[]{"com.learning.beanimport.importselector.UserService"};
return new String[]{UserService.class.getName()};
}
}
package com.learning.beanimport.importselector;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author wangyouhui
* @Description 应用类
**/
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(ImportConfig.class);
UserService userService = annotationConfigApplicationContext.getBean(UserService.class);
userService.print("hello");
}
}
2.4 指定实现了ImportBeanDefinitionRegistrar接口的类
package com.learning.beanimport.importbeandefinitionregister;
/**
* @Author wangyouhui
* @Description 业务类
**/
public class UserService {
public void print(String hello) {
System.out.println(hello);
}
}
package com.learning.beanimport.importbeandefinitionregister;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.core.type.AnnotationMetadata;
/**
* @Author wangyouhui
* @Description bean定义注册器
**/
public class ImportBeanDefinitionRegistrar implements org.springframework.context.annotation.ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
// 创建BeanDefinition
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(UserService.class);
// 注册BeanDefinition
registry.registerBeanDefinition("userService", beanDefinition);
}
}
package com.learning.beanimport.importbeandefinitionregister;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @Author wangyouhui
* @Description 配置类
**/
@Configuration
@ComponentScan
@Import(ImportBeanDefinitionRegistrar.class)
public class ImportConfig {
}
package com.learning.beanimport.importbeandefinitionregister;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author wangyouhui
* @Description 应用类
**/
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(ImportConfig.class);
UserService userService = (UserService)annotationConfigApplicationContext.getBean("userService");
userService.print("hello");
}
}