一、Springboot实现自动装配的 原理分析
@SpringBootApplication——>@SpringBootConfiguration——>@Configuration @ComponentScan @EnableAutoConfiguration——>@AutoConfigurationPackage @Import
二、ImportSelector和Registrar实现自定义装配
实现自定义装配(基于SPringIoc来管理bean的实例对象)
ImportSelector
Registrar
2.1、ImportSelector实现自定义装配
public class MaService {
public void show() {
System.out.println("========MaService========");
}
}
public class MaImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
MultiValueMap<String, Object> map = annotationMetadata.getAllAnnotationAttributes(EnableDefineService.class.getName());
return new String[]{MaService.class.getName()};
}
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({MaImportSelector.class})
public @interface EnableDefineService {
}
一、--------------------------------
@SpringBootApplication
public class Demo3Main {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Demo3Main.class);
MaService maService = (MaService) applicationContext.getBean(MaService.class);
maService.show();
}
}
结果
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.demo3.MaService' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:354)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1126)
at com.example.demo.demo3.Demo3Main.main(Demo3Main.java:12)
二、--------------------------------
@EnableDefineService
@SpringBootApplication
public class Demo3Main {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Demo3Main.class);
MaService maService = (MaService) applicationContext.getBean(MaService.class);
maService.show();
}
}
结果
========MaService========
2.2、Registrar实现自定义装配
public class MaService {
public void show() {
System.out.println("========MaService========");
}
}
public class MaRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
Class<MaService> maServiceClass = MaService.class;
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(maServiceClass);
String beanName = StringUtils.uncapitalize(maServiceClass.getSimpleName());
registry.registerBeanDefinition(beanName, rootBeanDefinition);
}
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(MaRegistrar.class)
public @interface EnableDefineService {
}
一、--------------------------------
@SpringBootApplication
public class Demo3Main {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Demo3Main.class);
MaService maService = (MaService) applicationContext.getBean(MaService.class);
maService.show();
}
}
结果
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.demo3.MaService' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:354)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1126)
at com.example.demo.demo3.Demo3Main.main(Demo3Main.java:12)
二、--------------------------------
@EnableDefineService
@SpringBootApplication
public class Demo3Main {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Demo3Main.class);
MaService maService = (MaService) applicationContext.getBean(MaService.class);
maService.show();
}
}
结果
========MaService========
三、springboot starter
1、spring-boot-starter的作用是引入依赖的jar包 以及 自己的自定义配置的jar包
2、spring-boot-autoconfiguration的作用是完成自动配置,一般是在 spring-boot-starter 的pom文件中依赖进去的
3.自定义我们的AutoConfiguration类
4.将我们自定义的Autoconfiguration类添加到项目META-INF/spring.factories文件中
public class BookService {
public String say() {
System.out.println("===BookService===say===");
return "BookService===say";
}
}
@Configuration
public class BookConfig {
@Bean
public BookService bookService() {
return new BookService();
}
}
spring.factories文件内加入以下内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.testcore.BookConfig
@SpringBootApplication
@EnableTransactionManagement
@Transactional
@ComponentScan(basePackages = "com.example.demo.*")
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
/*String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}*/
BookService bean = applicationContext.getBean(BookService.class);
System.out.println(bean.say());
}
}