Spring Boot 运行原理 - 实战 创建一个starter-pom

 

参考前面http编码设置的自动配置,我们来实现一个自己自动配置,在这里我们做得测底点的直接写一个自己的starter pom。这样我们不仅有自己的自动配置,而且耦合度更低。

这里举一个简单的例子,包含当某个类存在的时候自动配置这个Bean,并可将Bean的属性在application.properties中配置。

1 新建一个starter 的maven项目

1. 修改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>

	<groupId>com.chenfeng.xiaolyuh.hello</groupId>
	<artifactId>spring-boot-starter-hello</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-starter-hello</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
	    <!--在此处添加Spring Boot自身的自动配置最为依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfi
			
			gure</artifactId>
			<version>1.5.3.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

2. 属性配置类(安全类型配置类)

代码如下:

package com.chenfeng.xiaolyuh.hello.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {// 使用安全类型来获取属性,再application.properties中使用hello.msg来设置值,默认是world
	private static final String MSG = "world";
	
	private String msg = MSG;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
}

3. 判断依据类(也是需要自动配置的Bean)

package com.chenfeng.xiaolyuh.hello.service;

/**
 * @ClassName HelloService
 * @Description 需要自动配置的Bean
 * @author yuhao.wang
 * @Date 2017年4月28日 上午10:42:41
 * @version 1.0.0
 */
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;
	}
	
}

根据此类的存在与否来判断是否要注入这个Bean,这个类也可以是第三方的Bean。

4. 自动配置类

代码如下:

package com.chenfeng.xiaolyuh.hello.autoconfiguration;

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;

import com.chenfeng.xiaolyuh.hello.properties.HelloServiceProperties;
import com.chenfeng.xiaolyuh.hello.service.HelloService;

@Configuration // 声明是配置类
@EnableConfigurationProperties(HelloServiceProperties.class) // 开启属性注入,以@EnableConfigurationProperties声明,使用@Autowired注入
@ConditionalOnClass(HelloService.class)// 当HelloService在类路劲的条件下
@ConditionalOnProperty(prefix = "hello", value = "enabled", matchIfMissing = true) // // 当设置hello=enabled的情况下,如果没有设置默认是true,即符合条件
public class HelloServiceAutoconfiguration {
	
	@Autowired
	private HelloServiceProperties helloServiceProperties;
	
	@Bean
	@ConditionalOnMissingBean(HelloService.class)
	public HelloService helloService(){
		HelloService helloService = new HelloService();
		helloService.setMsg(helloServiceProperties.getMsg());
		return helloService;
	}
}

根据HelloServiceProperties提供参数,并通过@ConditionalOnClass判断HelloService这个类在类路径下是否存在,且当容器中没有这个Bean的情况下自动配置这个Bean。

5. 注册配置类

在前面的文章中我们提到过,如想自动配置生效,需要注册自动配置类。在src/main/resources下新建META-INF/spring.factories文件,并再spring.factories文件中添加如下注册内容:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.chenfeng.xiaolyuh.hello.autoconfiguration.HelloServiceAutoconfiguration

若有多个自动配置则中“,”号隔开,此处用的“\”是为了换行后任然能读到属性。

如果新建项目中没有src/main/resources文件夹,需要执行下面操作:

imageimage

2 新建Spring Boot项目

1. 在pom中添加spring-boot-starter-hello项目的依赖:

<dependency>
	<groupId>com.chenfeng.xiaolyuh.hello</groupId>
	<artifactId>spring-boot-starter-hello</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

2. 在application.properties中添加hello的配置

hello.msg=wyh

3. 使用自定义的自动配置类

package com.chenfeng.xiaolyuh;

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;

import com.chenfeng.xiaolyuh.hello.service.HelloService;

@SpringBootApplication // 这个是Spring Boot项目的核心注解,主要是开启自动配置。
@RestController
public class SpringBootStudentApplication {
	
	@Autowired // 通过注解直接注入配置类
	private HelloService helloService; 
	
	@RequestMapping("/")
	public String index() {
		return helloService.sayHello();
	}

	// 标准的JAVA应用main方法,主要作用作为项目启动的入口
	public static void main(String[] args) {
		SpringApplication.run(SpringBootStudentApplication.class, args);
	}

}

在代码中我们可以直接注入HelloService这个Bean,但是在项目中我们并没有配置这个Bean,这是通过自动配置完成的。

3 部署测试

直接访问http://localhost:8080/ 查看效果

  1.  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值