SpringBoot 加载jar包配置类/SPI机制/自定义starter
简介:使用SpringBoot开发应用,打包成jar包后,被别的项目引入时,需要注意直接引入不会对jar包里的配置类生效,需要如下配置才可以(五种方式任选一种)。
1. 配置@SpringBootApplication注解的扫描属性
在引入第三方jar包的服务启动类上添加@SpringBootApplication注解,并配置scanBasePackages等属性,指定需要扫描的配置类;
package com.ruhuanxingyun.dcy.service.manage; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableFeignClients @EnableTransactionManagement @MapperScan("com.ruhuanxingyun.dcy.service.manage.mapper") @SpringBootApplication(scanBasePackages = {"com.ruhuanxingyun.dcy.service.manage", "com.ruhuanxingyun.dcy.common.jwt.config", "com.ruhuanxingyun.dcy.common.tool.config"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
2. 配置@ComponentScan注解的扫描属性
在引入第三方jar包服务的启动类上添加@ComponentScan注解,并配置basePackages等属性,指定需要扫描的配置类;
3. 引用自定义注解开关
A. 第三方jar包里添加配置类;
package com.ruhuanxingyun.dcy.common.tool.config; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; /** * @description: FastJson 配置 * @author: ruphie * @date: Create in 2021/1/19 21:58 * @company: ruhuanxingyun */ @Configuration @ComponentScan(basePackageClasses = {FastJsonConfig.class}) public class FastJsonConfig { @Bean public HttpMessageConverter httpMessageConverter() { // 定义一个convert转换消息的对象 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); // FastJson配置 com.alibaba.fastjson.support.config.FastJsonConfig fastJsonConfig = new com.alibaba.fastjson.support.config.FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 添加fastJson配置信息 fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); fastJsonHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8); // 设置MediaType List<MediaType> supportedMediaTypes = new ArrayList<>(); supportedMediaTypes.add(MediaType.APPLICATION_JSON); fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes); return fastJsonHttpMessageConverter; } }
B. 第三方jar包里添加注解开关;
package com.ruhuanxingyun.dcy.common.tool.annotation; import com.ruhuanxingyun.dcy.common.tool.config.FastJsonConfig; import org.springframework.context.annotation.Import; import java.lang.annotation.*; /** * @description: FastJson配置注解 * @author: ruphie * @date: Create in 2021/3/8 22:12 * @company: ruhuanxingyun */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Documented @Import({FastJsonConfig.class}) public @interface EnableFastJson { }
C. 在引入第三方jar包的服务启动类上添加注解开关。
package com.ruhuanxingyun.dcy.service.manage; import com.ruhuanxingyun.dcy.common.tool.annotation.EnableFastJson; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableFastJson @EnableFeignClients @EnableTransactionManagement @MapperScan("com.ruhuanxingyun.dcy.service.manage.mapper") @SpringBootApplication(scanBasePackages = {"com.ruhuanxingyun.dcy.service.manage", "com.ruhuanxingyun.dcy.common.jwt.config"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
4. 配置类配置@Bean注解
在引入第三方jar包的服务里添加配置类,同时使用@Bean配置需要使用的配置类;
package com.ruhuanxingyun.dcy.gateway.gateway.config; import com.ruhuanxingyun.dcy.common.jwt.config.TokenProvider; import com.ruhuanxingyun.dcy.common.tool.config.FastJsonConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @description: 公共 配置类 * @author: ruphie * @date: Create in 2021/3/6 21:37 * @company: ruhuanxingyun */ @Configuration public class CommonConfig { @Bean public TokenProvider tokenProvider() { return new TokenProvider(); } @Bean public FastJsonConfig fastJsonConfig() { return new FastJsonConfig(); } }
5. SpringBoot自动配置SPI机制
A. 在第三方jar包的项目resources目录下新建META-INF目录,并创建spring.factories文件;
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ruhuanxingyun.dcy.common.tool.config.FastJsonConfig
B. org.springframework.boot.autoconfigure.EnableAutoConfiguration代表需要配置哪些类,\代表分行符,FastJsonConfig代表自动配置类,需写入类全限定名。
6. SpringBoot自动配置原理
A. @EnableAutoConfiguration、@Configuration和@ConditionalOnClass三个注解构成自动配置的核心;
B. @EnableAutoConfiguration给容器导入META-INF/spring.factories里定义的自动配置类;
C. 筛选有效的自动配置类,每一个自动配置类结合对应的xxxProperties.java读取配置文件进行自动配置功能。