一、搭建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. Angular环境准备和Angular cli

    Angular4.0来了,更小,更快,改动少 接下来为Angular4.0准备环境和学会使用Angular cli项目 1.环境准备: 1)在开始工作之前我们必须设置好开发环境 如果你的机器上还没有安 ...

  2. Eclipse 扩展activiti-desinger 安装

    activiti-desinger 工作流画图工具分为在线安装.离线安装两种方式:下图提供当前所用eclipse版本信息 1.1        在线安装 打开Eclipse -> Help -& ...

  3. 回归-LDA与QDA

    作者:桂. 时间:2017-05-23  06:37:31 链接:http://www.cnblogs.com/xingshansi/p/6892317.html 前言 仍然是python库函数sci ...

  4. poj3648

    poj3648 题意 有一对新人结婚,n-1对夫妇去参加婚礼.有一个很长的座子,新娘与新郎坐在座子的两边(相反).接下来n-1对夫妇就坐,其中任何一对夫妇都不能坐在同一边,且(有一些人有奸情)这些有奸 ...

  5. form表单在前台转json对象

    会发生序列化乱码问题,待解决. //根据表单id将其内空间,名称,值转为json var fireTraceEquipment =queryParamByFormId('form1'); functi ...

  6. wap网站的优化建设

    我们在成功建立wap网站之后,不要觉得一时没有达到自己想要的效果就丢之气之,让其成为垃圾链接,我们既然前期做了大量的工作来建立起来这个网站,一定要坚持耐心的把这个网站培养下去,其实就如同我们栽种一个树 ...

  7. 文件描述符与FILE

    1. 文件描述符(重点) 在Linux系统中一切皆可以看成是文件,文件又可分为:普通文件.目录文件.链接文件和设备文件.文件描述符(file descriptor)是内核为了高效管理已被打开的文件所创 ...

  8. 基于servlet和ajax的聊天室

    (手贱点了更新发布时间,发布时间变成6-9...) 2017-5-20,在这个奇特的日子,我不再满足于在本地测试javaweb,于是在上腾讯云买了第一个云服务器,由于是学生认证,所以一个月只要10块钱 ...

  9. vue-schart : vue.js 的图表组件

    介绍 vue-schart 是使用vue.js封装了sChart.js图表库的一个小组件.支持vue.js 1.x & 2.x 仓库地址:https://github.com/lin-xin/ ...

  10. Linux下串口通信工具minicom的用法

    一.查看串口设备 例如,将USB转串口线插入交换机Console口后,执行命令:$ll /dev/ttyUSB* 二.连接串口设备 $sudo minicom -D /dev/ttyUSB0 三.设置 ...