比如现在有一个User类型,那么我们想实现的功能是,如果存在User.class这个类,就自动创建一个类型为User的Bean
UserProperties.java
package org.springframework.boot.autoconfigure.api; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "spring.user") public class UserProperties { private String username = "wuhulala"; private String phone = "15748451556"; private String email = "wuhulala@gmail.com"; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
package org.springframework.boot.autoconfigure.api; import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; /** * 功能说明: com.wuhulala.springboot.autoconfiguration.condition<br> * 注意事项: <br> * 系统版本: v1.0<br> * 开发人员: wuhulala<br> * 开发时间: 2018/4/6<br> */ public class UserApiCondition extends SpringBootCondition{ @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { boolean enable = context.getEnvironment().getProperty("user.enable", Boolean.class); System.out.println(enable); return new ConditionOutcome(enable, "用户开关开启状态:" + enable); } }
新建一个Configuration,并且设置条件为User.Class
WuhulalaApiAutoConfiguration.java
package org.springframework.boot.autoconfigure.api; import com.wuhulala.api.model.User; 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; /** * 功能说明: org.springframework.boot.autoconfigure.api<br> * 注意事项: <br> * 系统版本: v1.0<br> * 开发人员: wuhulala<br> * 开发时间: 2018/4/6<br> */ @Configuration @ConditionalOnClass(User.class) @EnableConfigurationProperties(UserProperties.class) public class WuhulalaApiAutoConfiguration { private final UserProperties properties; public WuhulalaApiAutoConfiguration(UserProperties properties) { this.properties = properties; } @Bean @Conditional(UserApiCondition.class) public User owner(){ String name = properties.getUsername(); String phone = properties.getPhone(); String mail = properties.getEmail(); User user = new User(name, mail, phone); System.out.println(user); return user; } }
注册AutoConfiguration
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.api.WuhulalaApiAutoConfiguration
对应修改元数据定义META-INF/additional-spring-configuration-metadata.json
{ "hints": [], "groups": [ { "sourceType": "org.springframework.boot.autoconfigure.api.UserProperties", "name": "spring.user", "type": "org.springframework.boot.autoconfigure.api.UserProperties" } ], "properties": [ { "name": "spring.user.username", "type": "java.lang.String", "defaultValue": "wuhulala", "description": "Description for spring.user.name." }, { "name": "spring.user.email", "type": "java.lang.String", "defaultValue": "wuhulala@gmail.com", "description": "Description for spring.user.name." }, { "name": "spring.user.phone", "type": "java.lang.String", "defaultValue": "15784855564", "description": "Description for spring.user.name." } ] }
gradle
配置org.springframework.boot:spring-boot-configuration-processor
用于生成元数据约束文件
compileJava.dependsOn(processResources) dependencies { compile('com.wuhulala:api:1.0-SNAPSHOT@jar') compile('org.springframework.boot:spring-boot-starter') compile('org.springframework.boot:spring-boot-starter-web'){ exclude module: 'org.springframework.boot:spring-boot-starter-tomcat' } compileOnly ("org.springframework.boot:spring-boot-configuration-processor") compile('org.springframework.boot:spring-boot-starter-jetty') testCompile('org.springframework.boot:spring-boot-starter-test') }