自己写spring boot starter
学习了:《spring boot实战》汪云飞著 6.5.4节
pom.xml
4.0.0 com.stono sboot161 1.0-SNAPSHOT jar sboot161 http://maven.apache.org UTF-8 org.springframework.boot spring-boot-autoconfigure 1.5.9.RELEASE junit junit 3.8.1 test
HelloService.java
package com.stono;public class HelloService { private String msg; public String sayHello(){ return "Hello"+msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }}
HelloServiceProperties.java
package com.stono;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "hello")public class HelloServiceProperties { private static final String MSG = "world"; private String msg = MSG; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }}
HelloServiceAutoConfiguration.java
package com.stono;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(HelloServiceProperties.class)@ConditionalOnClass(HelloService.class)@ConditionalOnProperty(prefix = "hello", value = "enabled", matchIfMissing = true)public class HelloServiceAutoConfiguration { @Autowired private HelloServiceProperties helloServiceProperties; @Bean @ConditionalOnMissingBean(HelloService.class) public HelloService helloService(){ HelloService helloService = new HelloService(); helloService.setMsg(helloServiceProperties.getMsg()); return helloService; }}
编译完毕之后,使用IntelliJ 自带的Edit Configurations... 进行maven 编译设置,参数为 clean package
运行配置好的maven命令即可编译jar包;
如果在terminal中直接运行mvn clean package
会出现错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project sboot161: Compilation failure: Compilation failure:[ERROR] 不再支持源选项 1.5。请使用 1.6 或更高版本。[ERROR] 不再支持目标选项 1.5。请使用 1.6 或更高版本。[ERROR] -> [Help 1][ERROR][ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.[ERROR] Re-run Maven using the -X switch to enable full debug logging.[ERROR][ERROR] For more information about the errors and possible solutions, please read the following articles:[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
学习了:http://blog.csdn.net/sosous/article/details/78312867
进行pom.xml文件修改:
UTF-8 1.8 1.8
就可以正常运行 mvn clean package了;
运行之后,需要使用mvn install命令将jar包安装本地库:
mvn install:install-file -Dfile=D:\Java\IdeaProjects\sboot161\target\sboot161-1.0-SNAPSHOT.jar -DgroupId=com.stono -DartifactId=sboot161 -Dversion=1.0-SNAPSHOT -Dpackaging=jar
然后在其他项目中就可以添加该pom依赖,在程序中自动引入该HelloService了;