自定义一个Starter(spring_boot_mystarters)。要求:当类路径中存在MyService类时,自动配置该类的Bean,并可以将相应Bean的属性在application.properties中配置。
修改Spring Boot项目spring_boot_mystarters的pom文件,增加Spring Boot自身的自动配置作为依赖
在项目spring_boot_mystarters的com.ch.spring_boot_mystarters包中,创建属性配置类MyProperties。在使用spring_boot_mystarters的Spring Boot项目的application.properties中,可以使用my.msg=设置属性;若不设置,默认为my.msg=默认值。
在项目spring_boot_mystarters的com.ch.spring_boot_mystarters包中,创建判断依据类MyService。自定义的Starters将根据该类的存在与否来创建该类的Bean,该类可以是第三方类库的类。
创建自动配置类MyAutoConfiguration
在项目spring_boot_mystarters的com.ch.spring_boot_mystarters包中,创建自动配置类MyAutoConfiguration。在该类中使用@EnableConfigurationProperties注解开启属性配置类MyProperties提供参数;使用@ConditionalOnClass注解判断类加载器(类路径)中是否存在MyService类;使用@ConditionalOnMissingBean注解判断当容器中不存在MyService的Bean时,自动配置这个Bean。
注册配置 在项目spring_boot_mystarters的src/main/resources目录下新建文件夹META-INF,并在该文件夹下创建名为spring.factories的文件。在spring.factories文件中添加如下内容注册自动配置类MyAutoConfiguration: org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.ch.spring_boot_mystarters.MyAutoConfiguration 上述文件内容中,若有多个自动配置,则使用“,”分开,此处“\”是为了换行后仍然能读到属性值。 至此,经过上述6个步骤后,自定义Starters(spring_boot_mystarters)已经完成。我们可以将spring_boot_mystarters安装到Maven的本地库,或者将jar包发布到Maven的私服上。
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.cc</groupId> <artifactId>spring_boot_mystarters01</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>spring_boot_mystarters01 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package com.ch.spring_boot_mystarters; import org.springframework.boot.context.properties.ConfigurationProperties; //在application.properties中通过my.msg=设置属性 @ConfigurationProperties(prefix = "my") public class MyProperties { private String msg = "默认值"; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
package com.ch.spring_boot_mystarters; public class MyService { private String msg; public String sayMsg() { return "my " + msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
package com.ch.spring_boot_mystarters; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration //开启属性配置类MyProperties提供参数 @EnableConfigurationProperties(MyProperties.class) //类加载器(类路径)中是否存在对应的类 @ConditionalOnClass(MyService.class) //应用环境中属性是否存在指定的值 @ConditionalOnProperty(prefix = "my", value = "enabled", matchIfMissing = true) public class MyAutoConfiguration { @Autowired private MyProperties myProperties; @Bean // 当容器中不存在MyService的Bean时,自动配置这个Bean @ConditionalOnMissingBean(MyService.class) public MyService myService() { MyService myService = new MyService(); myService.setMsg(myProperties.getMsg()); return myService; } }
package com.ch.spring_boot_mystarters; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootMystartersApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMystartersApplication.class, args); } }
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ch.spring_boot_mystarters.MyAutoConfiguration