自定义一个springboot工程,删除启动类
- 自定义一个properties
//绑定前缀为customer的配置信息,并忽略未知的字段 @ConfigurationProperties(prefix = "customer", ignoreUnknownFields = true) public class CustomerProperties { private String ip; private String address; private String name; /*...getter and setter...*/ }
- 自定义一个配置类
- Customer 的配置信息
public class Customer { private String ip; private String address; private String name; private String other; /*...getter and setter...*/ }
- CustomerAutoConfiguration的配置信息
//@Configuration: 标明是一个配置类,proxyBeanMethods=false 表示不使用代理 @Configuration(proxyBeanMethods = false) //导入CustomerProperties这个配置类 @Import(CustomerProperties.class) public class CustomerAutoConfiguration { private CustomerProperties properties; //通过构造方法注入customerProperties public CustomerAutoConfiguration(CustomerProperties customerProperties) { this.properties = customerProperties; } /** 第一种:判断配置信息中是否有 customer.enable=true 的配置, 如果有,则getCustomer1执行 如果有,但是值不是true,则getCustomer1不执行 如果没有这个配置,则默认为false, getCustomer1也不执行 */ @Bean("customer") @ConditionalOnProperty(prefix = "customer", name = "enable", havingValue = "true", matchIfMissing = false) public Customer getCustomer1() { Customer customer = new Customer(); customer.setIp(properties.getIp()); customer.setAddress(properties.getAddress()); customer.setName(properties.getName()); customer.setOther("已经配置好的customer"); return customer; } /** 第二种:判断容器中是否存在Customer类型的实例, 如果不存在,则getCustomer2方法执行,像容器中注入一个customer实例 如果第一种配置生效了,容器中必然会存在Customer的实例,则第二种不生效 如果第一种配置不生效,容器中必然没有Customer实例,则则第二种生效 */ @Bean("customer") //判断容器中是否缺少Customer类型的实例 @ConditionalOnMissingBean(Customer.class) public Customer getCustomer2() { Customer customer = new Customer(); customer.setIp("127.0.0.1"); customer.setAddress("chengdu"); customer.setName("admin"); customer.setOther("默认的customer"); return customer; } }
-
在resource目录下新建META-INF目录,并且新增spring.properties文件
- 在spring.properties将配置的CustomerAutoConfiguration全路径配置进去,springboot在启动的时候就会把这个类写到EnableAutoConfiguration中,作为启动时加载的类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.thl.starter.config.CustomerAutoConfiguration
- 将写好的starter打成jar包,如果遇见报错, 则把pom中打包插件注释掉
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.4.5:repackage (repackage) on project starter: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.4.5:repackage failed: Unable to find main class
<!-- <build>--> <!-- <plugins>--> <!-- <plugin>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-maven-plugin</artifactId>--> <!-- <configuration>--> <!-- <excludes>--> <!-- <exclude>--> <!-- <groupId>org.projectlombok</groupId>--> <!-- <artifactId>lombok</artifactId>--> <!-- </exclude>--> <!-- </excludes>--> <!-- </configuration>--> <!-- </plugin>--> <!-- </plugins>--> <!-- </build>-->
- 在新的项目的pom.xml中引用自定义的stater依赖
<dependency> <groupId>com.thl</groupId> <artifactId>starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
- 在新项目中注入Customer
import com.thl.starter.config.Customer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyService { @Autowired private Customer customer; //注入Customer实例 public void show() { System.out.println("customer ------->" + customer); //打印Customer结果 } }
-
如果配置文件不增加对应的配置,输出结果:
customer ------->Customer(ip=127.0.0.1, address=chengdu, name=admin, other=默认的customer)
-
如果需要使配置生效的话,需要在application.properties中增加相应配置
customer.enable=true #启用customer配置 customer.name=zs #设置属性 customer.address=chengdu #设置属性 customer.ip=165.1.1.1 #设置属性
输出结果:
customer ------->Customer(ip=165.1.1.1, address=chengdu, name=zs, other=已经配置好的customer)