一、搭建spring boot环境

  maven工程

      pom文件内容

<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,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 -->  
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
</dependency>  
  </dependencies>
  <!-- <build>
      <plugins>
        java编译插件
        <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>
                    用户名
                    <username>tomcat</username>
                    密码
                    <password>tomcat</password>
                </configuration>
            </plugin>
      </plugins>
  </build>
             -->
</project>

项目目录结构

  其中springTest类是spring boot内部tomcat启动时要运行的类

其中内容如下

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);
    }
}

Application类的作用是外部服务器启动spring boot所要做的一些事。内容如下

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{
     /**
     * 实现SpringBootServletInitializer可以让spring-boot项目在web容器中运行
     */  
    @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文件内容如下

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

//热部署,需要提供的路径
spring.devtools.restart.additional-paths=src\\main\\java\\com\\qm
spring.thymeleaf.cache=false

踩过的坑

坑一,

一开始以为文件目录,可以随便放,经过坑一的教训,才知道代码文件存放有一定的顺序

如上项目结构图,其中springtest类和application类一定要放在最外面的包里,如com.qm  当启动spring boot时,它会去加载com.qm包及其子包下的所有类,

当你不按顺序时,且代码中有@Autowired注解注入bean时,会报以下错误

Description:

Field userService in com.qm.controller.UserController required a bean of type 'com.qm.service.UserService' that could not be found.

Action:

Consider defining a bean of type 'com.qm.service.UserService' in your configuration.
,即bean找不到。,这是应该注意代码顺序。

坑二

补齐坑一后,在service层的接口继承jpa,然后在controller层使用注入的service方法会报以下错误

Error creating bean with name 'dataController': Unsatisfied dependency expressed through field 'personRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property auto found for type Person!

这是因为hibernate版本的问题,

只要加上

<dependency>
    <groupId>org.antlr</groupId>
    <artifactId>antlr4-runtime</artifactId>
    <version>4.1</version>

这个依赖即可

添加之后,若maven仓库中有该jar包,最好删掉,编译时重新下载


若spring boot正常启动,而地址栏访问不到controller,则此时应该注意,spring boot启动时,未扫描到controller类,

在spring boot入口类上再添加一个注解

@ComponentScan("要扫描的包名")

以上就是我在学spring boot时遇到过得一些坑。



初学spring boot踩过的坑的更多相关文章

  1. 部署spring boot + Vue遇到的坑(权限、刷新404、跨域、内存)

    部署spring boot + Vue遇到的坑(权限.刷新404.跨域.内存) 项目背景是采用前后端分离,前端使用vue,后端使用springboot. 工具 工欲善其事必先利其器,我们先找一个操作L ...

  2. Spring Boot踩坑之路一

    Takes an opinionated view of building production-ready Spring applications. Spring Boot favors conve ...

  3. Spring Boot 踩坑之路之 Configuration Annotation Proessor not found in classpath

    1. 出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationProper ...

  4. spring boot踩坑记

    Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotWri ...

  5. 初学 Spring boot 报错 Whitelabel Error Page 404

    按照教程,写了个最简单的 HelloWorld,尼玛报错 -->Whitelabel Error Page 404. 网上99%都是项目结构不对,说什么 Application放在父级 pack ...

  6. 记录初学Spring boot中使用GraphQL编写API的几种方式

    Spring boot+graphql 一.使用graphql-java-tools方式 <dependency> <groupId>com.graphql-java-kick ...

  7. spring boot 枚举使用的坑3

    上一篇说到spring boot 使用jackson在枚举enum序列化和反序列化的问题, 再来说说在JPA中实体entity使用枚举的问题. 还是这个枚举: @Getter @AllArgsCons ...

  8. spring boot 枚举使用的坑

    java 枚举的功能挺多,但是坑更多,使用的时候要注意.如下面这个枚举. @Getter @AllArgsConstructor public enum EnumExpenseType impleme ...

  9. Spring Boot 学习填的坑一

    1.关于springBoot自动扫描规则: SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! "Application"类是指 ...

随机推荐

  1. 主机通过host-only虚拟网卡连接VBOX虚拟机

    主机通过host-only虚拟网卡连接VBOX虚拟机 How to set up host-only virtual machines that host can access it... 问题 因为 ...

  2. conda 使用清华大学开源软件镜像

    conda 使用清华大学开源软件镜像 Anaconda的安装步骤不在本文的讨论中,我们主要是学习一下如何配置conda的镜像,以及一些问题的解决过程 配置镜像 在conda安装好之后,默认的镜像是官方 ...

  3. python3 爬 妹子图

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 Beautiful Soup 4 通过PyP ...

  4. LINQ基础(三)

    一.并行LINQ System.Linq名称空间中包含的类ParallelEnumerable可以分解查询的工作,使其分布在多个线程上. 尽管Enumerable类给IEnumerable<T& ...

  5. ThinkPHP5.0版本和ThinkPHP3.2版本的区别

    5.0版本和之前版本的差异较大,本篇对熟悉3.2版本的用户给出了一些5.0的主要区别. URL和路由 5.0的URL访问不再支持普通URL模式,路由也不支持正则路由定义,而是全部改为规则路由配合变量规 ...

  6. 更换包管理工具npm为yarn

    官网:https://yarnpkg.com/zh-Hans/ 主要考虑: 1. npm管理安装模块依赖的版本不太方便,容易在删除node_modules重新install或在其他机器上新安装时, 安 ...

  7. 网络编程应用:基于TCP协议【实现文件上传】--练习

    要求: 基于TCP协议实现一个向服务器端上传文件的功能 客户端代码: package Homework2; import java.io.File; import java.io.FileInputS ...

  8. iOS gcd 串行,并行,同步,异步代码研究

    参考文章: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #527eff } span.s1 { } http: ...

  9. Spring学习(2)---IOC

    1.接口及面向接口编程 2.什么是IOC 3.Spring的Bean配置 4.Bean的初始化 5.Spring的常用注入方式 (一)接口 用于沟通的中介物的抽象化 实体把自己提供给我外接的一种抽象化 ...

  10. android登录实现,存储数据到/data/data/包名/info.txt

    1.一个简单登录界面布局代码如下: @1采用线性布局加相对布局方式 @2线性布局采用垂直排列 <?xml version="1.0" encoding="utf-8 ...