开发工具:IntellJ IDEA 2017 css
springboot建立父子工程、聚合工程及搭建框架过程当中遇到的问题解决java
wyait父工程【父】:wyait-parent(用于统一依赖版本管理)
wyait通用工程【子】:wyait-common(统一保存通用工具类)
wyait-web工程【子】:wyait-web(聚合工程) git
wyait-web 项目框架设计简述:github
github:https://github.com/wyait/web.git
码云:https://gitee.com/wyait/web.gitweb
注意,源码中没有提交空白目录,有些src/main/java/、src/main/resources/等目录须要你们手动建立!!!redis
MyWebMvcConfig类片断:spring
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/css/**”).addResourceLocations(“/css/”); registry.addResourceHandler(“/images/**”).addResourceLocations(“/images/”); registry.addResourceHandler(“/js/**”).addResourceLocations(“/js/”); //配置中的file:表示是一个具体的硬盘路径,其余的配置指的是系统环境变量 registry.addResourceHandler(“/img/**”).addResourceLocations(“file:D:/demo-images/”); super.addResourceHandlers(registry); }
把0.jpg图片放到D:/demo-images/路径下,启动项目,访问:
http://127.0.0.1:8099/img/0.jpg
显示图片,配置ok。sql
注意:使用IntellJ IDEA开发工具时,有个问题,见文末。数据库
redis的使用,参考wyait-common项目中RedisUtil类中的API方法;
参考博客:
spring boot 1.5.9 整合redis:https://blog.51cto.com/wyait/2048478apache
httpClient操做,参考midd-common项目中HttpService类中的API方法,结果封装在HttpResult类中
每一个平台都有一个parent项目,用于项目依赖版本统一管理
项目结构以下:
注意:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency>
使用spring-boot-dependencies依赖对springboot的依赖包进行统一管理。
... ... <groupId>com.wyait.parent</groupId> <artifactId>wyait-parent</artifactId> <version>1.0.0</version> <packaging>pom</packaging><!--父模块打包类型必须为pom--> <properties> <!--依赖版本号管理--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8 </project.reporting.outputEncoding> <java.version>1.7</java.version> <!--spring依赖版本控制(和spring-boot-parent版本保持一致)--> <springframework.version>4.3.13.RELEASE</springframework.version> <!--spring-boot-parent版本号,经过spring-boot管理其余第三方依赖版本--> <spring.boot.version>1.5.9.RELEASE</spring.boot.version> <mybatis.version>1.3.1</mybatis.version> <druid.version>1.1.5</druid.version> <pagehelper.version>1.2.3</pagehelper.version> <commons.lang3.version>3.6</commons.lang3.version> <commons.io.version>2.5</commons.io.version> <oval.version>1.86</oval.version> </properties> <!--管理依赖jar包--> <dependencyManagement> <dependencies> <!-- 统一管理Spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>${springframework.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> ...<!--此处省略,详见源码!源码地址详见文末--> </dependencies> </dependencyManagement> <!--统一插件配置版本管理 TODO--> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <!--指定编译时的jdk版本1.7 --> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
用于存放通用的工具类;
wyait-common项目,统一保存通用工具类
涉及到bean注入,须要在wyait-web的Application启动类中,添加注解:
@SpringBootApplication //@ComponentScan用于配置扫描com.wyait.web以外的包下面的类 @ComponentScan(basePackages={"com.wyait"}) public class WyaitWebApplication { public static void main(String[] args) { SpringApplication sa=new SpringApplication(WyaitWebApplication.class); // 禁用devTools热部署 //System.setProperty("spring.devtools.restart.enabled", "false"); // 禁用命令行更改application.properties属性 sa.setAddCommandLineProperties(false); sa.run(args); } }
方法和建立wyait-parent一致,注意不删除src。
<parent> <groupId>com.wyait.parent</groupId> <artifactId>wyait-parent</artifactId> <version>1.0.0</version> </parent> <groupId>com.wyait.common</groupId> <artifactId>wyait-common</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <dependencies> ...<!--此处省略,根据wyait-common须要导入相应的依赖便可--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
本demo为了方便,前期全部第三方依赖统一加在pom工程里面。好比:都放在wyait-web的pom文件中。上线前,再统一作调整,分别配置依赖。
建议前期就分配module各项目须要的依赖,避免引入没必要要的。
操做和建立wyait-common一致。
... <groupId>com.wyait.web</groupId> <artifactId>wyait-web</artifactId> <version>${wyait.web.version}</version> <packaging>pom</packaging> <parent> <groupId>com.wyait.parent</groupId> <artifactId>wyait-parent</artifactId> <version>1.0.0</version> </parent> <!--聚合子模块--> <modules> </modules> <properties> <!--wyait-web项目版本管理--> <wyait.web.version>1.0.0</wyait.web.version> <wyait.common.version>1.0.0</wyait.common.version> </properties> <dependencies> <!--引入common依赖--> <dependency> <groupId>com.wyait.common</groupId> <artifactId>wyait-common</artifactId> <version>${wyait.common.version}</version> </dependency> ... <!--详见源码;源码连接在文末--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <!-- 使用IDEA开发工具时,须要加上该resources配置,解决webapp/资源目录无效的问题 --> <resources> <resource> <directory>src/main/webapp</directory> <!--编译的时候把webapp文件放到resources下,必需要放在此目录下才能被访问到 --> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*</include> </includes> </resource> </resources> </build> </project>
选中pom项目wyait-web,右键:new --> module --> maven :
finish。
项目结构:
<parent> <artifactId>wyait-web</artifactId> <groupId>com.wyait.web</groupId> <version>1.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.wyait.web.pojo</groupId> <artifactId>wyait-web-pojo</artifactId> <version>${wyait.web.version}</version> <packaging>jar</packaging>
建立wyait-web-service、wyait-web-dao、wyait-web-controller项目同样。pom依赖根据依赖关系,自行调整,详见源码。
第一种,建立方式和wyait-web-pojo同样;
建立项目完成后,另外须要几步操做:
注意:这里WEB-INF/下面多了一个web.xml文件,或在pom文件中添加一个maven-war-plugin插件配置,下文会说明缘由。
第二种,在选择maven的时候,选中:Create from archetype,找到:maven-archetype-webapp,建立webapp项目;
而后调整项目结构和依赖和方式一一致便可。//TODO
第三种,New --> project --> Spring Initializr建立springboot项目 --> 调整项目结构和依赖和上面保持一致便可。//TODO
新建controller类、静态文件和页面,详见源码!
IndexController中有midd-common项目中通用工具类的测试,也可自行编写测试
其余技术依赖。//TODO
就会导出module模块wyait-web-controller,其余模块操做同样。
相比eclipse简单一些,主要在check out过程当中,要注意聚合项目的目录层级结构(平行结构、父子结构)。//TODO
eclipse和IntellJ IDEA开发工具不一样遇到的问题。
使用IntellJ IDEA,必须在依赖中添加如下配置:
Caused by: java.lang.IllegalArgumentException: Folder 'D:\wyaitWorkspace\wyait-web\src\main\resources' must exist and must be a directory
加上便可解决;
<!--使用IDEA开发工具时,注释该依赖,不然启动报错;IDEA内置tomcat--> <!--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>-->
<!-- 使用IDEA开发工具时,须要加上该resources配置,解决webapp/资源目录无效的问题 --> <resources> <resource> <directory>src/main/webapp</directory> <!--编译的时候把webapp文件放到resources下,必需要放在此目录下才能被访问到 --> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*</include> </includes> </resource> </resources>
wyait-web-webapp要打包为war时,项目src/main/webapp/WEB-INF/目录下,若是没有web.xml文件,打包会报错:找不到WEB-INF/web.xml.
... ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 30.266 s [INFO] Finished at: 2018-07-18T11:37:05+08:00 [INFO] Final Memory: 19M/184M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project mdd-admin: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR]
方案一:在项目WEB-INF/目录下新建一个web.xml文件,用于项目package时使用,无其余用途。
web.xml文件不须要增长任何配置,内容以下:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> </web-app>
方案二【推荐】:在pom.xml中添加依赖:
注意Failed失败中,maven-war-plugin的版本为2.2。因此pom中配置的插件版本要对应同样。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.2</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin>