Spring Boot自动配置

来源:互联网 发布:微交易一分钟k线数据 编辑:程序博客网 时间:2024/06/05 16:07

当某个类存在时,自动配置这个类的 Bean,并可将 Bean 的属性在 application.properties 中配置。

示例:

1、新建一个 maven 项目。选择quickstart。在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.zsq.demo</groupId>    <artifactId>spring-boot-starter-demo</artifactId>    <version>1.0-SNAPSHOT</version>    <packaging>jar</packaging>    <name>spring-boot-starter-demo</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.0.M1</version>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>3.8.1</version>            <scope>test</scope>        </dependency>    </dependencies></project>
2、属性配置,代码如下:

package com.zsq.demo;import org.springframework.boot.context.properties.ConfigurationProperties;/** * Created by Administrator on 2017/12/19. */@ConfigurationProperties(prefix = "hello")public class DemoServiceProperties {    private static final String MSG = "world";    private String msg = MSG;    public String getMsg() {        return msg;    }    public void setMsg(String msg) {        this.msg = msg;    }}
3、判断依据类:
package com.zsq.demo;/** * Created by Administrator on 2017/12/19. */public class DemoService {    private String msg;    public String testMsg() {        return "test:" + msg;    }    public String getMsg() {        return msg;    }    public void setMsg(String msg) {        this.msg = msg;    }}
4、自动配置类:

package com.zsq.demo;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;/** * Created by Administrator on 2017/12/19. */@Configuration@EnableConfigurationProperties(DemoServiceProperties.class)@ConditionalOnClass(DemoService.class)@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)public class DemoServiceAutoConfiguration {    @Autowired    private DemoServiceProperties demoServiceProperties;    @Bean    @ConditionalOnMissingBean(DemoService.class)    public DemoService demoService() {        DemoService demoService = new DemoService();        demoService.setMsg(demoServiceProperties.getMsg());        return demoService;    }}
根据DemoServiceProperties 提供的参数,并通过 @ConditionalOnClass 判断 DemoService 这个类在路径中是否存在,且当容器中没有这个 Bean 的情况下自动配置这个 Bean。

5、注册配置,在 src/main/resources 下新建 META-INF/spring.factories。

在 spring.factories 中添加以下内容:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.zsq.demo.DemoServiceAutoConfiguration

若有多个自动配置,则用","隔开,此处"/" 是为了换行后仍然能读到属性。

6、将项目打包为jar导入,或者直接安装到本地仓库或者远程私服,直接导入使用。

原创粉丝点击