is implemented by itself. When a class exists, the bean is automatically configured, and this property can be configured in application.properties.
Create a new maven project (need to introduce spring-boot-autoconfigure )
Pom. xml
<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.ibigsea</groupId> <artifactId>spring-boot-starter-hello</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-starter-hello</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.3.3.RELEASE</version> </dependency> </dependencies> </project>
package com.ibigsea.spring_boot_starter_hello; /** * Configured by hello.msg of application.properties, the default is world * @author bigsea * */ public class Hello { private String msg; public String sayHello() { return "hello " + msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
HelloProperties.java
package com.ibigsea.spring_boot_starter_hello; import org.springframework.boot.context.properties.ConfigurationProperties; /** * Property configuration, Spring boot's own automatic configuration * @author bigsea * */ @ConfigurationProperties(prefix="hello") public class HelloProperties { private static final String MSG = "world"; private String msg = MSG ; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
HelloAutoConfiguration.java
package com.ibigsea.spring_boot_starter_hello; 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(HelloProperties.class)//Enable property injection, injected through @autowired @ConditionalOnClass(Hello.class)//This class will only instantiate an object if it exists in the classpath //When hello=enabled is set, if it is not set, the default is true, that is, the condition is met @ConditionalOnProperty(prefix="hello", value="enabled", matchIfMissing = true) public class HelloAutoConfiguration { @Autowired private HelloProperties helloProperties; @Bean//Configure this class using java configuration @ConditionalOnMissingBean(Hello.class)//If there is no Hello class instance in the container, then this Hello is automatically configured public Hello hello() { Hello hello = new Hello(); hello.setMsg(helloProperties.getMsg()); return hello; } }
and to add spring.factories
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.ibigsea.spring_boot_starter_hello.HelloAutoConfiguration
The entire project structure
Well , here we have completed a starter project, let's test
Pom.xml by ourselves
<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.ibigsea</groupId> <artifactId>test-starter</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>test-starter</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <boot.version>1.3.3.RELEASE</boot.version> </properties> <dependencies> <dependency> <groupId>com.ibigsea</groupId> <artifactId>spring-boot-starter-hello</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${boot.version}</version> <scope>test</scope> </dependency> </dependencies> </project>
App.java
package com.ibigsea.test_starter; 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 com.ibigsea.spring_boot_starter_hello.Hello; @SpringBootApplication @RestController public class App { @Autowired private Hello hello; @RequestMapping("/") public String index() { return hello.sayHello(); } public static void main(String[] args) { SpringApplication.run(App.class, args); } }
Reprinted from: https://blog.csdn.net/a67474506/article/details/52013634