[Spring-boot] quickly builds the spring-boot microframework and spring-boot

Source: Internet
Author: User

[Spring-boot] quickly builds the spring-boot microframework and spring-boot

Spring-boot is a framework for rapid environment construction. Its design philosophy is to minimize xml configuration and simplify the initial setup and development process of new Spring applications. The framework uses a specific method for configuration, so that developers no longer need to define the configuration of the template.

Not to mention anything nonsense. Please refer to Baidu for details about spring-boot.

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

1. spring-boot is a mavan project, so all jar packages used are managed by maven. Of course, it is very convenient to use maven.

First, go to my project directory structure:

The package created by spring-boot is an executable jar package. It uses a built-in tomcat server, so you do not need to convert the project to an EJB project.

2. Set the pom. xml file

Anyone who has used maven knows that maven manages jar packages through the dependency of 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 basic parent class, which contains many necessary jar packages. If you do not use the parent class, you need to rely on these jars by yourself --> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-parent </artifactId> <version> 1.1.3.RELEASE </version> </parent> <dependencies> <! -- Dependency on the startup items of the web program. With this dependency, embedded tomcat and other necessary web jars can be introduced --> <dependency> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-web </artifactId> </dependency> <! -- The startup Item dependency of the spring-data-jpa program, which is implemented by hibernate at the underlying layer. If this framework is not used, it can be dependent on other orm frameworks --> <dependency> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-data-jpa </artifactId> </dependency> <! -- The startup Item dependency of the thymeleaf program. spring-boot provides the best support for the thymeleaf template engine. It is recommended that the template engine use this framework --> <dependency> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-thymeleaf </artifactId> </dependency> <! -- Mysql dependency. To use spring-data-jpa, you must specify a database dialect to connect to the database, mysql driver --> <dependency> <groupId> mysql </groupId> <artifactId> mysql-connector-java </artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> </dependencyManagement> <build> <plugins> <! -- Plug-ins built through maven --> <plugin> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> </plugin> </plugins> </build> <! -- Repository, use the spring-boot RELEASE version to require these --> <repositories> <repository> <id> spring-snapshots </id> <url> http://repo.spring.io/snapshot </url> <snapshots> <enabled> true </enabled> </snapshots> </repository> <id> spring-milestones </id> <url> http://repo.spring.io/milestone </url> </repository> </ repositories> <plugin> <pluginRepository> <id> spring-snapshots </id> <url> http://repo.spring.io/snapshot </url> </pluginRepository> <id> spring-milestones </id> <url> http://repo.spring.io/milestone </url> </pluginRepository> </pluginRepositories> </project>Pom. xml

 

3. Use maven update to download the jar package

4. Because we use the thymeleaf engine, this engine requires a templates folder to store static pages for jump to the foreground.

In resources, Add the folder to the corresponding page index.html (note: this folder must have an html page, otherwise thymeleaf startup Item will throw an exception)

5. Compile application. properties

This configuration file configures some spring-boot configurations. spring-boot configures some frameworks integrated in this file. The project structure shows that I have two application. properties files:

Application. properties: Master configuration file, which is directly read by spring-boot. Note: The configuration file must be placed under resources, that is, 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 some different configurations with the deployment environment, such as page cache and so on. This file is configured and read through the spring. profiles. active attribute of application. properties.

Code for the above two files:

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 # performancespring. 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 = 28700Application. 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 = trueApplication-dev.properties

6. The configuration is complete. Now, how can I start a web program using spring-boot?

The spring-boot package is an executable jar package. Of course, you can also compress it into an executable war package. The startup server does not need to start tomcat as before, started with java application

Use 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 );}}Application. java

First, explain the code in this file.

@ Configuration: Mark this file as a Configuration item

@ EnableAutoConfiguration: use automatic configuration

@ ComponentScan: scanable

SpringApplication: Start manager.

7. Just execute it.

 


How to Build the Srping framework in the Spring framework example?

This book aims to guide readers how to use the Spring framework to develop various effective applications (from simple Web applications to complex enterprise-level applications ). A complete sample application is provided throughout the book. Examples show how Spring provides a clear, layered architecture, how to use Spring to implement good OO design and architecture design, and how to apply best practices.
This book is intended for Java/J2EE architecture designers and developers who want to learn the Spring framework in depth, so that they can effectively use the Spring framework from simple Web applications to complex enterprise-level applications} is also very suitable for beginners of the Spring framework as a reference book.

How to build a spring framework in Eclipse

1. configuration file web. xml 2. applicationContext. xml
3. strues2 configuration file struts. xml 4. struts2-spring-plugin-2.0.11.jar

Related Article

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.