当前位置:   article > 正文

SpringBoot自动配置原理_neither pathpatterns nor string patterns condition

neither pathpatterns nor string patterns condition

1.Condition自动装配 

这边先抛出一个问题

1.1 根据条件加载bean

  下面是根据字节码文件加载,这个代码的作用是如果加载了某个字节码文件,那么就会继续加载其他类。不加载的话也就不会加载和这个相关的方法,非常实用

1.1 自定义自动装配

 上面这个功能在springboot 里面大量使用,下面举几个例子。可以更好的理解Springboot 原理。

下面就是Springboot 自动装配使用到的注解,当然这边提到的还很少,有兴趣的可以自己查查这些注解的功能效用!

下面是Springboot自动装配的redis类

2.切换内置服务器

运用到上面的知识,切换服务器就变的非常的简单易懂!

3.@Enable*系列注解

3.1 @EnableAutoConfiguration

这个底层实际上是@import, 下面就是这个注解的源码

Springboot加载配置类的时候只会加载启动类本包(同级)及其子包的配置类,那么Spring加载的时候也会加载@import 引入的Class类,并且也可以用@CommentScan 包扫描来加载指定路径下的类

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. @AutoConfigurationPackage
  6. @Import(AutoConfigurationImportSelector.class)
  7. public @interface EnableAutoConfiguration {
  8. /**
  9. * Environment property that can be used to override when auto-configuration is
  10. * enabled.
  11. */
  12. String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
  13. /**
  14. * Exclude specific auto-configuration classes such that they will never be applied.
  15. * @return the classes to exclude
  16. */
  17. Class<?>[] exclude() default {};
  18. /**
  19. * Exclude specific auto-configuration class names such that they will never be
  20. * applied.
  21. * @return the class names to exclude
  22. * @since 1.3.0
  23. */
  24. String[] excludeName() default {};
  25. }

上面图片就是所有自动配置的类,但是不一定都会加载,因为加了判断,情况满足参会加载到容器里面。 

4.@Import注解的四种用法

4.1 导入类

  1. @SpringBootApplication
  2. @Import(User.class)
  3. public class SpringInitApplication {
  4. public static void main(String[] args) {
  5. ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringInitApplication.class, args);
  6. User bean = applicationContext.getBean(User.class);
  7. System.out.println(bean);
  8. }
  9. }

4.2 导入配置类

首先是配置类的代码

  1. package com.zx.domain;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. public class UserConfig {
  6. @Bean
  7. public User user(){
  8. return new User();
  9. }
  10. }

下面是启动类的导入

  1. @SpringBootApplication
  2. //@Import(User.class)
  3. @Import(UserConfig.class)
  4. public class SpringInitApplication {
  5. public static void main(String[] args) {
  6. ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringInitApplication.class, args);
  7. User bean = applicationContext.getBean(User.class);
  8. System.out.println(bean);
  9. }
  10. }

4.3 导入ImportSelector

首先实现ImportSelector类

  1. package com.zx.domain;
  2. import org.springframework.context.annotation.ImportSelector;
  3. import org.springframework.core.type.AnnotationMetadata;
  4. import java.util.function.Predicate;
  5. public class MyImportSelector implements ImportSelector {
  6. @Override
  7. public String[] selectImports(AnnotationMetadata importingClassMetadata) {
  8. return new String[]{
  9. //这边是你要导入的对象
  10. "com.zx.domain.User"
  11. };
  12. }
  13. @Override
  14. public Predicate<String> getExclusionFilter() {
  15. return ImportSelector.super.getExclusionFilter();
  16. }
  17. }

导入的方式还是上面老样子

@Import(MyImportSelector.class)

4.4 ImportBeanDefinitionRegistrar 接口

使用方法

  1. public class MyImportRegister implements ImportBeanDefinitionRegistrar {
  2. @Override
  3. public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
  4. ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry);
  5. registry.registerBeanDefinition("cs",BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition());
  6. }
  7. }

启动类

  1. @SpringBootApplication
  2. //@Import(User.class)
  3. //@Import(UserConfig.class)
  4. @Import(MyImportRegister.class)
  5. public class SpringInitApplication {
  6. public static void main(String[] args) {
  7. ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringInitApplication.class, args);
  8. User bean = (User) applicationContext.getBean("cs");
  9. System.out.println(bean);
  10. }
  11. }

5. 自定义starter

5.1自动配置类

  1. @Configuration
  2. @EnableConfigurationProperties(UserProperties.class)
  3. public class AutoZx {
  4. @Bean
  5. public UserInfo getUserInfo(UserProperties userProperties){
  6. System.out.println("加载默认用户!");
  7. return new UserInfo(userProperties.getName());
  8. }
  9. }

5.2 配置映射

  1. //这边遇到一个问题,无法映射到项目内部的配置文件,只能映射项目外部的配置文件,暂不清楚什么原因
  2. @ConfigurationProperties(prefix = "zx")
  3. public class UserProperties {
  4. private String name;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. }

5.3 对象类

  1. public class UserInfo {
  2. private String name ;
  3. public UserInfo(String name) {
  4. this.name = name;
  5. }
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. }

 5.4 核心扫描配置文件

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/796465
推荐阅读
相关标签
  

闽ICP备14008679号