自动化配置需满足两个条件:
- 能够生成 Bean,并注册到 Bean 容器中;
- 能够自动配置项目所需要的配置。
1、首先创建一个spring boot 项目,删除不需要的依赖、test包,另外启动类不需要,否则会导致@ConfigurationProperties冲突,只留一个依赖即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
只引入这一个starter是因为在这个spring-boot-starter里面有我们需要的自动配置相关的依赖
2、创建属性类
创建属性类,prefix = "helloworld"代表该项目在属性文件中配置的前缀,即可以在属性文件中通过 helloworld.***=***,就可以改变属性类字段的值了。
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @Author yunkang
* @Date 2019-06-05 20:53
* @Description TODO
*/
@ConfigurationProperties(prefix = "helloworld")
public class HelloWorldProperties {
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、创建一个服务类,用于提供服务
/**
* @Author yunkang
* @Date 2019-06-06 10:57
* @Description TODO
*/
public class HelloService {
HelloWorldProperties helloWorldProperties;
public HelloWorldProperties getHelloWorldProperties() {
return helloWorldProperties;
}
public void setHelloWorldProperties(HelloWorldProperties helloWorldProperties) {
this.helloWorldProperties = helloWorldProperties;
}
/**
* 外部调用方法
* @param name
* @return
*/
public String sayHello(String name){
return helloWorldProperties.getPrefix()+"-"+name + helloWorldProperties.getSuffix();
}
}
4、创建自动化配置类
这个相当于就是一个普通的 Java 配置类,可以在这里创建 Bean,并可获得与 application.properties 属性文件相对应的属性类的 Bean。
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.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author yunkang
* @Date 2019-06-06 10:59
* @Description TODO
*/
// 相当于一个普通的 java 配置类
@Configuration
// 当 HelloService 在类路径的条件下
@ConditionalOnClass({HelloService.class})
// 将 application.properties 的相关的属性字段与该类一一对应,并生成 Bean
@EnableConfigurationProperties(HelloWorldProperties.class)
public class HelloworldAutoConfiguration {
// 注入属性类
@Autowired
private HelloWorldProperties helloWorldProperties;
@Bean
// 当容器没有这个 Bean 的时候才创建这个 Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloWorldProperties(helloWorldProperties);
return helloService;
}
}
5、在 META-INF 目录下创建 spring.factories,这个属性文件非常重要,因为 SpringBoot 自动化配置最终就是要扫描 META-INF/spring.factories 来加载项目的自动化配置类。
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.helloworld.HelloworldAutoConfiguration
完成后大致结构如下:
好了,starter就可以用了
只需在项目里引用
新建一个spring boot项目引入starter,开始引用
package com.example.demo;
import com.example.helloworld.HelloService;
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;
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Autowired
private HelloService helloworldService;
@RequestMapping("/")
public String sayHello() {
return helloworldService.sayHello("zhangsan");
}
}
配置文件application.properties可以配置
helloworld.prefix=hi
helloworld.suffix=haole
server.port=8081
然后访问项目,binggo