Spring Boot自动配置实战

来源:互联网 发布:王昱珩 知乎 编辑:程序博客网 时间:2024/05/22 15:38

Spring Boot自动配置

Spring Boot可以在我们引入对应的jar包后进行和spring的自动配置;这是理解Spring Boot运作原理的关键。

Spring Boot关于自动配置的源码在spring-boot-autoconfigure-1.x.x.RELEASE.jar内,主要包含如下的配置

包含的配置

下面我们模仿Spring Boot的自动配置,自己写一个项目也具有这样的功能

开发环境

java.version=jdk8spring-boot.version=1.5.2.RELEASEos.version=win8.1idea=Intellij 2017.3apache.maven.version=3.3.1

实现步骤

我们实现一个当某个类存在的时候,自动配置这个类的Bean,并可将Bean的属性在application.properties中配置

新建一个maven项目,pom文件如下:

<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>       <!--增加Spring Boot自身的自动配置作为依赖-->        <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>

属性配置,代码如下:

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

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;    }}

自动配置类:

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) //判断HelloService这个类在类路径中是否存在,且当容器中没有这个bean的时候自动配置这个bean@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,内容如下('\'为了换行使用):

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\  com.knight.spring_boot_starter_hello.HelloServiceAutoConfiguartion

项目的整个结构如下图:

![spring boot自定义自动配置](http://opzxa0ul7.bkt.clouddn.com/spring boot自定义自动配置.jpg)

验证上面配置创建的自动配置是否可行,创建一个项目然后引入该项目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>    <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类

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@SpringBootApplicationpublic 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文件,内容如下:

hello.msg=Spring Boot

测试自动配置自定义值结果

原创粉丝点击