[size=large][b]自定义starter pom[/b][/size]
自己实现一个简单的例子,当某个类存在的时候,自动配置这个Bean,并且可以讲这个属性在application.properties中配置
新建一个maven项目(需要引入[color=red][b]spring-boot-autoconfigure[/b][/color])
Pom.xml
HelloProperties.java
HelloAutoConfiguration.java
并且要添加spring.factories
整个项目结构
[img]http://dl2.iteye.com/upload/attachment/0129/7724/06ba3d00-0867-3b63-9a20-26c7097c7fe5.png[/img]
好了,到这里我们就完成了一个starter项目了,下面自己测试下
Pom.xml
App.java
转自:[url]https://blog.csdn.net/a67474506/article/details/52013634[/url]
自己实现一个简单的例子,当某个类存在的时候,自动配置这个Bean,并且可以讲这个属性在application.properties中配置
新建一个maven项目(需要引入[color=red][b]spring-boot-autoconfigure[/b][/color])
Pom.xml
<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.ibigsea</groupId>
<artifactId>spring-boot-starter-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-starter-hello</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.3.3.RELEASE</version>
</dependency>
</dependencies>
</project>
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;
}
}
HelloProperties.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;
}
}
HelloAutoConfiguration.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;
}
}
并且要添加spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ibigsea.spring_boot_starter_hello.HelloAutoConfiguration
整个项目结构
[img]http://dl2.iteye.com/upload/attachment/0129/7724/06ba3d00-0867-3b63-9a20-26c7097c7fe5.png[/img]
好了,到这里我们就完成了一个starter项目了,下面自己测试下
Pom.xml
<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.ibigsea</groupId>
<artifactId>test-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-starter</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<boot.version>1.3.3.RELEASE</boot.version>
</properties>
<dependencies>
<dependency>
<groupId>com.ibigsea</groupId>
<artifactId>spring-boot-starter-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${boot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
App.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);
}
}
转自:[url]https://blog.csdn.net/a67474506/article/details/52013634[/url]