0
点赞
收藏
分享

微信扫一扫

SpringBoot核心-自定义starter


文章目录

  • ​​1.自定义starter​​
  • ​​1.1.IDEA中创建maven项目​​
  • ​​1.2.配置依赖​​
  • ​​1.3.属性配置类​​
  • ​​1.4.判断依据类​​
  • ​​1.4.自动配置类​​
  • ​​1.5.注册配置​​
  • ​​2.使用自定义的starter​​
  • ​​2.1.创建好SpringBoot项目​​
  • ​​2.2.引入我们自定义的starter​​
  • ​​3.查看引入的具体依赖​​
  • ​​4.工具类中使用​​
  • ​​5.启动测试​​


为了加深对SpringBoot中自动装配的理解,我们自定义一个starter来实现,具体步骤如下

1.自定义starter

1.1.IDEA中创建maven项目

SpringBoot核心-自定义starter_maven项目
指定项目的坐标信息
SpringBoot核心-自定义starter_spring boot_02
SpringBoot核心-自定义starter_spring boot_03

1.2.配置依赖

在pom配置文件中添加如下依赖,增加SpringBoot自身的自动配置作为依赖。

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
</dependencies>

1.3.属性配置类

package com.bruceliu.service;

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

@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;
}
}

1.4.判断依据类

package com.bruceliu.service;

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

1.4.自动配置类

package com.bruceliu.service;

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;

@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;
}
}

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

1.5.注册配置

若想自动配置生效,我们需要注册自动配置类,在​​src/main/resources​​​下新建​​META-INF/spring.factories​​,如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.bruceliu.service.HelloServiceAutoConfiguration

如果有多个自动配置,则用“,”隔开。

2.使用自定义的starter

2.1.创建好SpringBoot项目

创建好一个SpringBoot项目。

2.2.引入我们自定义的starter

<dependency>
<groupId>com.bruce.myhello</groupId>
<artifactId>spring-boot-starter-myhello</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

3.查看引入的具体依赖

SpringBoot核心-自定义starter_自定义_04

4.工具类中使用

@RestController
public class HelloStarterController {
@Resource
HelloService helloService;

@RequestMapping("/")
public String index(){
return helloService.sayHello();
}
}

5.启动测试

访问:http://localhost:8082/
SpringBoot核心-自定义starter_java_05
然后我们在​​​application.properties​​中配置如下内容

hello.msg=SpringBoot

再次启动访问测试结果!

ok~自定义的starter搞定


举报

相关推荐

0 条评论