Spring Boot Custom Starter

Source: Internet
Author: User
Tags bind xmlns tomcat

In the Springboot, the most used is starter. Starter can be understood as a pluggable plug-in, for example, if you want to use the JDBC plugin, you can use Jdbc-starter, and if you want to use Mongo-starter, you can use Mongo-starter.

As below, a simple springboot starter dependent,

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId> spring-boot-starter-parent</artifactid>
        <version>1.4.1.RELEASE</version>
    </parent >
    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid >
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency >
            <groupId>org.springframework.boot</groupId>
            <artifactId> spring-boot-starter-log4j2</artifactid>
        </dependency>
        <dependency>
            < Groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</ artifactid>
        </dependency>
    </dependencies>

Among them, rely on the starter-log4j2 and starter-web two plug-ins. More on how to use Springboot can go to the spring website to learn more about the use.

Above the springboot in the starter in the basis of use, the following oneself define a starter.

Demand:
Customize a starter project named User-starter, create a boss project from Springboot, and invoke the function in User-starter in the project, and output Hello starter. The first step

Create a MAVEN project boss, how to create I don't say much, unfamiliar can Baidu create Maven project, we focus on the second step. Part II

First, rely on Pom.xml in the following

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.eju.ess</groupId> <artifactid>user-sta

    Rter</artifactid> <version>0.0.2-SNAPSHOT</version> <packaging>jar</packaging> <name>user-starter</name> <url>http://maven.apache.org</url> <properties> & Lt;project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> <dependenci Es> <!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-<dependen Cy> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot</a Rtifactid> <version>1.4.3.

 release</version> </dependency> </dependencies> </project>

Because starter projects need to use some of the features of spring, at least one dependency on org.springframework.boot is required in the starter project.

Next, create a MAVEN project User-starter, create the UserService class, and the code is as follows

UserService

Package com.eju.ess;

public class UserService {public
    void Hellostarter () {
        System.out.println (">> Hello starter");
    }
}

Create a Userautoconfiguration class with the following code

Userautoconfiguration

Package com.eju.ess;

Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;

@Configuration public
class Userautoconfiguration {
    @Bean public
    userservice Dbcountrunner () {
        return new UserService ();
    }
}

= = Then create the Meta-inf folder in the resources directory and create the spring.factories file = = In the Meta-inf folder, which is used to tell spring boot to find the specified automatic configuration file. During the application startup process, the spring Boot uses the Springfactoriesloader class loader to find the Java configuration file that corresponds to the Org.springframework.boot.autoconfigure.enableautoconfiguration keyword. Spring boot iterates through the Spring.factories file in the Meta-inf directory of each jar package and builds it into a list of profiles.

Spring.factories content is as follows

Org.springframework.boot.autoconfigure.enableautoconfiguration=com.eju.ess.userautoconfiguration
Part III

Rely on the Boss project to add the following code to the POM

<dependency>
            <groupId>com.eju.ess</groupId>
            <artifactid>user-starter</ artifactid>
            <version>0.0.2-SNAPSHOT</version>
        </dependency>

Use @autowired in the controller to inject, the code is as follows,

Package Com.banana;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;

Import Com.eju.ess.UserService;

Import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController Public
class Samplecontroller {

    @Autowired
    private userservice t=null;

    @RequestMapping ("/")
    String Home () {

        t.hellostarter ();

        Return "Hello world!";
    }
}

Then start, springboo program, Access http://127.0.0.1:8080/in console input as follows

15:04:29.148 [Restartedmain] INFO  org.apache.tomcat.util.net.nioselectorpool-using a shared selector for servlet Write/read
15:04:29.186 [Restartedmain] INFO  Org.springframework.boot.context.embedded.tomcat.tomcatembeddedservletcontainer-tomcat started on port (s): 9900 ( HTTP)
15:04:29.195 [Restartedmain] INFO  com.banana.startup-started Startup in 4.519 seconds (JVM running for 5.4 (+)
15:04:37.260 [http-nio-9900-exec-1] INFO  org.apache.catalina.core.containerbase.[ Tomcat]. [localhost]. [/]-Initializing Spring frameworkservlet ' dispatcherservlet '
15:04:37.261 [http-nio-9900-exec-1] INFO  Org.springframework.web.servlet.dispatcherservlet-frameworkservlet ' Dispatcherservlet ': initialization started
15:04:37.277 [http-nio-9900-exec-1] INFO  Org.springframework.web.servlet.DispatcherServlet- Frameworkservlet ' Dispatcherservlet ': initialization completed in MS
>> Hello Starter

You can see the requirements in front of us Hello starter has been output to the console.

Article reference:

http://www.jianshu.com/p/85460c1d835a

= = Mobile QQ Scan the QR code below, quickly join the Java Architect Exchange Group = =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.