spring boot项目搭建 发布 打包弯路全纪录

来源:互联网 发布:经传软件骗局2016 编辑:程序博客网 时间:2024/06/06 05:35

这几天在学习spring boot,因为当前手头没有需求,就尝试将之前一个小工程转换为spring boot项目。同时由于目前手头的IDE实在太老(Eclipse indigo),很多步骤都是手动完成的,走了不少弯路。记录如下,也方便以后查阅:


一、Eclipse Indigo安装sts插件


sts全称是springsource-tool-suite,是Spring官方提供给各IDE的工具包。由于eclipse版本众多,因此不同版本对应的sts版本也是不一样的。indigo版本对应的是这个版本,但是经我安装测试,由于版本久远,其对spring boot的支持也相当有限,用于创建普通的spring maven项目尚可,其默认的spring版本为3.2.3,的确是远古的产物了。



如果使用新版本的eclipse,功能会很强大:




二、添加spring-boot-starter依赖


由于我的eclipse版本不支持一键创建spring-boot-starter项目,所以只能自己先创建maven项目,然后手工在pom文件中添加依赖如下:


<parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>1.5.7.RELEASE</version></parent><dependencies><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId>    <version>1.5.7.RELEASE</version>    <type>pom</type>    <scope>compile</scope></dependency></dependencies><build>    <finalName>test_springboot</finalName>    <plugins>    <plugin>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId>     </plugin>    </plugins></build>

注意:上面的<parent>节点十分重要,会影响到后面的打包。



三、spring boot调试找不到类


@SpringBootApplicationpublic class Application{ public static void main(String[] args) {      SpringApplication.run(Application.class, args); }}

注意:1.要添加@springBootApplication的注解,否则springboot无法找到启动的配置类;

          2.这个Application类要放在包的根目录下,因为其只对自己路径下的子包进行扫描和注入bean;





四、spring boot和mybatis集成


当我测试的时候发现一个问题,在如上面代码配置的前提下,当我的web层单独注入一个service是没有问题的,在单独注入一个dao(mybatis mapper)也是没有问题的,但是当注入一个已注入mapper的service的时候就会抛出如下异常org.springframework.beans.factory.NoUniqueBeanDefinitionException:No qualifying bean of type ... available: expected single matching bean but found 2。

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.wasu.service.interfaces.ITaskSV' available: expected single matching bean but found 2: taskSVImpl,ITaskSVat org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:173) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:518) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:627) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]... 25 common frames omitted

观其异常信息是由于我的service被注入了2遍,分别为接口和其实现类。这个问题诡异之处在于,一般抛出这个异常都是发生在有2个实现类同时实现了同一接口时,而我们的情况并不是如此。其原因是这样,由于我们在集成mybatis时还添加了如下的依赖:

<dependency>    <groupId>org.mybatis.spring.boot</groupId>    <artifactId>mybatis-spring-boot-starter</artifactId>    <version>1.0.0</version></dependency>
因此当我们启动项目的时候,会有2个扫描器进行bean的扫描注入,一个是spring的,另一个是mybatis mapper的。由于我们没有配置mybatis mapper的扫描路径,所以其也会基于Application的子包进行扫描,结果就导致了service被扫描注入了2遍。解决这个问题很简单,只要我们在启动类上加上注解即可:

@SpringBootApplication@EnableAutoConfiguration@MapperScan("com.wasu.dao")public class Application{ public static void main(String[] args) {      SpringApplication.run(Application.class, args); }}

五、spring boot集成mybati分页插件PageHelper


这里也走了一个弯路,最初我以为spring boot项目也和其他普通maven项目一样添加PageHelper的依赖就好了,其实不是的,因为我们用的mybatis依赖就已经是spring boot官方注入的了,因此其对应的分页插件也要使用其官方提供的版本:

<dependency>  <groupId>com.github.pagehelper</groupId>  <artifactId>pagehelper-spring-boot-starter</artifactId>  <version>1.0.0</version>  </dependency>

顺便贴一下spring boot推荐使用的yml配置文件的参考版本:


# Server settings  server:      port: 9000    address: localhost  # SPRING PROFILES  spring:    datasource:        name: test        url: jdbc:mysql://localhost:3306/xxxx        username: root        password: xxxx        # 使用druid数据源        type: com.alibaba.druid.pool.DruidDataSource        driver-class-name: com.mysql.jdbc.Driver        filters: stat        maxActive: 20        initialSize: 1        maxWait: 60000        minIdle: 1        timeBetweenEvictionRunsMillis: 60000        minEvictableIdleTimeMillis: 300000        validationQuery: select 'x'        testWhileIdle: true        testOnBorrow: false        testOnReturn: false        poolPreparedStatements: true        maxOpenPreparedStatements: 20           # HTTP ENCODING      http:          encoding.charset: UTF-8          encoding.enable: true          encoding.force: true    # MyBatis  mybatis:      mapperLocations: classpath:mapper/*.xml      typeAliasesPackage: com.wasu.common.bean  # pagehelper分页插件配置pagehelper:    helperDialect: mysql    reasonable: true    supportMethodsArguments: true    params: count=countSql    # LOGGING  logging:      level:         com.ibatis:DEBUG  

yml文件有一点要特别注意:key后面的冒号":"后面一定要加一个空格,否则该行配置将无法服务。这里推荐使用org.dadacoalition.yedit_1.0.20.201509041456-RELEASE.jar插件。


六、spring boot项目打包时html页面文件找不到


因为spring boot项目和普通的maven项目的静态文件路径不同,spring boot项目的为main/resources/static,而maven项目的为main/webapps,当从普通maven项目向spring boot项目转换的时候注意复制一下就好了。


七、spring boot项目发布


使用java -jar xxx.jar即可发布项目



终于见到了久违的项目页面: