Custom starter for springboot


1、 Springboot starter mechanism

Unified management, unified introduction, reducing cumbersome configuration and resolving version conflicts

2、 Custom starter

1. There are many configuration modules independent of business in development, which can be used when referenced repeatedly in POM.

For example:

  • Dynamic data source.
  • Login module.
  • Realize log facet based on AOP technology.

2. Customize the naming rules of starter

  • The starter provided by springboot is named spring boot starter XXX.
  • It is officially recommended that XXX spring boot starter naming rules be used for customized starters. To distinguish the springboot ecosystem
    New starter

3. Realization

3.1 create a maven jar project named ldz spring boot starter. The import depends on:


Automatic configuration of core dependencies

3.2 writing JavaBean entities

@ConfigurationProperties(prefix = "simplebean")
public class SimpleBean {
      private int id;
      private String name;
      public int getId() {
            return id;
      public void setId(int id) {
   = id;
      public String getName() {
            return name;
      public void setName(String name) {
   = name;
      public String toString() {
            return "SimpleBean{" +
            "id=" + id +
            ", name='" + name + '\'' +

General configuration class entity (configure the called attribute value in the called project)

3.3 write configuration class myautoconfiguration

public class MyAutoConfiguration {
      static {
            System.out.println("MyAutoConfiguration init....");
      public SimpleBean simpleBean(){
            return new SimpleBean();

Instantiate the class of general java bean. When instantiating this class, the result of @ bean method will be put into IOC container as spring bean and managed by spring

3.4 create / meta-inf / under resources factories

Meta-inf is a manually created directory, spring Factories is also a manually created file in which self configuration is configured
Auto configuration class


Spring will start according to key: org springframework. boot. autoconfigure. Enableautoconfiguration load/META-INF/spring.factoriesThen instantiate and execute the @ bean method to instantiate the configuration bean.

4. Use custom starter

4.1 dependency of importing custom starter


4.2 configure attribute values in the global configuration file
simplebean. Name = custom starter

4.2 testing

//Test custom starter
      private SimpleBean simpleBean;
      public void zdyStarterTest(){

3、 If one day we don’t want to start the project, we will automatically assemble simplebean

1. Delete POM Dependency in XML (low)

2. Hot plug technology

We often add @ enablexxx annotation on the startup class application. In fact, this @ enablexxx annotation is a hot plug technology. Adding this annotation can start the corresponding starter. When the corresponding starter is not needed, we only need to comment out this annotation.

2.1 hot plug support added in the transformation ldz project

2.1.1 add a marker configmarker

   public class ConfigMarker {

2.1.2 new enableregisterserver annotation

    public @interface EnableRegisterServer {

2.1.3 transformation of myautoconfiguration

Add condition annotation @ conditionalonbean (configmarker. Class),
@Conditionalonbean is a conditional annotation. The previous meaning means that only if the current context contains a configmarker object, the marked class will be instantiated.

public class MyAutoConfiguration {
        static {
        System.out.println("MyAutoConfiguration init....");
        public SimpleBean simpleBean(){
        return new SimpleBean();

2.1.4 transformation and transfer project

Add @ enableimregisterserver annotation on the startup class

Custom starter for springboot


When @ enableimregisterserver is added, the simplebean will be automatically assembled when the zdy project is started. Otherwise, it will not be assembled.

2.1.4 principle

When the @ enableimregisterserver annotation is added, because the annotation uses @ import ({configmarker. Class}), it will cause spring to load configmarker into the context, and because of the existence of the conditional annotation @ conditionalonbean (configmarker. Class), the myautoconfiguration class will be instantiated.

3 explanation of condition notes

  • @Conditionalonbean: a bean is instantiated only when an object exists in the current context.
  • @Conditionalonclass: a bean will only be instantiated if a class is on the class path.
  • @Conditionalonexpression: when the expression is true, a bean will be instantiated. Based on spiel table
    Conditional judgment of expression.
  • @Conditionalonmissingbean: an object is instantiated only if it does not exist in the current context
  • @Conditionalonmissingclass: a bean will be instantiated only when a class does not exist on the class path.
  • @Conditional onnotwebapplication: a bean will be instantiated only if it is not a web application.
  • @Conditionalonwebapplication: instantiates when the project is a web project.
  • @Conditionalonproperty: instantiates when the specified property has a specified value.
  • @Conditionalonjava: triggers instantiation when the JVM version is within the specified version range.
  • @Conditionalonresource: trigger instantiation when there is a specified resource under the classpath.
  • @Conditionalonjndi: trigger instantiation when JNDI exists.
  • @Conditionalonsinglecandidate: when there is only one specified bean in the container, or there are multiple specified beans
    Trigger instantiation when the preferred bean is.