当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot自定义starter实例代码

SpringBoot自定义starter实例代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

一、简介

springboot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能。即使是这样,springboot也不能囊括我们所有的使用场景,往往我们需要自定义starter,来简化我们对springboot的使用。

下面话不多说了,来一起看看详细的介绍吧

二、如何自定义starter

1.实例

如何编写自动配置 ?

我们参照@webmvcautoconfiguration为例,我们看看需要准备哪些东西,下面是webmvcautoconfiguration的部分代码:

@configuration
@conditionalonwebapplication
@conditionalonclass({servlet.class, dispatcherservlet.class, webmvcconfigureradapter.class})
@conditionalonmissingbean({webmvcconfigurationsupport.class})
@autoconfigureorder(-2147483638)
@autoconfigureafter({dispatcherservletautoconfiguration.class, validationautoconfiguration.class})
public class webmvcautoconfiguration {

	@import({webmvcautoconfiguration.enablewebmvcconfiguration.class})
 @enableconfigurationproperties({webmvcproperties.class, resourceproperties.class})
 public static class webmvcautoconfigurationadapter extends webmvcconfigureradapter {

 @bean
 @conditionalonbean({view.class})
 @conditionalonmissingbean
 public beannameviewresolver beannameviewresolver() {
  beannameviewresolver resolver = new beannameviewresolver();
  resolver.setorder(2147483637);
  return resolver;
 }
 }
}

我们可以抽取到我们自定义starter时,同样需要的一些配置。

@configuration //指定这个类是一个配置类
@conditionalonxxx //指定条件成立的情况下自动配置类生效
@autoconfigureorder //指定自动配置类的顺序
@bean //向容器中添加组件
@configurationproperties //结合相关xxxproperties来绑定相关的配置
@enableconfigurationproperties //让xxxproperties生效加入到容器中

自动配置类要能加载需要将自动配置类,配置在meta-inf/spring.factories中
org.springframework.boot.autoconfigure.enableautoconfiguration=\
org.springframework.boot.autoconfigure.admin.springapplicationadminjmxautoconfiguration,\
org.springframework.boot.autoconfigure.aop.aopautoconfiguration,\

模式

我们参照 spring-boot-starter 我们发现其中没有代码:

我们在看它的pom中的依赖中有个 springboot-starter

<dependency>
	<groupid>org.springframework.boot</groupid>
	<artifactid>spring-boot-starter</artifactid>
</dependency>

我们再看看 spring-boot-starter 有个 spring-boot-autoconfigure

<dependency>
	<groupid>org.springframework.boot</groupid>
	<artifactid>spring-boot-autoconfigure</artifactid>
</dependency>

关于web的一些自动配置都写在了这里 ,所以我们有以下总结:

启动器starter只是用来做依赖管理
需要专门写一个类似spring-boot-autoconfigure的配置模块
用的时候只需要引入启动器starter,就可以使用自动配置了

命名规范

官方命名空间

  • 前缀:spring-boot-starter-
  • 模式:spring-boot-starter-模块名
  • 举例:spring-boot-starter-web、spring-boot-starter-jdbc

自定义命名空间

  • 后缀:-spring-boot-starter
  • 模式:模块-spring-boot-starter
  • 举例:mybatis-spring-boot-starter

三、自定义starter实例

我们需要先创建两个工程 hello-spring-boot-starter 和 hello-spring-boot-starter-autoconfigurer

1. hello-spring-boot-starter

1.pom.xml

<?xml version="1.0" encoding="utf-8"?>
<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.gf</groupid>
	<artifactid>hello-spring-boot-starter</artifactid>
	<version>0.0.1-snapshot</version>
	<packaging>jar</packaging>

	<name>hello-spring-boot-starter</name>

	<!-- 启动器 -->
	<dependencies>
		<!-- 引入自动配置模块 -->
		<dependency>
			<groupid>com.gf</groupid>
			<artifactid>hello-spring-boot-starter-autoconfigurer</artifactid>
			<version>0.0.1-snapshot</version>
		</dependency>
	</dependencies>
</project>

同时删除 启动类、resources下的文件,test文件。

2. hello-spring-boot-starter-autoconfigurer

1. pom.xml

