How to achieve a SpringBoot Starter

Starter is a very important concept in SpringBoot, Starter equivalent module, the module can integrate the required dependencies within the Bean and automatic configuration of modules based on conditions. Users only need to rely on the corresponding function Starter, do not need too much configuration and dependencies, SpringBoot can automatically scan and load the appropriate module, for example, when we create SpringBoot project, often introduced as spring-boot-starter-webthis dependence, the dependence of our do a lot of default configuration, no longer need to rely on spring-web, spring-webmvcand other related package configuration and make it able to use it immediately.

This article describes a simple case of how to define a Starter

First, write Starter

1, import-dependent

Here comes the SpringBoot compile replaced with the apache, or will be able to find the main class of Kazakhstan

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

2. Define a comment

The annotations marked when the method is executed, a log output

/**
 * @author Gjing
 **/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Print {
    
}

3, writing notes AOP process

Here by AOP to comment process, if not familiar with AOP, you can refer to my article: SpringBoot use AOP

/**
 * @author Gjing
 **/
@Aspect
@Component
public class PrintProcess {

    @Pointcut("@annotation(com.gjing.Print)")
    public void cut() {

    }

    @Around("cut()")
    public Object printCut(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法: " + method.getName() + " 开始执行, 当前时间: " + LocalDateTime.now());
        Object proceed = joinPoint.proceed();
        System.out.println("方法: " + method.getName() + " 执行结束, 当前时间: " + LocalDateTime.now());
        return proceed;
    }
}

4, write configuration class

/**
 * @author Gjing
 **/
@Configuration
public class PrintConfiguration {

    @Bean
    public PrintProcess printProcess() {
        return new PrintProcess();
    }
}

5, automatic assembly configuration

Create a folder in the resources META-INFpackage, and creates a spring.factoriesfile, as follows :
package
spring.factories document reads as follows :

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gjing.PrintConfiguration

Where =the right number for your configuration class, you need to specify the package name

Second, write a test project

1, import-dependent

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--这个为我们定义的starter-->
<dependency>
    <groupId>com.gjing</groupId>
    <artifactId>my-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

2, define an interface

We define the interfaces and use annotations starter on the method defined in

/**
 * @author Gjing
 **/
@RestController
public class TestController {

    @PostMapping("/test")
    @Print
    public String test() {
        return "ok";
    }
}

3, run the test

Start the project and test, you can see the output from the console log:
console

4, summary

As can be seen from the above example, when we use this annotation, did not do any additional configuration will be able to use it, which is what SpringBoot automatic assembly brings convenience, in the traditional Spring project, we often need and to manually configure some to Spring's IOC container management, which seemed a bit cumbersome and complicated.


This concludes the article, the article only cite a simple example, more high-level usage of you readers can explore on their own to go, this is not explained. If there are any delays this article, you can leave a message in the comments area. This article Demo Address: SpringBoot-Demo

Guess you like

Origin yq.aliyun.com/articles/709389