一、说明
1、使用到的注解
@Configuration 表明是一个配置文件,被注解的类将成为一个 Bean 配置类
@ConditionalOnClass 当 classpath 下发现该字节码的情况下进行自动配置
@EnableConfigurationProperties 使 @ConfigurationProperties 注解生效
@Bean @ConditiononMissBean 完成自动配置后实例化这个 Bean
2、命令
命名归约-官方命名:
前缀:spring-boot-starter-xxx(比如:spring-boot-starter-web)
命名归约-自定义命名:
xxx-spring-boot-starter(比如:mybatis-spring-boot-starter)
二、实战
1、实战1:redis-spring-boot-starter�
1.1:添加maven 依赖
<!-- 想要让自定义配置项有提示,导入这个包,在项目编译后会生成元数据,同样是在META-INF目录下-->
<!-- 会自动生成spring-configuration-metadata.json文件。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
1.2:属性类
用于获取 application.yml 中前缀为 custom 的配置信息。
package com.example.authorspringbootstarter.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Created with IntelliJ IDEA.
*
* @Author: geek
* @Date: 2023/08/23/9:41 AM
* @Description: 用于获取配置文件前缀为custom的value值
*/
@ConfigurationProperties(prefix = "custom")
public class AuthorProperties {
public static final String DEFAULT_AUTHOR = "Geek";
public String author = DEFAULT_AUTHOR;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
1.3:Service 类
package com.example.authorspringbootstarter.starter;
/**
* Created with IntelliJ IDEA.
*
* @Author: geek
* @Date: 2023/08/23/9:45 AM
* @Description:
*/
public class AuthorServer {
public String author;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
1.4:自动配置类
package com.example.authorspringbootstarter.starter;
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.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created with IntelliJ IDEA.
*
* @Author: geek
* @Date: 2023/08/23/9:45 AM
* @Description:
*/
@Configuration
@ConditionalOnClass(AuthorServer.class)
@EnableConfigurationProperties(AuthorProperties.class)
public class AuthorAutoConfiguration {
@Autowired
private AuthorProperties authorProperties;
@Bean
@ConditionalOnMissingBean(AuthorServer.class)
public AuthorServer authorResolver() {
AuthorServer authorServer = new AuthorServer();
authorServer.setAuthor(authorProperties.getAuthor());
return authorServer;
}
}
1.5:spring.factories
在 resources 目录下新建 META_INF 文件夹,并在其中新建 spring.factories,在其中写入:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.authorspringbootstarter.starter.AuthorAutoConfiguration
至此,Starter 项目创建完成,使用 mvn install 命令打包。
1.6:测试 Starter
引入starter 依赖
<dependency>
<groupId>com.example</groupId>
<artifactId>author-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
配置 application.yml 文件
custom:
author: Geek001
测试接口
package com.example.springbootdemo.controller;
import com.example.authorspringbootstarter.starter.AuthorServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
*
* @Author: geek
* @Date: 2023/08/23/10:13 AM
* @Description:
*/
@RestController
@RequestMapping("/testController")
public class TestController {
@Autowired
private AuthorServer authorServer;
@GetMapping("/author")
public String author() {
return authorServer.getAuthor();
}
}
2、实战2:author-spring-boot-starter�
2.1:添加maven 依赖
<!-- 想要让自定义配置项有提示,导入这个包,在项目编译后会生成元数据,同样是在META-INF目录下-->
<!-- 会自动生成spring-configuration-metadata.json文件。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
2.2:属性类
package com.example.redisspringbootstarter.config.redis;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Created with IntelliJ IDEA.
*
* @Author: geek
* @Date: 2023/08/22/9:30 AM
* @Description:
*/
@ConfigurationProperties(prefix = "redis")
public class JedisProperties {
private String host;
private int port;
/**
* Login password of the redis server.
*/
private String password;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.3:自动配置类
package com.example.redisspringbootstarter.config.redis;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
/**
* Created with IntelliJ IDEA.
*
* @Author: geek
* @Date: 2023/08/22/9:32 AM
* @Description:
*/
@Configuration
@ConditionalOnClass(Jedis.class)
@EnableConfigurationProperties(JedisProperties.class)
public class JedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Jedis jedis(JedisProperties redisProperties) {
Jedis jedis = new Jedis(redisProperties.getHost(), redisProperties.getPort());
jedis.auth(redisProperties.getPassword());
return jedis;
}
}
2.4:spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.redisspringbootstarter.config.redis.JedisAutoConfiguration
2.5:测试 Starter
引入starter 依赖
<dependency>
<groupId>com.example</groupId>
<artifactId>author-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
配置 application.yml 文件
redis:
host: 1.XXX.43.XXX
port: 6379
password: XXXX
测试接口
package com.example.springbootdemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;
/**
* Created with IntelliJ IDEA.
*
* @Author: geek
* @Date: 2023/08/22/9:50 AM
* @Description:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class JedisTest {
@Autowired
Jedis jedis;
@Test
public void show() {
//jedis.auth("123456");
System.out.println(jedis.ping());
jedis.set("starter", "myJedis");
System.out.println(jedis.get("starter"));
}
}
三、异常解决
1、Maven 编译:Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date]
解决方法1:如果只是希望避开这个错误,进行打包测试,可以使用下面的方法
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin><!--编译跳过测试文件检查的生命周期-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
解决方法2:
可以点开查看具体的报错,有针对的解决。