Spring Boot自定义starter pom实例(/META-INFO/spring.factory文件)
[size=large][b]自定义starter pom[/b][/size] 自己实现一个简单的例子,当某个类存在的时候,自动配置这个Bean,并且可以讲这个属性在application.properties中配置 新建一个maven项目(需要引入[color=red][b]spring-boot-autoconfigure[/b][/color]) Pom.xml [code="xml"] 4.0.0 com.ibigsea spring-boot-starter-hello 0.0.1-SNAPSHOT jar spring-boot-starter-hello http://maven.apache.org UTF-8 org.springframework.boot spring-boot-autoconfigure 1.3.3.RELEASE [/code] [code="java"] package com.ibigsea.spring_boot_starter_hello; /** * 通过application.properties的hello.msg来配置,默认为world * @author bigsea * */ public class Hello { private String msg; public String sayHello() { return "hello " + msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } } [/code] HelloProperties.java [code="java"] package com.ibigsea.spring_boot_starter_hello; import org.springframework.boot.context.properties.ConfigurationProperties; /** * 属性配置,Spring boot 自身的自动配置 * @author bigsea * */ @ConfigurationProperties(prefix="hello") public class HelloProperties { private static final String MSG = "world"; private String msg = MSG ; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } } [/code] HelloAutoConfiguration.java [code="java"] package com.ibigsea.spring_boot_starter_hello; 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 @EnableConfigurationProperties(HelloProperties.class)//开启属性注入,通过@autowired注入 @ConditionalOnClass(Hello.class)//这个类在classpath中存在 才会实例化一个对象 //当设置hello=enabled的情况下,如果没有设置则默认为true,即条件符合 @ConditionalOnProperty(prefix="hello", value="enabled", matchIfMissing = true) public class HelloAutoConfiguration { @Autowired private HelloProperties helloProperties; @Bean//使用java配置方式配置这个类 @ConditionalOnMissingBean(Hello.class)//容器中如果没有Hello这个类实例,那么自动配置这个Hello public Hello hello() { Hello hello = new Hello(); hello.setMsg(helloProperties.getMsg()); return hello; } } [/code] 并且要添加spring.factories [code="properties"] # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.ibigsea.spring_boot_starter_hello.HelloAutoConfiguration [/code] 整个项目结构 [img]http://dl2.iteye.com/upload/attachment/0129/7724/06ba3d00-0867-3b63-9a20-26c7097c7fe5.png[/img] 好了,到这里我们就完成了一个starter项目了,下面自己测试下 Pom.xml [code="xml"] 4.0.0 com.ibigsea test-starter 0.0.1-SNAPSHOT jar test-starter http://maven.apache.org UTF-8 1.3.3.RELEASE com.ibigsea spring-boot-starter-hello 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-web ${boot.version} org.springframework.boot spring-boot-starter-test ${boot.version} test [/code] App.java [code="java"] package com.ibigsea.test_starter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ibigsea.spring_boot_starter_hello.Hello; @SpringBootApplication @RestController public class App { @Autowired private Hello hello; @RequestMapping("/") public String index() { return hello.sayHello(); } public static void main(String[] args) { SpringApplication.run(App.class, args); } } [/code] 转自:[url]https://blog.csdn.net/a67474506/article/details/52013634[/url]