面试(3——SpringBoot

一、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());
        
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值