I. Build the spring boot environment Maven project Pom file content
|
<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.qm.demo</groupId>
<artifactId>springbootDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>MySQL</groupId>
<artifactId>mysql-connector-Java</artifactId>
<version>5.0.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional><!--optional=true, dependencies are not passed, the project relies on Devtools, and then projects that depend on Myboot project if you want to use Devtools, Need to re-introduce-
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<!--<build>
<plugins>
Java compilation Plug-in
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8080</port>
<path>/</path>
<url>http://192.168.25.135:8080/manager/text</url>
User name
<username>tomcat</username>
Password
<password>tomcat</password>
</configuration>
</plugin>
</plugins>
</build>
-
</project> |
Project directory Structure
Where the Springtest class is the class to run when spring boot internal Tomcat starts
The contents are as follows
Package com.qm;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.boot.web.servlet.ServletComponentScan;
Import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
Import Com.qm.controller.UserController;
@SpringBootApplication
public class Springtest {
public static void Main (string[] args) {
Springapplication.run (Springtest.class, args);
}
} |
The role of the application class is something that the external server has to do to start spring boot. The contents are as follows
|
Package com.qm;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.boot.builder.SpringApplicationBuilder;
Import Org.springframework.boot.web.support.SpringBootServletInitializer;
Import Org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan
public class Application extends springbootservletinitializer{
/**
* Implement Springbootservletinitializer to allow Spring-boot projects to run in the Web container
*/
@Override
Protected Springapplicationbuilder Configure (Springapplicationbuilder builder) {
Builder.sources (This.getclass ());
return super.configure (builder);
}
public static void Main (string[] args) {
Springapplication.run (Application.class, args);
}
}
|
Application.properties file contents are as follows
|
server.port=8080
Spring.datasource.url=jdbc:mysql://localhost:3309/springbootdemo
Spring.datasource.driver-class-name=com.mysql.jdbc.driver
Spring.datasource.username=root
spring.datasource.password=123456
Spring.jpa.show-sql=true
Spring.jpa.hibernate.ddl-auto=update
Spring.devtools.livereload.enabled=true
Hot deployment, the path that needs to be provided
Spring.devtools.restart.additional-paths=src\\main\\java\\com\\qm
Spring.thymeleaf.cache=false
|
Stepping on the pit of a pit,
At first think the file directory, you can casually put, after the lesson of the pit, just know the code files stored in a certain order
As in the project structure diagram, where the Springtest class and the application class must be placed in the outermost package, such as COM.QM when you start spring boot, it loads all the classes under the COM.QM package and its sub-packages.
The following error is reported when you are not in the order, and there are @autowired annotations injected into the bean in the code
Description:
Field UserService in Com.qm.controller.UserController required a bean of type ' com.qm.service.UserService ' this could not be found.
Action:
Consider defining a bean of type ' com.qm.service.UserService ' in your configuration.
, that is, the bean cannot be found. , this is the code order that should be noted.
Pit II
After filling the pit one, the interface on the service layer inherits JPA, and the service method injected at the controller layer will report the following error
Error creating Bean with Name ' Datacontroller ': Unsatisfied dependency expressed through field ' personrepository '; Nested exception is org.springframework.beans.factory.BeanCreationException:Error creating beans with Name ' Personrepository ': Invocation of Init method failed; Nested exception is Org.springframework.data.mapping.PropertyReferenceException:No property auto found for type person!
This is because of the hibernate version of the problem,
Just add
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.1</version>
This dependency can be
Once added, if the jar package is in the Maven repository, it is best to delete it and download it again at compile time.
If spring boot starts normally and the address bar does not access the controller, you should be aware that the Controller class is not scanned at spring boot startup.
Add an annotation to the Spring boot entry class
@ComponentScan ("Package name to scan")
These are some of the pits I encountered when I was learning Spring boot.
Beginner Spring boot step over the pit