spring-boot is a framework for quickly building environments. Its design concept is to minimize the configuration of xml as much as possible, and it is used to simplify the initial construction and development process of new Spring applications. The framework uses a specific way to configure it, so that developers no longer need to define boilerplate configurations.
Without further ado, please Baidu regarding what spring-boot is.
Official website: http://projects.spring.io/spring-boot
1. spring-boot is a mavan project, so all the jar packages it uses are managed through maven. Of course, it is also very convenient to use maven.
First, let’s go to my project directory structure:
The package produced by spring-boot is an executable jar package, using the built-in tomcat server, so there is no need to convert the project into an EJB project.
2. Set the pom.xml file
Friends who have used maven know that maven manages jar packages through the dependency of pom file, so the core is also 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 yourself --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.3.RELEASE</version> </parent> <dependencies> <!-- The start-up item dependency of the web program, through which the required jars for web such as tomcat can be introduced --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- The start-up item dependency of the spring-data-jpa program is implemented in hibernate. If you do not use this framework, you can rely 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 supports 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. Using spring-data-jpa requires specifying a database dialect for connecting to the database, that is, mysql driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> </dependencies> </dependencies> </dependencyManagement> <build> <plugins> <!-- Plugins built through maven--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- Repository, these are required using spring-boot RELEASE version--> <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> </repository> </repository> <pluginRepository> <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>
3. Use maven update to download the jar package
4. Since we use the thymeleaf engine, this engine needs a templates folder to store static pages in order to jump to the foreground.
So add this folder under resources and add a default page index.html (Note: There must be an html page under this folder, otherwise the thymeleaf startup item will throw an exception)
5. Write application.properties
This configuration file is a configuration of spring-boot, through which spring-boot configures some frameworks integrated into it. As can be seen from my project structure, I have two application.properties files:
application.properties: main configuration file, spring-boot reads this file directly. Note: The configuration file must be placed under resources, that is, in the project root directory.
application-dev.properties: Development environment configuration file, this is the configuration file of my development environment. In order to simplify some development, some configurations that are different from the deployment environment, such as page cache, etc. This file is configured to read through the spring.profiles.active property of application.properties.
Code for the two files:
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 there is 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=falsspring.jpa.properties.hibernate.use_sql_comments=true
6. So the configuration is complete. Now let’s see how to use spring-boot to start a web program.
The package that spring-boot is an executable jar package, and of course it can also be made into an executable war package. When starting the server, there is no need to start a tomcat like before. It is completely started by java application.
Through a main method of starting a file
@Configuration@EnableAutoConfiguration@ComponentScanpublic class Application { public static void main(String[] args){ SpringApplication springApplication = new SpringApplication (Application.class); springApplication.run (args); }}Let's explain the code in this file first.
@Configuration: Tag this file as a configuration item
@EnableAutoConfiguration: Use automatic configuration
@ComponentScan: Scanable
SpringApplication: Start the manager.
Note that since it is using annotations, you need to configure the scan path. Spring-boot uses the package where the startup manager is located as the root scan path. It will scan the package and subpackage it is located, so you need to place Application.java in the following path, that is, the com.test package.
7. Then just execute it.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.