Spring Boot自动配置
Spring Boot可以在我们引入对应的jar包后进行和spring的自动配置;这是理解Spring Boot运作原理的关键。
Spring Boot关于自动配置的源码在spring-boot-autoconfigure-1.x.x.RELEASE.jar
内,主要包含如下的配置
下面我们模仿Spring Boot的自动配置,自己写一个项目也具有这样的功能
开发环境
1 2 3 4 5
| java.version=jdk8 spring-boot.version=1.5.2.RELEASE os.version=win8.1 idea=Intellij 2017.3 apache.maven.version=3.3.1
|
实现步骤
我们实现一个当某个类存在的时候,自动配置这个类的Bean,并可将Bean的属性在application.properties
中配置
新建一个maven项目,pom文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <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.knight</groupId> <artifactId>auto-config</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.2.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
|
属性配置,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.knight.spring_boot_starter_hello; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "hello") public class HelloServiceProperties { private static final String MSG = "world"; private String msg = MSG; public static String getMSG() { return MSG; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
|
这里使用了类型安全属性配置。在application.properties
中通过hello.msg=
来指定
判断依据类,当这个类存在时会自动配置这个Bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.knight.spring_boot_starter_hello; 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; } }
|
自动配置类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.knight.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(HelloServiceProperties.class) @ConditionalOnClass(HelloService.class) @ConditionalOnProperty(prefix = "hello", value = "enabled", matchIfMissing = true) public class HelloServiceAutoConfiguartion { @Autowired private HelloServiceProperties helloServiceProperties; @Bean @ConditionalOnMissingBean(HelloService.class) public HelloService helloService() { HelloService helloService = new HelloService(); helloService.setMsg(helloServiceProperties.getMsg()); return helloService; } }
|
注册配置
若想自动配置生效,需要注册自动配置类。在sr/main/resources
下创建META-INF/spring.factories
,内容如下('\'
为了换行使用):
1 2
| org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.knight.spring_boot_starter_hello.HelloServiceAutoConfiguartion
|
项目的整个结构如下图:
验证上面配置创建的自动配置是否可行,创建一个项目然后引入该项目pom.xml文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <groupId>com.knight</groupId> <artifactId>auto-config-test</artifactId> <version>1.0.0</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.knight</groupId> <artifactId>auto-config</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
|
创建一个Application类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.knight.ch6_5; import com.knight.spring_boot_starter_hello.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 Ch65Application { @Autowired private HelloService helloService; public static void main(String[] args) { SpringApplication.run(Ch65Application.class, args); } @RequestMapping("/") public String index() { return helloService.sayHello(); } }
|
访问结果如下:
测试通过application.properties
自定义配置属性,在Resources 目录下新建一个application.properties文件,内容如下: