pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.allen.start</groupId> <artifactId>no1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>no1</name> <description>自定义启动器</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.6.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <!--<version>3.1</version>--> <configuration> <source>1.8</source> <target>1.8</target> <compilerVersion>1.8</compilerVersion> </configuration> </plugin> </plugins> </build> </project>
自定义start
4个注解+一个spring.factories完成自定义start
4个注解:
2个作用:
- 属性类(把配置文件的配置项目转化java对象)@ConfigurationProperties
- 自动配置类:
2.1 指定其是配置类---@Configuration
-
- 开启属性配置@EnableConfigurationProperties
- 指定其自动配置类在什么情况下,使用--@ConditionalOnClass(条件类)
-
- @Configuration
告诉spring这个是配置类
1.2 @ConfigurationProperties(prefix = "前缀")
告诉spring这个配置属性类,并归spring管理可以直接注入使用
-
- @EnableConfigurationProperties(@ConfigurationProperties修饰的类.class)
开启此配置属性类
-
- @ConditionalOnClass(MyService.class--条件类)
条件类,说明classpath下有此类时,此配置类才可以使用
- 在resource建个META-INF目录,并新建一个spring.factorties文件
其内容:自动配置类的全限定名
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.allen.start.no1.mystart.MyServiceAutoConfiguration
- 配置属性类
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import java.nio.charset.StandardCharsets; /** * @author AW * @date 2019/7/19 10:13 * @desc 用于和配置文件字段映射 * @ConfigurationProperties 2个作用: * 1. 标识此类是配置属性类 * 2. 此类被spring管理,可以直接注入 */ @ConfigurationProperties(prefix = "allen") //必须要有@EnableConfigurationProperties使用此类,否则idea飘红 public class MyProperties { private static final String DEFAULT_NAME = "allen"; private static final String DEFAULT_MSG = "JAVA之窗"; private String name = DEFAULT_NAME; private String msg = DEFAULT_MSG; public String getName() { return name; } public void setName(@Value("${allen.name}") String name) { //解决配置文件中文乱码问题 this.name = new String(name.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); // this.name = name; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = new String(msg.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); // this.msg = msg; } }
- 此start对外提供的服务
/** * @author AW * @date 2019/7/19 10:16 * @desc 用于其他项目调用(测试用的) */ public class MyService { private String msg; private String name; public String sayHello() { return name + " say " + msg + " !"; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
- 配置类:
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.annotation.Resource; /** * @author AW * @date 2019/7/19 10:17 * @desc * 1. 是个配置类 * 2. 打开配置项开关(如果此jar被使用) * 3. 开关:什么条件下,开启此功能 * */ @Configuration @EnableConfigurationProperties(MyProperties.class) @ConditionalOnClass(MyService.class) public class MyServiceAutoConfiguration { @Resource MyProperties helloProperties; /** * 创建一个bean,一般其他依赖此jar包的项目可以直接使用 */ @Bean MyService myService() { MyService myService = new MyService(); myService.setName(helloProperties.getName()); myService.setMsg(helloProperties.getMsg()); return myService; } }
ps:把此项目,执行maven的install命令打包到本地,其他项目引入依赖即可
源码在我的csdn可以下载