Autoconfiguration详解

这篇具有很好参考价值的文章主要介绍了Autoconfiguration详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Autoconfiguration详解

一、 理解自动装配bean

1. 常用注解

  1. @AutoConfiguration(每个配置类都要加上)
    1. Class<?>[] after() default {};
    2. Class<?>[] before() default {};
    3. 以上两个配置可以控制加载顺序;
    4. 不需要再增加@Configuration注解;
  2. @AutoConfigureBefore and @AutoConfigureAfter
  3. @Configuration
  4. @Conditional(后面会详细讲到)
    1. @ConditianalOnClass
    2. @ConditionalOnMissingClass
    3. @ConditionalOnWebApplication:只在web应用中加载;
  5. @EnableConfigurationProperties:配置文件参数内容,参照类RedisProperties;
    1. @ConfigurationProperties(prefix = "spring.redis"),该注解展示了配置文件前缀;
  6. @DependsOn:列举一些前置的注入bean,以备用,用在类上需要有 @Component自动扫描的时候才能生效;
    1. 实际上控制了bean加载的顺序,优先加载指定的bean,然后加载当前bean;
    2. 销毁的时候,注解的bean优先与于依赖的bean销毁;

2. 定位自动装配的候选类

springboot 框架会自动扫描 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 进行引入,所以需要自动注入的Configuration文件都写在这个文件中。每个class一行。

这里本质上是一个自动版的@Import。

示例:

# comments
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration

如果需要引入特定的Component,使用@Import注解。

3. 条件注解

在所有自动装配类出现的地方,我们都因该时加上@Conditional注解,允许使用的开发人员覆盖自动装配的bean,当然他们也可以选择什么也不做,使用默认的配置。

Spring Boot 提供了一些条件注解,可以注解在@Configuration类或者@Bean方法上。

3.1 有关类的判断

对于@Configuration类来说,@ConditionalOnClass@ConditionalOnMissingClass代表了在指定类存在或者不存在的时候进行加载。因为实际上注解的元数据使用ASM技术进行解析,所以可以使用**value参数来指定特定的类的class对象(可以是多个),或者使用name参数**来指定特定的类名(可以是多个),两种方式所指向的类即使不存在也不影响正常执行。

@Bean方法返回值是条件注解的的目标之时,可能会因为JVM加载顺序的问题导致加载失败,上文提到的两个注解可以用在@Bean方法上。

3.2 有关bean的判断

@ConditionalOnBean@ConditionalOnMissingBean,代表在指定bean存在或者不存在时加载。value参数可以指定bean的class(多个),name可以指定bean的名称(多个)。search参数允许你限制ApplicationContext即应用上下文的搜索范围,可选当前上下文,继承上层,或者是全部(默认)。

@Bean方法上使用时,默认参数为当前方法返回类型。

在使用@Bean注解时,建议使用具体类型而不是父类型进行指代。

3.3 配置条件

@ConditionalOnProperty,指定配置项文件(例如dev,pro),prefix属性规定了配置前缀,name属性指定了应该被检查的参数。默认,所有存在且不等于false的参数都会被匹配到,你也可以使用havingValuematchIfMissing属性闯将更多的校验。

例子:@ConditionalOnProperty(name = "spring.redis.client-type", havingValue = "lettuce", matchIfMissing = true);

属性名 类型 解析
name String[] name() default {}; 配置项全称,如果有prefix,可以省略prefix中的前缀部分
prefix String prefix() default “”; 统一的配置项前缀
havingValue String havingValue() default “”; 配置项需要匹配的内容,如果没有指定,那么配置的值等于false时结果为false,否则结果都为true
matchIfMissing boolean matchIfMissing() default false; 配置项不存在时的配置,默认为false
3.4 源文件条件

@ConditionalOnResource,指定源文件存在时引入。

例如:@ConditionalOnResource(resources = {"classpath:test.log"});

3.5 web 应用条件

@ConditionalOnWebApplication@ConditionalOnNotWebApplication ,web应用或者不是web应用时启用,以下部分只要满足一个条件即为web 应用。

servlet-based web 应用特点:

  1. 使用 Spring WebApplicationContext;
  2. 定义了一个session作用域的bean;
  3. 有一个WebApplicationContext;

reactive web 应用特点:

  1. 使用了ReactiveWebApplicationContext;

  2. 有一个ConfigurableReactiveWebEnvironment;

ConditionalOnWarDeployment,仅限于使用war进行部署的场景,在嵌入式tomcat的场景里不会启用;

