非常简单的一个启动器,功能只是调用方法传入参数,启动器就会自动添加前后缀(需自动在配置文件中配置)并打印
一、项目结构
starter项目是一个空项目,只需要引用autoconfigure到pom即可
二、autoconfigure代码
1.HelloService.java
这里是启动器具体功能的实现。
此类功能是添加前后缀并打印
package com.xiong.spring.boot.autoconfigure;
public class HelloService {
HelloProperties helloProperties;
public String sayHello(String name) {
return helloProperties.getPrefix() + "[" + name + "]" +helloProperties.getSuffix();
}
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
}
2.HelloProperties.java
可以在配置文件中配置的属性。
package com.xiong.spring.boot.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "xiong.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
3.HelloAutoConfiguration.java
将HelloService自动配置到容器中,自动配置在这里体现
package com.xiong.spring.boot.autoconfigure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
4.spring.factories
springboot开启自动配置后,启动时,会扫描此配置文件并将里面的类自动装载
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xiong.spring.boot.autoconfigure.HelloAutoConfiguration
5.pom
只需要一个spring-boot-starter就好
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.18.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xiong.spring.boot</groupId>
<artifactId>xiong-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xiong-spring-boot-autoconfigure</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
三、starter代码
starter是一个空项目,只需要引入autoconfigure就好
1.pom
<?xml version="1.0" encoding="UTF-8"?>
<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.xiong.spring.boot</groupId>
<artifactId>xiong-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.xiong.spring.boot</groupId>
<artifactId>xiong-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
四、test测试
1.在pom中引入启动类
<dependency>
<groupId>com.xiong.spring.boot</groupId>
<artifactId>xiong-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
2.application.properties
在配置文件中配置
xiong.hello.prefix=hello
xiong.hello.suffix=helloworld
3.HelloController.java
package com.xiong.springboot.controller;
import com.xiong.spring.boot.autoconfigure.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/hello")
public String hello(@RequestParam("name") String name) {
String s = helloService.sayHello(name);
return s;
}
}