spring boot 自定义starter的实现教程

网友投稿 281 2023-03-06


spring boot 自定义starter的实现教程

spring boot 使用 starter 解决了很多配置问题, 但是, 他是怎么来解决这些问题的呢?

这里通过一个简单的例子, 来看一下, starter是怎么来设置默认配置的.

一. 建 starter 项目

自定义的starter, 项目命名规范是: 自定义名-spring-boot-starter

先来看一下, 我最后的目录结构

1. 修改pom.xml文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.elvin

my-spring-boot-starter

1.0-SNAPSHOT

jar

my-spring-boot-starter

http://maven.apache.org

UTF-8

org.springframework.boot

spring-boot-autoconfigure

1.5.9.RELEASE

junit

junit

3.8.1

test

org.apache.maven.plugins

maven-compiler-plugin

2.3.2

1.8

1.8

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.elvin

my-spring-boot-starter

1.0-SNAPSHOT

jar

my-spring-boot-starter

http://maven.apache.org

UTF-8

org.springframework.boot

spring-boot-autoconfigure

1.5.9.RELEASE

junit

junit

3.8.1

test

org.apache.maven.plugins

maven-compiler-plugin

2.3.2

1.8

1.8

其实只是加入了 spring-boot-autoconfigure

App文件中的main方法, 我注释掉了, 这个在这里没有用到

2. 配置属性对应的接收文件

package org.elvin;

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

* author: Elvin

* Date: 2017/12/12 14:51

* Description:

*/

@ConfigurationProperties(prefix = "hello")

public class HelloServiceProperties {

//默认配置内容

private static final String MSG = "world";

private String msg = MSG;

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

}

3. 对外Service

package org.elvin;

/**

* author: Elvin

* Date: 2017/12/12 14:55

* Description:

*/

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;

}

}

4. 对外service与配置对应文件关联

package org.elvin;

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.properIiLLjties.EnableConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

* author: Elvin

* Date: 2017/12/12 14:59

* Description:

*/

@Configuration

@EnableConfigurationProperties(HelloServiceProperties.class)

@ConditionalOnClass(HelloService.class)

@ConditionalOnProperty(prefix = "hello", value="enabled", matchIfMissing =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;

}

}

5. starter配置 : spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.elvin.HelloServiceAutoConfiguration

做完这些之后, 通过 mvn clean install , 打包到maven库里面

二. spring boot 项目使用

新建一个spring boot 项目, 选择web即可.

目录结构:

先看一下引用pom.xml

org.elvin

my-spring-boot-starter

1.0-SNAPSHOT

再看一下HelloController

package org.elvin.learn.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import org.elvin.*;

/**

* author: Elvin

* Date: 2017/12/12 15:34

* Description:

*/

@RestController

@RequestMapping("hello")

public class HelloController {

@Autowired

private HelloService helloService;

@RequestMapping("index")

public String index(){

return helloService.sayHello();

}

}

这里的 HelloService 就是 前面自定义 starter 里面的.

1. 结果: 未配置情况下, 应该是输出 hello world

2. 在配置文件中, 加入 hello.msg=hahahahahah

这个例子很简单, 只是显示一下主要的过程, 别的都是各插件自己的逻辑判断了.

参考资料:

javaEE开发的颠覆者 Spring Boot实战


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java swing框架实现的贪吃蛇游戏完整示例
下一篇:用户管理系统api(用户管理系统登录19216801)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~