栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot自定义配置类及自动配置基本原理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot自定义配置类及自动配置基本原理

SpringBoot自定义配置类及自动配置基本原理

  • @SpringBootApplication注解

  • @EnableAutoConfiguration注解

  • @import加载bean的三种方式

  • 配置类常用注解

  • 手写一个springboot自动配置类


@SpringBootApplication注解
   @Target({ElementType.TYPE}) //注解适用范围,如常用的TYPE(类和接口)、FIELD(字段)、METHOD(方法)、PARAMETER(参数)、ANNOTATION_TYPE(注解)等
   @Retention(RetentionPolicy.RUNTIME) //注解的生命周期,如SOURCE(只存在编译期,编译结束后无该注释信息)、CLASS(默认,存在.class文件中)、RUNTIME(存在类文件中,且允许JVM读入)
   @documented //是否包含在用户文档中
   @Inherited //注解继承与哪个注解类
   @SpringBootConfiguration // 继承了Configuration注解,表示当前是一个配置类
   @EnableAutoConfiguration // 开启自动配置功能,借助@import加载配置类
   @ComponentScan( //扫描路径设置
       excludeFilters = {@Filter(
       type = FilterType.CUSTOM,
       classes = {TypeExcludeFilter.class}
   ), @Filter(
       type = FilterType.CUSTOM,
       classes = {AutoConfigurationExcludeFilter.class}
   )}
   )
   public @interface SpringBootApplication {
       ..
   }

@EnableAutoConfiguration注解

该注解主要由@import({AutoConfigurationimportSelector.class})和AutoConfigurationPackage两个注解构成

  • 第一个注解主要是将配置类自动加载到spring容器中,AutoConfigurationimportSelector实现了importSelector接口,自动调用selectimports方法,调用其内部方法getCandidateConfigurations读取classpath位置在meta-INF/spring.factories中的文件内容,找到EnableAutoConfiguration对应类名的类加入到容器,另外在加载对应配置类时(通常配置类有@Configuration注解和其它条件注解)通过Conditional条件注解来加载满足条件类
  • AutoConfigurationPackage注解的作用是将添加该注解的类所在的package作为 自动配置package进行管理,如SpringBoot应用启动时默认会将启动类所在的package作为自动配置的package

@import加载bean的三种方式
  • 直接导入A类如@import(A.class)
  • 实现importSelector接口,根据接口返回的类全限定名导入,如@import(SelfimportSelector.class),该SelfimportSelector.class实现了importSelector接口
  • 同上一样实现importBeanDefinitionRegistrar 接口,但接口中通过注册beanDefinition进行导入

配置类常用注解
  • @Configuration 表示一个配置类,形如xml给容器添加组件
  • @EnableConfigurationProperties(xxxProperties.class) 使指定的xxxProperties类生效,并加入到容器;另外@ConfigurationProperties可以将配置文件中对应的值和xxxProperties类中的属性进行绑定
  • @ConditionalOnWebApplication 判断当前应用是否为web应用,若是配置类生效
  • @ConditionalOnClass(xxx.class) 当前项目中是否有xxx.class,若有才生效
  • @ConditionalOnMissBean(xxx.class) 容器中没有这个xxx的bean才生效
  • @ConditionalOnResource(resources=“classpath:jdbc.properties”) 类路径下指定配置文件才生效
  • @ConditionalOnProperty(perfix=“xxx”,value=“enable”,matchIfMissing=true) 判断配置文件中是否存在某个属性值xxx.enabled;如果不存在,判断也是成立的,即不配置也是默认生效,即指定属性是否有指定值

手写一个springboot自动配置类
  • 导入项目的相关依赖

    org.projectlombok
    lombok



    org.springframework.boot
    spring-boot-configuration-processor
    true

  • 配置maven插件,将maven插件修改为maven-compiler-plugin,否则可能无法引入
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.0
                
                    1.8
                    1.8
                    UTF-8
                
            
        
    
  • 添加一个properties类,来绑定配置文件
@Data
@ConfigurationProperties(prefix = "com.wsp.user")
public class WelcomeServiceProperties {
    private String name = "wsp";
}
  • 添加一个服务实现类;
@Data
public class WelcomeService {

    private String name;
    
    public String sayUserName(){
        return "Welcome " + name + " Enter !";
    }
    
}
  • 添加一个自动配置类;
@Configuration
@EnableConfigurationProperties(WelcomeServiceProperties.class)
@ConditionalOnClass(WelcomeService.class)
@ConditionalOnProperty(prefix = "com.wsp.user",value = "enabled",matchIfMissing = true)
public class WelcomeServiceAutoConfiguration {

    @Autowired
    private WelcomeServiceProperties welcomeServiceProperties;

    @Bean
    @ConditionalOnMissingBean(WelcomeService.class)//当容器中无该Bean会进行自动配置
    public WelcomeService welcomeService(){
        WelcomeService welcomeService = new WelcomeService();
        welcomeService.setName(welcomeServiceProperties.getName());
        return welcomeService;
    }
}
  • 在meta-INF下的spring.factories文件中配置以下内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.wsp.demo.service.impl.WelcomeServiceAutoConfiguration
  • 本地打包maven install 并引入坐标,最后测试
 
     com.wsp
     auto-config-demo
     1.0-SNAPSHOT
 
@SpringBootTest
class TrainApplicationTests {
    
    @Autowired
    WelcomeService welcomeService;
    
    @Test
    void contextLoads() {
        System.out.println(welcomeService.sayUserName());
    }
    
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571518.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号