開發工具: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>