SpringBoot模块自动装配
方式一 Import 导入模块类:
1.模块定义
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloWorldConfiguration {
@Bean
public String helloWorld(){
return "hello world";
}
}
2.激活注解
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloWorldConfiguration.class)
public @interface EnableHelloWorld {
}
3.方法测试
@EnableHelloWorld
public class EnableModuleDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(EnableModuleDemo.class);
context.refresh();
String helloWorld = context.getBean("helloWorld",String.class);
System.out.println(helloWorld);
context.close();
}
}
方式二 Import 导入ImportSelector接口实现类:
1.模块定义
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloWorldConfiguration {
@Bean
public String helloWorld(){
return "hello world";
}
}
2.ImportSelector接口实现
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class HelloWorldImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"cn.modular.HelloWorldConfiguration"};
}
}
3.激活注解
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloWorldImportSelector.class)
public @interface EnableHelloWorld {
}
4.方法测试
@EnableHelloWorld
public class EnableModuleDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(EnableModuleDemo.class);
context.refresh();
String helloWorld = context.getBean("helloWorld",String.class);
System.out.println(helloWorld);
context.close();
}
}
方式三 Import 导入ImportBeanDefinitionRegistrar接口实现类:
1.模块定义
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloWorldConfiguration {
@Bean
public String helloWorld(){
return "hello world";
}
}
2.ImportBeanDefinitionRegistrar接口实现
public class HelloWorldImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(HelloWorldConfiguration.class);
BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition,registry);
}
}
3.激活注解
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloWorldImportBeanDefinitionRegistrar.class)
public @interface EnableHelloWorld {
}
4.方法测试
@EnableHelloWorld
public class EnableModuleDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(EnableModuleDemo.class);
context.refresh();
String helloWorld = context.getBean("helloWorld",String.class);
System.out.println(helloWorld);
context.close();
}
}