"Spring-boot" rapid construction of spring-boot micro-frame

Source: Internet
Author: User

Spring-boot is a set of frameworks for a fast-build environment that is designed to reduce the configuration of XML as much as possible to simplify the initial setup and development of new spring applications. The framework uses a specific approach to configuration, which eliminates the need for developers to define boilerplate configurations.

Nonsense not much to say, about Spring-boot is what specific please Baidu.

Official website: http://projects.spring.io/spring-boot

1. Spring-boot is a Mavan project, so the jar packages it uses are all managed through MAVEN, and of course it's very handy to use Maven.

First on my project directory structure:

Spring-boot The package is an executable jar package state, using the built-in Tomcat server, so you do not need to turn the project into an EJB project.

2. Setting up the Pom.xml file

Friends who have used Maven know that Maven manages the jar package by relying on the Pom file, so the core is the Pom.xml file.

    

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "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.lclc.boot</groupId>    <Artifactid>Boot-cache</Artifactid>    <version>0.0.1-snapshot</version>    <!--Inherit defaults from Spring Boot -    <Parent>        <!--Spring Boot Base parent class, which contains a lot of necessary jar packages, if you do not use the parent class, you need to rely on these jars -        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-parent</Artifactid>        <version>1.1.3.RELEASE</version>    </Parent>    <Dependencies>        <!--a Web program's startup item dependency, which can be introduced into embedded Tomcat and other web-required jars -        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-web</Artifactid>        </Dependency>        <!--SPRING-DATA-JPA the startup item dependency of the program, the underlying is hibernate implementation, if you do not use this framework can rely on other ORM framework -        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-data-jpa</Artifactid>        </Dependency>        <!--Thymeleaf the startup item dependency of the program, Spring-boot support for thymeleaf template engine is best, it is recommended that the template engine use this framework -        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-thymeleaf</Artifactid>        </Dependency>        <!--MySQL relies on using SPRING-DATA-JPA to specify a database dialect for connecting to the database, which is the MySQL driver -        <Dependency>            <groupId>Mysql</groupId>            <Artifactid>Mysql-connector-java</Artifactid>        </Dependency>    </Dependencies>    <dependencymanagement>        <Dependencies>        </Dependencies>    </dependencymanagement>    <Build>        <Plugins>            <!--plug-ins built with Maven -            <plugin>                <groupId>Org.springframework.boot</groupId>                <Artifactid>Spring-boot-maven-plugin</Artifactid>            </plugin>        </Plugins>    </Build>    <!--Warehouse, using the Spring-boot release version requires these -    <repositories>        <Repository>            <ID>Spring-snapshots</ID>            <URL>Http://repo.spring.io/snapshot</URL>            <Snapshots>                <enabled>True</enabled>            </Snapshots>        </Repository>        <Repository>            <ID>Spring-milestones</ID>            <URL>Http://repo.spring.io/milestone</URL>        </Repository>    </repositories>    <pluginrepositories>        <pluginrepository>            <ID>Spring-snapshots</ID>            <URL>Http://repo.spring.io/snapshot</URL>        </pluginrepository>        <pluginrepository>            <ID>Spring-milestones</ID>            <URL>Http://repo.spring.io/milestone</URL>        </pluginrepository>    </pluginrepositories></Project>
Pom.xml

3. Download the jar package using MAVEN update

4. Since we have used the Thymeleaf engine, this engine requires a templates folder to hold the static page for jumping to the foreground.

So add this folder under Resources and join a default page index.html (Note: There must be an HTML page in this folder, otherwise the Thymeleaf startup item will throw an exception)

5. Writing Application.properties

This configuration file is a configuration of spring-boot, and spring-boot this file to configure some of the frameworks that are integrated in it. As can be seen from the structure of my project, I have two application.properties files:

Application.properties: Master config file, spring-boot read this file directly. Note: The configuration file must be placed under resources, which is placed under the project root directory.

Application-dev.properties: Development environment configuration file, this is my development environment configuration file, in order to simplify some development, so need some different deployment environment configuration, such as page cache. This file is configured for read through the Spring.profiles.active property of Application.properties.

Code for the last two files:

The first is application.properties:

    

# profiles## Dev | Prod | testspring.profiles.active=dev# EMBEDDED SERVER CONFIGURATION (serverproperties) server.port= 8080server.session-timeout=30server.context-path=server.tomcat.max-threads=0server.tomcat.uri-encoding=utf-8# THYMELEAF (thymeleafautoconfiguration) spring.thymeleaf.encoding=utf-8# datasourcespring.datasource.initialize= falsespring.datasource.test-on-borrow=falsespring.datasource.test-on-return= falsespring.datasource.test-while-idle=truespring.datasource.max-wait-millis= 30000spring.datasource.validation-query=select 1spring.datasource.time-between-eviction-runs-millis= 20000spring.datasource.min-evictable-idle-time-millis=28700
application.properties

Then the application-dev.properties:

#page cachespring.thymeleaf.cache=false# DATASOURCE spring.datasource.platform=mysqlspring.datasource.url= Jdbc:mysql://localhost/test_development?useunicode=true&characterencoding=utf-8& Zerodatetimebehavior=converttonull&transformedbitisboolean= Truespring.datasource.username=rootspring.datasource.password=123456spring.datasource.driverclassname= com.mysql.jdbc.driverspring.datasource.max-active=5spring.datasource.max-idle=2spring.datasource.min-idle= 1spring.datasource.initial-size=1spring.datasource.initialize=false# jpaspring.jpa.hibernate.ddl-auto= Updatespring.jpa.show-sql=truespring.jpa.properties.hibernate.format_sql= Falsespring.jpa.properties.hibernate.use_sql_comments=true
application-dev.properties

6. So the configuration is done, now see how to start a Web program using Spring-boot

Spring-boot hit the package is an executable jar package, of course, can also be played into an executable war package, start the server does not need to get a tomcat as before to boot, completely Java application to start

Through the main method of a startup file

  

@Configuration @enableautoconfiguration@componentscan  Public class Application {    publicstaticvoid  main (string[] args) {        new springapplication (application.  Class);        Springapplication.run (args);}    }
Application.java

Let's start by explaining the code in this file.

@Configuration: Label This file as a configuration item

@EnableAutoConfiguration: Using Automatic configuration

@ComponentScan: Scan-able

Springapplication: Boot manager.

7. Then execute it for a moment.

"Spring-boot" rapid construction of spring-boot micro-frame

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.