SpringBoot注意事项及坑

本文记录SpingBoot使用过程中遇到的一些需要注意的小问题

编译版本

springboot默认比较保守使用了jdk1.6,若使用了1.6之后版本的特性需要在mavenpom.xml中在plugins中增加如下插件

1
2
3
4
5
6
7
8
9
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

无法注入

SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!

如果Application类所在的包为:io.github.welljay.app,则只会扫描io.github.welljay.app包及其所有子包,如果service或dao所在包不在io.github.gefangshuai.app及其子包下,则不会被扫描!

打包jar问题

首先需要在mavenpom.xml中增加插件

1
2
3
4
5
6
7
8
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果没有该项配置,devtools不会起作用,即应用不会restart【springboot调试热更新用】 -->
<fork>true</fork>
</configuration>
</plugin>

然后打包项目的时候,项目->maven->xxxModule->Plugins->spring-boot->spring-boot:repackage
会报如下错误:

1
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage (default-cli) on project web

解决办法:在spring-boot:repackage右键–>Create 'xxxModule'[spring-boot:repackage]...->在对话框中的Command Line中输入

1
org.apache.maven.plugins:maven-jar-plugin:2.4:jar  org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage

或者简写为:

1
package  spring-boot:repackage

Linux后台运行

因为springboot在Linux中使用java -jar xxxx.jar运行后会占用终端,不能其他任何操作。所以使用linux的命令后端执行,以下是一个sh脚本文件方便每次方便执行

1
2
#!/bin/bash
java -jar web-1.0-SNAPSHOT.jar > web-log.file 2>&1 &

后端运行并将log打印到相对路径下web-log.file文件中;