<?xml version="1.0" encoding="utf-8"?>
<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.gf</groupid>
	<artifactid>hello-spring-boot-starter-autoconfigurer</artifactid>
	<version>0.0.1-snapshot</version>
	<packaging>jar</packaging>

	<name>hello-spring-boot-starter-autoconfigurer</name>
	<description>demo project for spring boot</description>

	<parent>
		<groupid>org.springframework.boot</groupid>
		<artifactid>spring-boot-starter-parent</artifactid>
		<version>1.5.9.release</version>
		<relativepath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceencoding>utf-8</project.build.sourceencoding>
		<project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- 引入spring-boot-starter,所有starter的基本配合 -->
		<dependency>
			<groupid>org.springframework.boot</groupid>
			<artifactid>spring-boot-starter</artifactid>
		</dependency>

	</dependencies>
</project>

2. helloproperties

package com.gf.service;

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

@configurationproperties(prefix = "gf.hello")
public class helloproperties {

 private string prefix;
 private string suffix;

 public string getprefix() {
  return prefix;
 }

 public void setprefix(string prefix) {
  this.prefix = prefix;
 }

 public string getsuffix() {
  return suffix;
 }

 public void setsuffix(string suffix) {
  this.suffix = suffix;
 }
}

3. helloservice

package com.gf.service;


public class helloservice {

 helloproperties helloproperties;

 public helloproperties gethelloproperties() {
  return helloproperties;
 }

 public void sethelloproperties(helloproperties helloproperties) {
  this.helloproperties = helloproperties;
 }

 public string sayhello(string name ) {
  return helloproperties.getprefix()+ "-" + name + helloproperties.getsuffix();
 }
}

4. helloserviceautoconfiguration

package com.gf.service;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.autoconfigure.condition.conditionalonwebapplication;
import org.springframework.boot.context.properties.enableconfigurationproperties;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

@configuration
@conditionalonwebapplication //web应该生效
@enableconfigurationproperties(helloproperties.class)
public class helloserviceautoconfiguration {

 @autowired
 helloproperties helloproperties;

 @bean
 public helloservice helloservice() {
  helloservice service = new helloservice();
  service.sethelloproperties( helloproperties );
  return service;
 }
}

5. spring.factories

在 resources 下创建文件夹 meta-inf 并在 meta-inf 下创建文件 spring.factories ,内容如下:

# auto configure
org.springframework.boot.autoconfigure.enableautoconfiguration=\
com.gf.service.helloserviceautoconfiguration

到这儿,我们的配置自定义的starter就写完了 ,我们把 hello-spring-boot-starter-autoconfigurer、hello-spring-boot-starter 安装成本地jar包。

三、测试自定义starter

我们创建个项目 hello-spring-boot-starter-test,来测试系我们写的stater。

1. pom.xml

<?xml version="1.0" encoding="utf-8"?>
<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.gf</groupid>
	<artifactid>hello-spring-boot-starter-test</artifactid>
	<version>0.0.1-snapshot</version>
	<packaging>jar</packaging>

	<name>hello-spring-boot-starter-test</name>
	<description>demo project for spring boot</description>

	<parent>
		<groupid>org.springframework.boot</groupid>
		<artifactid>spring-boot-starter-parent</artifactid>
		<version>1.5.9.release</version>
		<relativepath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceencoding>utf-8</project.build.sourceencoding>
		<project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupid>org.springframework.boot</groupid>
			<artifactid>spring-boot-starter-web</artifactid>
		</dependency>

		<!-- 引入自定义starter -->
		<dependency>
			<groupid>com.gf</groupid>
			<artifactid>hello-spring-boot-starter</artifactid>
			<version>0.0.1-snapshot</version>
		</dependency>

		<dependency>
			<groupid>org.springframework.boot</groupid>
			<artifactid>spring-boot-starter-test</artifactid>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupid>org.springframework.boot</groupid>
				<artifactid>spring-boot-maven-plugin</artifactid>
			</plugin>
		</plugins>
	</build>
</project>

2. hellocontroller

package com.gf.controller;

import com.gf.service.helloservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.restcontroller;

@restcontroller
public class hellocontroller {

 @autowired
 helloservice helloservice;

 @getmapping("/hello/{name}")
 public string hello(@pathvariable(value = "name") string name) {
  return helloservice.sayhello( name + " , " );
 }
}

3. application.properties

gf.hello.prefix = hi
gf.hello.suffix = what's up man ?

我运行项目访问 http://127.0.0.1:8080/hello/zhangsan,结果如下:

hi-zhangsan , what's up man ?

源码下载: https://github.com/gf-huanchupk/springbootlearning

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网