Proficient in Spring Boot -- Part 5: write a Spring Boot starter package

Keywords: Spring Maven SpringBoot xml

In order to better understand the automatic configuration and working principle of spring boot, let's write a spring boot Hello starter today. This process is very simple, not much code. Let's see how to start. ##1. New maven project. This is not a demonstration. If you can't do it yourself, baidu... Lala, because it's too simple

2. Create a new properties class

/**
 * @author Lee
 * @// TODO 2018/7/25-9:21
 * @description
 */
@ConfigurationProperties(prefix = "customer")
public class CustomerProperties {
    private static final String DEFAULT_NAME = "Lensen";

    private String name = DEFAULT_NAME;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2. Create a service class, CustomerService

/**
 * @author Lee
 * @// TODO 2018/7/25-10:30
 * @description
 */
public class CustomerService {
    private String name;

    public String findCustomer(){
        return "The Customer is " + name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

3.AutoConfiguration class

/**
 * @author Lee
 * @// TODO 2018/7/25-10:32
 * @description
 */
@Configuration
@EnableConfigurationProperties(CustomerProperties.class)
@ConditionalOnClass(CustomerService.class)
@ConditionalOnProperty(prefix = "customer", value = "enabled", matchIfMissing = true)
public class CustomerAutoConfiguration {

    @Autowired
    private CustomerProperties customerProperties;

    @Bean
    @ConditionalOnMissingBean(CustomerService.class)
    public CustomerService customerService() {
        CustomerService customerService = new CustomerService();
        customerService.setName(customerProperties.getName());
        return customerService;
    }
}

##4. spring.factories configuration

Create a new folder META-INF in src/main/resources, and then create a new spring.factories file


org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.developlee.configurer.CustomerAutoConfiguration

The pom file changes the artifactId to spring boot Hello starter and packages it into jar. Then use mvn install to install it to the local mvn warehouse. ##5. Test spring boot Hello start Create a new springboot project and import the installed jar package in pom.xml file

	<dependency>
		<groupId>com.developlee</groupId>
		<artifactId>spring-boot-hello-starter</artifactId>
		<version>1.0-SNAPSHOT</version>
	</dependency>

Test directly in the project startup class:

@SpringBootApplication
@RestController
public class TestStarterApplication {

	@Autowired
	CustomerService customerService;

	@GetMapping("/")
	public String index(){
		return customerService.getName();
	}

	public static void main(String[] args) {
		SpringApplication.run(TestStarterApplication.class, args);
	}
}

Our customer.name under the application.properties file configuration

customer.name = BigBBrother

Next, start the project, request the address localhost:8080, and you can see that bigbbother is displayed on the page.

So far, we have completed the development of a spring boot starter jar package. There are many functions that we can encapsulate into a jar by ourselves. In the future, we need to use direct reference to do so. Is it a different experience to write code like this?

Posted by novicephp on Fri, 20 Dec 2019 09:15:18 -0800