您的位置:首页 > 编程语言 > Java开发

Spring Boot自动配置

2017-12-19 10:16 381 查看
当某个类存在时,自动配置这个类的 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

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

6、将项目打包为jar导入,或者直接安装到本地仓库或者远程私服,直接导入使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: