"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"? ><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.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 package, if you do not use the parent class, you need to rely on these jars--<groupId>org.springframework.boot</groupId> <artifactid&gt ;spring-boot-starter-parent</artifactid> <version>1.1.3.RELEASE</version> </parent> & lt;dependencies> <!--Web program start-up dependencies, which can be introduced by this dependency into embedded Tomcat and other web jars--<dependency> &L T;groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <!--the startup item of the SPRING-DATA-JPA program depends on The underlying is hibernate, and if you don't use this framework you can rely on other ORM frames-<dependency> <groupid>org.springframework.boot&        Lt;/groupid> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--thymeleaf program is dependent on startup items, Spring-boot support for thymeleaf template engine is best, it is recommended that the template engine use this framework--<dependency> <g Roupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifact Id> </dependency> <!--mysql dependency, use SPRING-DATA-JPA to specify a database dialect to connect to the database, that is, MySQL driver--<d Ependency> <groupId>mysql</groupId> <artifactid>mysql-connector-java</artifa        ctid> </dependency> </dependencies> <dependencyManagement> <dependencies> </dependencies&Gt </dependencyManagement> <build> <plugins> <!--plug-ins built via Maven---& Lt;plugin> <groupId>org.springframework.boot</groupId> <artifactid>spri ng-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <!--Warehouse , using the Spring-boot release version requires these--<repositories> <repository> <id>spring-snapshot                S</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> &L t;/repository> </repositories> <pluginRepositories> <pluginRepository> <id >spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> &L T;pluginrepository> <id>spring-milestones</id> <url>http://repo.spring.io/miles Tone</url> </pluginRepository> </pluginRepositories></project>

  

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

  

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

  

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@componentscanpublic class Application {public    static void Main (string[] args) {        springapplication springapplication = new Springapplication (application.class);        Springapplication.run (args);}    }

  

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.

Note that because of the way annotations are used, the scan path needs to be configured, and Spring-boot uses the package that contains the boot manager as the root scan path. Will scan the packages and sub-packages they are in, so you need to put Application.java under the path, that is, com.test the package.

7. Then execute it for a moment.

Http://www.cnblogs.com/lic309/p/4073307.html

"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.