spring自动装配机制

starter 命名规则:
springboot项目有很多专一功能的starter组件,命名都是spring-boot-starter-xx,如spring-boot-starter-logging,spring-boot-starter-web,

如果是第三方的starter命名一般是:xx-springboot-starter 如:mongodb-plus-spring-boot-starter,mybatis-spring-boot-starter;

starter的原理:

springboot的自动装配机制

image

属性文件自动装配

image
  • 需求,我们准备弄个日志相关的starter,当别人依赖我们的jar包时,在需要打印日志的方法上贴上对应的注解即可,日志打印的前置通知和后置通知内容可以在application.yml或者application.properties中配置

    思路: 我们需要定义一个注解:这样别人在需要打日志的地方贴上该注解即可:

package org.example.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyLog {
}

接着,我们要让注解生效,所以需要一个切面类:

package org.example.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;

import java.util.Arrays;

@org.aspectj.lang.annotation.Aspect
public class MyLogAspect {

    private MyLogProperties myLogProperties;

    public MyLogAspect(MyLogProperties myLogProperties) {
        this.myLogProperties = myLogProperties;
    }

    @Pointcut("@annotation(org.example.annotation.MyLog)")
    public void myLogPointCut() {}

    @Around("myLogPointCut()")
    public Object invoke(ProceedingJoinPoint joinPoint){
        System.out.println(myLogProperties.getPerfix()+"---"+ Arrays.toString(joinPoint.getArgs()));
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println(myLogProperties.getSubfix()+"---"+ Arrays.toString(joinPoint.getArgs()));
        return proceed;

    }

}

切面类中有个配置文件类:

package org.example.config;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;

@ConfigurationProperties(prefix = "mylog")
public class MyLogProperties {

    private String perfix;
    private String subfix;

    public String getPerfix() {
        if (StringUtils.isEmpty(perfix)) {
            return "默认前缀";
        }
        return perfix;
    }

    public void setPerfix(String perfix) {
        this.perfix = perfix;
    }

    public String getSubfix() {
        if (StringUtils.isEmpty(subfix)) {
            return "默认后缀";
        }
        return subfix;
    }

    public void setSubfix(String subfix) {
        this.subfix = subfix;
    }
}

要让上面类注入spring容器,需要一个配置类:

package org.example.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(MyLogProperties.class)
@ConditionalOnBean(MyLogConfiguration.class)
public class MyLogAutoConfiguration {
    @Bean
    public MyLogAspect myLogAspect(MyLogProperties myLogProperties) {
        return new MyLogAspect(myLogProperties);
    }

}

到此为止,只要MyLogAutoConfiguration 注入spring容器了,那么他里面的bean也会被注入,而怎么样使得MyLogAutoConfiguration 注入spring呢,那就要用到springboot的自动装配机制:

在resources下创建一个META-INF文件夹,然后在创建一个文件:spring.factories文件加入内容:key是固定的org.springframework.boot.autoconfigure.EnableAutoConfiguration,value可以有多个

org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.example.config.MyLogAutoConfiguration

依赖: 平时我们在写application.yml时会有提示,那么我也想让我的日志配置也会生效,也就是当我输入mylog时,会提示mylog.prefix 或者mylog.subfix,此时需要下面的配置:

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

因为我们项目用到springboot和aop,所以需要:


            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-aop
        

注意:maven打包时,不能用spring-boot-maven-plugin,我用它打包没报错,给其他服务引用对应的jar时,启动报错了。需要换成:


        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    

至此,项目创建完毕: 现在看下整体情况:

image.png

项目打包进行测试:

image

新建web项目引入我们的日志依赖:


      org.example
      start-demo
      2.0-SNAPSHOT
    

测试项目提供一个controller,对应方法贴上我们的日志注解:

package org.example;

import org.example.annotation.EnableMyLog;
import org.example.annotation.MyLog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@RestController

public class App {
    @GetMapping("/test")
    @MyLog
    public String test() {
        return "1";
    }

    public static void main( String[] args ) {
        SpringApplication.run(App.class, args);
    }
}

测试项目的整体情况:


image.png

此时我们没有在yml中配置日志前缀,启动测试项目测试:

浏览器输入:http://localhost/test

控制台输出:


image.png

我们在yml配置文件输入前后缀:


image.png

可见,有提示,跟我们配置其他组件的属性一样:


至此,我们实现了一个完整的springboot starter,在springboot项目中,很多组件的底层原理都是这样实现的,通过这种实现,可以做底层架构,然后给其他服务使用,如可以校验请求参数,处理返回结果等

附加git代码地址: https://github.com/shizhenshuang123/start-demo

上面的实现,有个问题,当我不想用该功能时,相关的bean也会注入容器中,那如果我想实现动态可插拔功能,怎么处理?

要实现可插拔功能,那关键是对MyLogAutoConfiguration 这个配置类下手了,用到@ConditionalOnBean(xx.class)注解,当容器中含有xxbean时,才会使得配置生效,那如何使得xxbean可以注入容器,那就要用到@EnableXX注解

xxbean只是一个标记类,不用作特殊配置:

package org.example.config;

public class MyLogConfiguration {
}

@EnableXX注解:

package org.example.annotation;


import org.example.config.MyLogConfiguration;
import org.springframework.context.annotation.Import;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(MyLogConfiguration.class)
public @interface EnableMyLog {
}

修改MyLogAutoConfiguration配置类:

package org.example.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(MyLogProperties.class)
@ConditionalOnBean(MyLogConfiguration.class)  // 当容器中有这个xxbean就会使得下面配置生效
public class MyLogAutoConfiguration {
    @Bean
    public MyLogAspect myLogAspect(MyLogProperties myLogProperties) {
        return new MyLogAspect(myLogProperties);
    }

}

通过上面代码知道,如果要使得MyLogAutoConfiguration生效,容器中必须有LogMarkerConfiguration这个标志bean,容器中要有这个标志bean,就要用到@EnableMyLog注解,因此,当第三方引用我们的依赖时,只需要再主启动类上加入@EnableMyLog注解即可:

附加: zuul网关实现可插拔的原理也是一样:

image
image
image
image

你可能感兴趣的:(spring自动装配机制)