3.6 Spel表单式条件

ConditionalOnWarDeployment ,使用Spel表达式返回结果进行判断。

注意:在表达式中引用一个bean会导致这个bean非常早的被加载,此时还没有进行预加载(例如配置项的绑定),可能会导致不完成的加载。

二、自动注入配置基础

  1. @EnableConfigurationProperties(CommonRedisProperties.class) 注解configuration类;
  2. @ConfigurationProperties(prefix = "myserver")注解配置文件类,prefix标明配置文件的前缀;
  3. public RedisTemplate<String, Object> getRedisTemplate(CommonRedisProperties properties, RedisConnectionFactory redisConnectionFactory) ,加到需要使用的参数中即可;
  4. META-INF目录下添加additional-spring-configuration-metadata.json文件,格式如下
{
  "groups": [
    {
      "name": "server",
      "type": "com.huawei.workbenchcommon.redis.CommonRedisProperties",
      "sourceType": "com.huawei.workbenchcommon.redis.CommonRedisProperties"
    }
  ],
  "properties": [
    {
      "name": "myserver.database",
      "type": "java.lang.String",
      "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
    }
  ]
}

三、注释切面 @Metrics

1. 注解@Metrics

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Metrics {
    /**
     * 在方法成功执行后打点,记录方法的执行时间发送到指标系统,默认开启
     */
    boolean recordSuccessMetrics() default true;

    /**
     * 在方法成功失败后打点,记录方法的执行时间发送到指标系统,默认开启
     */
    boolean recordFailMetrics() default true;

    /**
     * 通过日志记录请求参数,默认开启
     */
    boolean logParameters() default true;

    /**
     * 通过日志记录方法返回值,默认开启
     */
    boolean logReturn() default true;

    /**
     * 出现异常后通过日志记录异常信息,默认开启
     */
    boolean logException() default true;

    /**
     * 出现异常后忽略异常返回默认值,默认关闭
     */
    boolean ignoreException() default false;

2. 切面MetricsAspect

@Aspect
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
public class MetricsAspect {
    /**
     * 让Spring帮我们注入ObjectMapper,以方便通过JSON序列化来记录方法入参和出参
     */
    @Resource
    private ObjectMapper objectMapper;

    /**
     * 实现一个返回Java基本类型默认值的工具。其实,你也可以逐一写很多if-else判断类型,然后手动设置其默认值。
     * 这里为了减少代码量用了一个小技巧,即通过初始化一个具有1个元素的数组,然后通过获取这个数组的值来获取基本类型默认值
     */
    private static final Map<Class<?>, Object> DEFAULT_VALUES = Stream
            .of(boolean.class, byte.class, char.class, double.class, float.class, int.class, long.class, short.class)
            .collect(toMap(clazz -> clazz, clazz -> Array.get(Array.newInstance(clazz, 1), 0)));

    public static <T> T getDefaultValue(Class<T> clazz) {
        //noinspection unchecked
        return (T) DEFAULT_VALUES.get(clazz);
    }

    /**
     * 标记了Metrics注解的方法进行匹配
     */
    @Pointcut("@annotation(com.common.config.metrics.annotation.Metrics)")
    public void withMetricsAnnotationMethod() {
    }

    /**
     * within指示器实现了匹配那些类型上标记了@RestController注解的方法
     * 注意这里使用了@,标识了对注解标注的目标进行切入
     */
    @Pointcut("within(@org.springframework.web.bind.annotation.RestController *)")
    public void controllerBean() {
    }

    @Pointcut("@within(com.common.config.metrics.annotation.Metrics)")
    public void withMetricsAnnotationClass() {
    }

    @Around("controllerBean() || withMetricsAnnotationMethod() || withMetricsAnnotationClass()")
    public Object metrics(ProceedingJoinPoint pjp) throws Throwable {
        // 通过连接点获取方法签名和方法上Metrics注解,并根据方法签名生成日志中要输出的方法定义描述
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Metrics metrics = signature.getMethod().getAnnotation(Metrics.class);

        String name = String.format("【%s】【%s】", signature.getDeclaringType().toString(), signature.toLongString());

        if (metrics == null) {
            @Metrics
            final class InnerClass {
            }
            metrics = InnerClass.class.getAnnotation(Metrics.class);
        }
        // 尝试从请求上下文获得请求URL
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            name += String.format("【%s】", request.getRequestURL().toString());
        }
        // 入参的日志输出
        if (metrics.logParameters()) {
            log.info(String.format("【入参日志】调用 %s 的参数是:【%s】", name, objectMapper.writeValueAsString(pjp.getArgs())));
        }
        // 连接点方法的执行,以及成功失败的打点,出现异常的时候记录日志
        Object returnValue;
        Instant start = Instant.now();
        try {
            returnValue = pjp.proceed();
            if (metrics.recordSuccessMetrics()) {
                // 在生产级代码中,应考虑使用类似Micrometer的指标框架,把打点信息记录到时间序列数据库中,实现通过图表来查看方法的调用次数和执行时间,
                log.info(String.format("【成功打点】调用 %s 成功,耗时:%d ms", name, Duration.between(start, Instant.now()).toMillis()));
            }
        } catch (Exception ex) {
            if (metrics.recordFailMetrics()) {
                log.info(String.format("【失败打点】调用 %s 失败,耗时:%d ms", name, Duration.between(start, Instant.now()).toMillis()));
            }
            if (metrics.logException()) {
                log.error(String.format("【异常日志】调用 %s 出现异常!", name), ex);
            }
            if (metrics.ignoreException()) {
                returnValue = getDefaultValue(signature.getReturnType());
            } else {
                throw ex;
            }
        }
        // 返回值输出
        if (metrics.logReturn()) {
            log.info(String.format("【出参日志】调用 %s 的返回是:【%s】", name, returnValue));
        }
        return returnValue;
    }

3. 自动注入AutoConfiguration

@AutoConfiguration
@Slf4j
@EnableConfigurationProperties(MetricsProperties.class)
@ConditionalOnProperty(prefix = "common.metrics", name = {"keep-alive"}, havingValue = "true", matchIfMissing = true)
public class AspectAutoConfiguration {

    public AspectAutoConfiguration() {
        log.info("AspectAutoConfiguration initialize.");
    }

    @Bean
    public MetricsAspect metricsAspect() {
        return new MetricsAspect();
    }
}

4. 配置文件MetricsProperties

@ConfigurationProperties(prefix = "common.metrics")
public class MetricsProperties {
    public Boolean getKeepAlive() {
        return keepAlive;
    }

    public void setKeepAlive(Boolean keepAlive) {
        this.keepAlive = keepAlive;
    }

    private Boolean keepAlive = true;

}

5. 其它配置

配置自动注入

配置resource.META-INF.spring.org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,增加AspectAutoConfiguration类路径。

配置文件提示

{
  "groups": [],
  "properties": [
    {
      "name": "common.metrics.keepAlive",
      "type": "java.lang.Boolean",
      "sourceType": "com.common.config.metrics.properties.MetricsProperties"
    }
  ]
}

四、自定义spring的profile限定注解

1. 注解@RunOnProfiles

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface RunOnProfiles {
    /**
     * Profile name array,eg,dev,pro.
     */
    String[] value() default {};

    /**
     * Skip the code of  the method of the class or method itself.
     */
    boolean skip() default true;

}

2. 切面RunOnProfilesAspect

@Aspect
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
@Component
public class RunOnProfilesAspect {
    @Autowired
    private ApplicationContext applicationContext;

    @Pointcut("@annotation(com.common.config.profiles.annotation.RunOnProfiles)")
    public void withAnnotationMethod() {
    }

    @Pointcut("@within(com.common.config.profiles.annotation.RunOnProfiles)")
    public void withAnnotationClass() {
    }

    @Around("withAnnotationMethod() || withAnnotationClass()")
    public Object runsOnAspect(ProceedingJoinPoint pjp) throws Throwable {
        var activeArray = applicationContext.getEnvironment().getActiveProfiles();
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        RunOnProfiles runOnProfiles = signature.getMethod().getAnnotation(RunOnProfiles.class);
        if (runOnProfiles == null) {
            return null;
        }
        var profilesArray = runOnProfiles.value();
        if (profilesArray == null || profilesArray.length == 0) {
            return pjp.proceed();
        }
        for (var profile : profilesArray) {
            for (var p : activeArray) {
                if (p.equals(profile)) {
                    return pjp.proceed();
                }
            }
        }
        return null;
    }
}

3. 自动注入AutoConfiguration

@AutoConfiguration
@Slf4j
public class RunsOnProfilesAutoConfiguration {

    public RunsOnProfilesAutoConfiguration() {
        log.info("RunsOnProfilesAutoConfiguration initialize.");
    }

    @Bean
    public RunOnProfilesAspect runsOnProfilesAspect() {
        return new RunOnProfilesAspect();
    }
}

4. 其它配置

配置自动注入

配置resource.META-INF.spring.org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,增加RunsOnProfilesAutoConfiguration类路径。

参考

[1] springboot doc configuration metadata文章来源地址https://www.toymoban.com/news/detail-723171.html

到了这里,关于Autoconfiguration详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • spring-boot-autoconfigure.jar/META-INF/spring.factories介绍

      spring-boot-autoconfigure.jar/META-INF/spring.factories 是Spring Boot自动配置的核心文件,它包含了各种自动配置类的注册信息。这个文件是Spring Boot根据应用程序的依赖关系和配置文件中的条件注解,自动加载和配置所需的Bean的依据。 在 spring.factories 文件中,每个自动配置类都对应一

    2024年02月06日
    浏览(35)
  • java.lang.IllegalStateException Error processing condition on org.springframework.boot.autoconfigur

    这两天搭建了一个spring cloud项目,简单写了个hellocontroller,结果项目启动失败了。 以下是控制台打印的异常: 以上的一大堆异常中,一开始我关注的是第一个异常。也就是, java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.context.PropertyPlaceholder

    2024年02月05日
    浏览(29)
  • Spring Boot各版本与Java版本的对应兼容关系,与构建工具(Maven、Gradle)版本的对应兼容关系,对servlet 容器的支持

    by:垃圾程序员 当前文章具有时效性,在当前springboot的版本下做的整合。之后大家视情况可以直接到Spring的官网查看 Spring | Home Level up your Java code and explore what Spring can do for you. https://spring.io/ 下面是Spring Boot各个版本的支持时间 下面是Spring Boot 推荐使用的各个版面,并标注出

    2024年02月10日
    浏览(34)
  • Spring 与 Servlet-2

    Spring的通知类型 Spring 通知类型按切面功能调用的不同时刻,可以分为提供了 5 种 Advice 类型 1、前置通知 Before advice:在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常) 2、后置通知 After returning advice:在某连接点正常完成后执行

    2024年02月12日
    浏览(29)
  • Spring整合JUnit和Servlet

    1、Spring注解配置 @Component 组件 @Repository @Service @Controller @Value @Autowired @Qualifier ~=== @Resource @Bean @ComponentScan @PropertySource @Configuration @Import @EnableTransactionManagement AnnotationConfigApplicationContext 2、Spring整合Junit单元测试 1、Junit 简介 Junit 是 Java 编程语言的单元测试框架,用于编写和运

    2024年02月04日
    浏览(45)
  • 【Servlet】Servlet 详解(使用+原理)

    Servlet(Server Applet 的缩写,全称 Java Servlet): 是 用 Java 编写的服务器端程序 。其主要功能在于交互式地浏览和修改数据, 生成动态 Web 内容 。狭义的 Servlet 是指 Java 语言实现的一个接口,广义的 Servlet 是指任何实现了这个 Servlet 接口的类,一般情况下,人们将 Servlet 理解

    2024年02月09日
    浏览(28)
  • Spring - Security 之 Servlet身份验证架构

    这个讨论是对之前文章的扩展,用于描述Spring Security在Servlet身份验证中使用的主要架构组件。 组件 描述 SecurityContextHolder SecurityContextHolder是Spring Security存储已认证用户详细信息的位置。 SecurityContext 从SecurityContextHolder获取,包含当前认证用户的认证信息。 Authentication 可以是

    2024年01月23日
    浏览(29)
  • JAVA开发(Spring框架详解)

    javaweb项目几乎已经离不开spring框架了,spring 是一个典型的分层架构框架,它包含一系列的功能并被分为多个功能模块,springboot对spring框架又做了一层封装,以至于很多人对原来的spring框架越来越不了解。         要谈Spring的历史,就要先谈J2EE。J2EE应用程序的广泛实现是

    2023年04月20日
    浏览(31)
  • [Spring5.3.2] Servlet[springmvc]的Servlet.init()引发异常, 解析类文件失败

    问题表现: 图中提到的问题: 例外情况 javax.servlet.ServletException: Servlet[springmvc]的Servlet.init()引发异常 根本原因 org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [D:apache-tomcat-9.0.69webappsch2_2WEB-INFclassescontrollerIndexController.class]; nested excep

    2024年02月02日
    浏览(26)
  • 什么是Servlet、JSP和Spring MVC?

    Servlet、JSP和Spring MVC都是Java Web开发中的重要技术。 Servlet是Java Servlet的简称,它是一个用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容。Servlet运行于支持Java的应用服务器中,可以响应客户端发送的请求,并

    2024年01月19日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包