Spring Boot Custom Start Component Development

The core of Start component development is the annotation order of automatic annotation classes, that is, annotation according to conditions.

First, let's understand the @Configuration, @Value, @Bean annotations

Take the DataSourceAutoConfiguration class annotation as an example.
@ Configuration table name class is a configuration class.
@ Conditional OnClass indicates that the following configuration will continue when DataSource. class, Embedded DatabaseType. class exist; @Enable Configuration Properties retrieves the Bean of the application.properties configuration file from the IOC container;
@ Import imports related classes.

@ The Configuration Properties annotation is mainly used to convert properties configuration files into beans for use, while the @Enable Configuration Properties annotation is used to validate the @Configuration Properties annotation. If you configure only the @Configuration Properties annotation, you won't get the beans converted by the properties configuration file in the IOC container.
@ Enable Configuration Properties

Autoloading Core Annotations

With the above understanding, to create a Maven project, the directory structure is as follows:

Add pom.xml dependencies (based on component functionality)

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.0.0.RELEASE</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

When using Spring's official Starter, you can usually configure parameters in application.properties to override default values. That is, the value of sex is replaced by the value in the configuration file.
PersonProperties class

@ConfigurationProperties(prefix = "spring.person")
public class PersonProperties {
    // Full name
    private String name;
    // Age
    private int age;
    // Gender
    private String sex = "M";

    // Getter & Setter
    }

PersonService class

public class PersonService {

    private PersonProperties properties;

    public PersonService() {
    }

    public PersonService(PersonProperties properties) {
        this.properties = properties;
    }

    public void sayHello(){
        System.out.println("Hello, everyone. My name is: " + properties.getName() + ", This year" + properties.getAge() + "year"
                + ", Gender: " + properties.getSex());
    }
}

PersonService AutoConfiguration class

@Configuration 
@EnableConfigurationProperties(PersonProperties.class)
@ConditionalOnClass(PersonService.class)
@ConditionalOnProperty(prefix = "spring.person", value = "enabled", matchIfMissing = true)
public class PersonServiceAutoConfiguration {

    @Autowired
    private PersonProperties properties;

    @Bean
    @ConditionalOnMissingBean(PersonService.class)  // Automatically configure the PersonService class when no Bean is specified in the container
    public PersonService personService(){
        PersonService personService = new PersonService(properties);
        return personService;
    }
}

spring.factories file
Note: META-INF is a directory that you create manually, and spring.factories is also a file that you create manually, in which you configure your own automatic configuration class.

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.cnbi.PersonServiceAutoConfiguration

Finally, pack the project into mvn clean install

Adding dependencies to another project How to add local jar packages to a project

		<dependency>
			<groupId>cnbi</groupId>
			<artifactId>helloworld-spring-boot-starter</artifactId>
			<version>1.0</version>
		</dependency>

Configure application.properties

spring.person.age=23
spring.person.name=ss
spring.person.sex=F

Start the test class

@RunWith(SpringRunner.class)
@SpringBootTest
public class LarkApplicationTests {
	@Autowired
	@SuppressWarnings("ALL")
	private PersonService personService;

	@Test
	public void testHelloWorld() {
		personService.sayHello();
	}

}

2019-09-11 09:25:19.453 INFO 7272 — [ main] com.tiamo.lark.LarkApplicationTests : Started LarkApplicationTests in 4.893 seconds (JVM running for 6.7)

Hello everyone, my name is ss, 23 years old, gender: F

Keywords: Spring Maven xml jvm

Added by BLINDGUY on Wed, 11 Sep 2019 06:57:36 +0300