自定义starter
自定义类
package com.myboot;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "spring.person")
public class PersonProperties {
// 姓名
private String name;
// 年龄
private int age;
// 性别
private String sex = "M";
// Getter & Setter
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getSex() {
return sex;
}
}
package com.myboot;
public class PersonService {
private PersonProperties properties;
public PersonService() {
}
public PersonService(PersonProperties properties) {
this.properties = properties;
}
public void sayHello(){
System.out.println("大家好,我叫: " + properties.getName() + ", 今年" + properties.getAge() + "岁"
+ ", 性别: " + properties.getSex());
}
}
package com.myboot;
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
//ConfigurationProperties的辅助类,用于在容器中生成bean
@EnableConfigurationProperties(PersonProperties.class)
//判断类是否存在,如果存在装配到容器中,即生成bean
@ConditionalOnClass(PersonService.class)
//判断配置是否有效,即装配开关
@ConditionalOnProperty(prefix = "spring.person", value = "enabled", matchIfMissing = true)
public class PersonServiceAutoConfiguration {
@Autowired
private PersonProperties properties;
@Bean
@ConditionalOnMissingBean(PersonService.class) // 当容器中没有指定Bean的情况下,自动配置PersonService类
public PersonService personService() {
PersonService personService = new PersonService(properties);
return personService;
}
}
解释:
@EnableConfigurationProperties:使ConfigurationProperties生效,并且将关联的类实例化放在容器中
@Conditional(TestCondition.class)
这句代码可以标注在类上面,表示该类下面的所有@Bean都会启用配置,也可以标注在方法上面,只是对该方法启用配置。
@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)
@ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
@ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
@ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
@ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
@ConditionalOnNotWebApplication(不是web应用)
@ConditionalOnProperty:使@Configuration生效
编写启动配置文件
/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.myboot.PersonServiceAutoConfiguration
解释:
spring.factories的作用:
springboot在初始化的时候SpringFactoriesLoader会去加载spring.factories中的工厂类的实现方法
pom 文件
<?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.mybootstarter</groupId>
<artifactId>my-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.6.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
测试项目
在测试项目中引入此GAV
测试类如下
package com.mystarter.test.myspringbootstartertest;
import com.myboot.PersonService;
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 javax.annotation.Resource;
@SpringBootApplication
@RestController
@RequestMapping("starter")
public class MySpringbootStartertestApplication {
@Resource
private PersonService personService;
public static void main(String[] args) {
SpringApplication.run(MySpringbootStartertestApplication.class, args);
}
@RequestMapping("t")
public Object starterT() {
personService.sayHello();
return null;
}
}
网友评论