Custom starter

Posted Jun 5, 20202 min read

Custom starter naming rules

The starter provided by SpringBoot is named after `spring-boot-starter-xxx`. Officially recommend that custom starters use the naming rules of `xxx-spring-boot-starter`. To distinguish the starter provided by the SpringBoot ecosystem

The whole process is divided into two parts:

- Custom starter

- Use starter

First, complete the custom starter

(1) New maven jar project, the project name is zdy-spring-boot-starter, import dependencies:

xml

org.springframework.boot

spring-boot-autoconfigure

2.2.2.RELEASE

(2) Write javaBean

java

@EnableConfigurationProperties(SimpleBean.class)

@ConfigurationProperties(prefix = "simplebean")

public class SimpleBean {

private int id;

private String name;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return "SimpleBean{" +

"id=" + id +

", name='" + name +'\'' +

'}';

}

}

(3) Write configuration class MyAutoConfiguration

java

@Configuration

@ConditionalOnClass //@ConditionalOnClass:Automatic configuration when there is a specified class in the classpath classpath

public class MyAutoConfiguration {

static {

System.out.println("MyAutoConfiguration init....");

}

@Bean

public SimpleBean simpleBean(){

return new SimpleBean();

}

}

(4) Create /META-INF/spring.factories under resources

Note:META-INF is a manually created directory, spring.factories is also a manually created file, configure your own automatic configuration class in this file

image-20200111123116471

factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.lagou.config.MyAutoConfiguration

I just learned the "Java Engineer High Salary Training Camp" of Lagou Education, and I answered when I saw what I just learned. I hope Lagou can push me to the company I want to go to. Goal:byte! !