SpringBoot entry (final) ------ custom starter

After the first few of learning and understanding, we can customize a starter for springboot, allowing springboot project do something we want to do when you start (such as listening ports, etc.)

Specific implementation steps:

1. Create a new MAVEN project
2.pom file dependencies must be introduced (if you need additional features, the need to introduce other dependencies, such as databases, etc.), as well as inheritance springboot

Here groupId my maven project, artifactId, version for the

    <groupId>org.jym</groupId>
    <artifactId>com-jym-starter-test</artifactId>
    <version>1.0-SNAPSHOT</version>

Need to inherit:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>

We need to be introduced:

    <dependencies>
        <!-- springboot自定义starter规范 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>
3. Then write business code, here to write a simple output statement:

Project structure:
Here Insert Picture Description
Let me talk to configure categories:
@ConfigurationProperties Note: Description of such maintenance property class, attributes can be configured in application.properties, the value of the annotation path, for example: com.jym.name = 12138

package com.jym.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @program: com-jym-starter-test
 * @description: 配置属性的类,开发者可在springboot配置文件里配置
 * @author: jym
 * @create: 2020/01/24
 */
@ConfigurationProperties("com.jym")
public class JymProperties {

    private String name = "jym";  // 默认值为jym

    public String getName() {
        return name;
    }

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

}

interface:

package com.jym;

/**
 * @program: com-jym-starter-test
 * @description: 一个测试接口
 * @author: jym
 * @create: 2020/01/24
 */
public interface JymSayHello {

    void sayHello();
}

Implementing Classes:

package com.jym.impl;

import com.jym.JymSayHello;
import com.jym.properties.JymProperties;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @program: com-jym-starter-test
 * @description: 对JymSayHello的实现
 * @author: jym
 * @create: 2020/01/24
 */
public class JymSayHelloImpl implements JymSayHello {

    @Autowired
    private JymProperties jymProperties;

    @Override
    public void sayHello() {
        String name = jymProperties.getName();
        System.out.println(name+": hello,springboot");
    }
}

Imitate springboot source process, write configuration categories:

package com.jym.configuration;

import com.jym.JymSayHello;
import com.jym.impl.JymSayHelloImpl;
import com.jym.properties.JymProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: com-jym-starter-test
 * @description: 配置类,需要在springboot启动时自动加载进去
 * @author: jym
 * @create: 2020/01/24
 */
// 说明配置类
@Configuration
// 条件注解,只有在classpath下指定class文件时,才被实例化bean
@ConditionalOnMissingClass
// 可以通过spring.properties配置属性
@EnableConfigurationProperties({JymProperties.class})
public class JymAutoConfiguration {

    // 将JymSayHelloImpl注入进IOC容器里,方法名为BeanId
    @Bean
    // 条件注解,意思是:仅当ioc容器中不存在指定类型bean时,才会创建
    @ConditionalOnMissingBean
    public JymSayHello jymSayHello (){
        return new JymSayHelloImpl();
    }

}

In the source process, there will be spring.factories file, so we need to maintain our class configuration file into spring.factories:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.jym.configuration.JymAutoConfiguration
4. When finished package need to use MAVEN, MAVEN local warehouse or not, an error will not lead

carried outmvn clean install
Here Insert Picture Description
Then to complete the custom starter! !

5. Go to the need to introduce the project springboot starter project, adding a dependency in the pom file:

You need to enter the correct groupId, artifactId, version

        <!-- 引入自定义starter -->
        <dependency>
            <groupId>org.jym</groupId>
            <artifactId>com-jym-starter-test</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
6. disposed application.properties name attribute, if not configured, the default value
com.jym.name=12138

Test code:

package com.jym.demo;

import com.jym.JymSayHello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author Administrator
 */
@SpringBootApplication()
public class DemoApplication implements CommandLineRunner {

    @Autowired
    private JymSayHello jymSayHello;

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

    public void run(String... args) {
        jymSayHello.sayHello();
    }
}

After starting the console successfully print:
Here Insert Picture Description

Note: The configuration property is available only application.properties, you can not use yml files, or the configuration fails! ! !

There are 10 kinds of people in the world, one is to understand binary, one is do not understand binary.

Today is New Year's Eve, I wish you all a Happy New Year, next year a little less bug, pay a little more

Published 71 original articles · won praise 54 · views 420 000 +

Guess you like

Origin blog.csdn.net/weixin_43326401/article/details/104081432