Maven多模塊,Dubbo分佈式服務框架,SpringMVC,先後端分離項目,基礎搭建,搭建過程出現的問題
首先說一下這篇文章的主要內容分爲:html
一、Maven多模塊項目的建立;前端
二、Maven與SpringMVC的整合;java
三、Dubbo的環境配置及與整合;mysql
四、新手在整合過程易犯的錯誤。git
經過一個簡單的demo來講明,你們多多指教,分享經驗!github
1、Maven多模塊項目的建立web
咱們須要創建一個多模塊的maven項目,其目錄結構爲spring
其中student-api用於暴露接口;student-service用語處理業務邏輯及調用數據訪問對象,返回相應數據;student-web主要用於提供dubbo服務,及其餘db、spring、springMVC、mybatis等配置。這樣設計可以將業務邏輯與數據訪問隔離開,同時貼合了spring目標之一,就是容許咱們在開發應用的程序時,可以遵循面向對象(OO)原則中的「針對接口編程」,很大程度上達到鬆耦合。這裏將接口API隔離出來做爲dubbo生產者的服務接口,供消費應用調用(在後續詳細講解)。sql
1.新建Maven項目數據庫
2.選擇項目存放路徑後,選擇建立一個簡單的maven項目
3.填寫GroupId和ArtifactId,注意選擇或者填寫版本號
點擊完成後,創建好了student-demo項目。以後刪除項目src目錄,打開pom.xml將<packaging>jar</packaging>修改成<packaging>pom</packaging>,pom表示它是一個被繼承的模塊。修改後的pom.xml以下
- <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.student.demo</groupId>
- <artifactId>student-demo</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- <packaging>pom</packaging>
- <name>student-demo</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <!--模塊創建好之後自動生成的-->
- <modules>
- <module>student-service</module>
- <module>student-api</module>
- <module>student-web</module>
- </modules>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
在建成的項目student-demo上右鍵選擇Maven Module後建立子項目student-api。
填寫子模塊名字:
項目類型選擇:
點擊完成,創建好第一個模塊後,一樣的過程創建好student-service模塊和student-web模塊,須要注意的是student-web選擇的maven類型是:
修改子模塊項目目錄中的pom.xml文件,把<groupId>XXX</groupId>和<version>1.0.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>,由於groupId和version會繼承student-demo中的groupId和version,packaging設置打包方式爲jar。例如修改後的student-service模塊的pom.xml以下:
- <?xml version="1.0"?>
- <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.student.demo</groupId>
- <artifactId>student-demo</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </parent>
- <artifactId>student-service</artifactId>
- <packaging>jar</packaging>
- </project>
至此,maven多模塊項目已經建立完成,如今咱們須要在student-demo項目的pom中增長<dependencyManagement>標籤訂義可被子項目繼承的第三方依賴包,打包配置,資源插件等,同時注意統必定義依賴的版本,避免衝突。這裏還利用Maven配置了profiles,可根據狀況增長不一樣的運行環境,並方便快捷地切換項目運行環境,目前咱們只設置了dev開發環境。
- <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.student.demo</groupId>
- <artifactId>student-demo</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- <packaging>pom</packaging>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <dep.ver.lombok>1.16.10</dep.ver.lombok>
- <dep.ver.druid>1.0.1</dep.ver.druid>
- <dep.ver.mysql>5.1.21</dep.ver.mysql>
- <dep.ver.springframework>4.2.5.RELEASE</dep.ver.springframework>
- <dep.ver.mybatis>3.3.0</dep.ver.mybatis>
- <dep.ver.mybatis-spring>1.3.0</dep.ver.mybatis-spring>
- <dep.ver.pagehelper>4.1.6</dep.ver.pagehelper>
- <dep.ver.aspectjrt>1.5.4</dep.ver.aspectjrt>
- <dep.ver.aspectjweaver>1.8.0</dep.ver.aspectjweaver>
- <dep.ver.servlet>3.1.0</dep.ver.servlet>
- <dep.ver.orika-core>1.4.6</dep.ver.orika-core>
- <dep.ver.javassist>3.20.0-GA</dep.ver.javassist>
- <dep.ver.paranamer>2.7</dep.ver.paranamer>
- <dep.ver.concurrentlinkedhashmap-lru>1.4.2</dep.ver.concurrentlinkedhashmap-lru>
- <dep.ver.gson>2.2.4</dep.ver.gson>
- <dep.ver.guava>15.0</dep.ver.guava>
- <dep.ver.slf4j>1.7.21</dep.ver.slf4j>
- <dep.ver.logback>1.1.7</dep.ver.logback>
- <dep.ver.log4j>1.2.12</dep.ver.log4j>
- <dep.ver.slf4j-log4j12>1.7.5</dep.ver.slf4j-log4j12>
- <dep.ver.logback-ext-spring>0.1.2</dep.ver.logback-ext-spring>
- <dep.ver.dubbo>2.5.3</dep.ver.dubbo>
- <dep.ver.zookeeper>3.4.8</dep.ver.zookeeper>
- <dep.ver.zkclient>0.8</dep.ver.zkclient>
- <dep.ver.commons-lang3>3.1</dep.ver.commons-lang3>
- <dep.ver.jackson>1.9.12</dep.ver.jackson>
- <dep.ver.shiro>1.2.3</dep.ver.shiro>
- <dep.ver.freemarker>2.3.22</dep.ver.freemarker>
- <dep.ver.commons-beanutils>1.9.2</dep.ver.commons-beanutils>
- <dep.ver.junit>4.11</dep.ver.junit>
- <dep.ver.mockito>1.10.19</dep.ver.mockito>
- <dep.ver.joda-time>2.9.3</dep.ver.joda-time>
- <dep.ver.commons-collections4>4.1</dep.ver.commons-collections4>
- <dep.ver.httpclient>4.5.2</dep.ver.httpclient>
- <dep.ver.fastjson>1.2.11</dep.ver.fastjson>
- <plg.ver.maven-resources-plugin>2.7</plg.ver.maven-resources-plugin>
- <plg.ver.maven-compiler-plugin>2.5.1</plg.ver.maven-compiler-plugin>
- <plg.ver.maven-source-plugin>3.0.0</plg.ver.maven-source-plugin>
- <plg.ver.lombok-maven-plugin>1.14.8.0</plg.ver.lombok-maven-plugin>
- <jdk.ver>1.8</jdk.ver>
- <encoding>UTF-8</encoding>
- <center.project.name>${project.artifactId}</center.project.name>
- <profiles.dir>src/profiles</profiles.dir>
- <jackson.version>2.6.0</jackson.version>
- </properties>
- <modules>
- <module>student-service</module>
- <module>student-api</module>
- <module>student-web</module>
- </modules>
- <dependencies>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-validator</artifactId>
- <version>5.2.4.Final</version>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <!-- spring -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${dep.ver.springframework}</version>
- <exclusions>
- <exclusion>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${dep.ver.springframework}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${dep.ver.springframework}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${dep.ver.springframework}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${dep.ver.springframework}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${dep.ver.springframework}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${dep.ver.springframework}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>${dep.ver.springframework}</version>
- </dependency>
- <!-- spring -->
- <!-- db -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>${dep.ver.druid}</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>${dep.ver.mysql}</version>
- </dependency>
- <!-- db -->
- <!-- mybatis -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>${dep.ver.mybatis}</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>${dep.ver.mybatis-spring}</version>
- </dependency>
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper</artifactId>
- <version>${dep.ver.pagehelper}</version>
- </dependency>
- <!-- mybatis -->
- <!-- apache -->
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>${dep.ver.commons-lang3}</version>
- </dependency>
- <!-- apache -->
- <!-- commons-beanutils -->
- <dependency>
- <groupId>commons-beanutils</groupId>
- <artifactId>commons-beanutils</artifactId>
- <version>${dep.ver.commons-beanutils}</version>
- <exclusions>
- <exclusion>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- commons-beanutils -->
- <!-- log -->
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${dep.ver.slf4j}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>log4j-over-slf4j</artifactId>
- <version>${dep.ver.slf4j}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>jcl-over-slf4j</artifactId>
- <version>${dep.ver.slf4j}</version>
- </dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- <version>${dep.ver.logback}</version>
- </dependency>
- <!-- log -->
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>${dep.ver.dubbo}</version>
- <exclusions>
- <exclusion>
- <artifactId>spring</artifactId>
- <groupId>org.springframework</groupId>
- </exclusion>
- <exclusion>
- <artifactId>netty</artifactId>
- <groupId>org.jboss.netty</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- dubbo -->
- <!-- zookeeper -->
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>${dep.ver.zookeeper}</version>
- <exclusions>
- <exclusion>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <version>${dep.ver.zkclient}</version>
- <exclusions>
- <exclusion>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- zookeeper -->
- <!-- jackson -->
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-core-asl</artifactId>
- <version>${dep.ver.jackson}</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>${dep.ver.jackson}</version>
- </dependency>
- <!-- jackson -->
- <!-- aspectj -->
- <dependency>
- <groupId>aspectj</groupId>
- <artifactId>aspectjrt</artifactId>
- <version>${dep.ver.aspectjrt}</version>
- </dependency>
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjweaver</artifactId>
- <version>${dep.ver.aspectjweaver}</version>
- </dependency>
- <!-- aspectj -->
- <!-- servlet -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>${dep.ver.servlet}</version>
- <scope>provided</scope>
- </dependency>
- <!-- servlet -->
- <!-- javassist -->
- <dependency>
- <groupId>org.javassist</groupId>
- <artifactId>javassist</artifactId>
- <version>${dep.ver.javassist}</version>
- </dependency>
- <!-- javassist -->
- <!-- test start -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>${dep.ver.junit}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${dep.ver.springframework}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.mockito</groupId>
- <artifactId>mockito-core</artifactId>
- <version>${dep.ver.mockito}</version>
- <scope>test</scope>
- </dependency>
- <!-- test end -->
- <!-- google -->
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- <version>${dep.ver.gson}</version>
- </dependency>
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- <version>${dep.ver.guava}</version>
- </dependency>
- <!-- google -->
- <!-- lombok -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>${dep.ver.lombok}</version>
- </dependency>
- <!-- lombok -->
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-collections4</artifactId>
- <version>${dep.ver.commons-collections4}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- <version>${dep.ver.httpclient}</version>
- <exclusions>
- <exclusion>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <profiles>
- <profile>
- <id>local</id>
- <build>
- <resources>
- <resource>
- <directory>${profiles.dir}/local</directory>
- </resource>
- </resources>
- </build>
- </profile>
- <profile>
- <id>dev</id>
- <build>
- <resources>
- <resource>
- <directory>${profiles.dir}/dev</directory>
- </resource>
- </resources>
- </build>
- </profile>
- </profiles>
- <build>
- <finalName>${center.project.name}</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-resources-plugin</artifactId>
- <version>${plg.ver.maven-resources-plugin}</version>
- <configuration>
- <encoding>${encoding}</encoding>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>${plg.ver.maven-compiler-plugin}</version>
- <configuration>
- <source>${jdk.ver}</source>
- <target>${jdk.ver}</target>
- <encoding>${encoding}</encoding>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-source-plugin</artifactId>
- <version>${plg.ver.maven-source-plugin}</version>
- <configuration>
- <attach>true</attach>
- </configuration>
- <executions>
- <execution>
- <phase>compile</phase>
- <goals>
- <goal>jar</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- </resource>
- </resources>
- </build>
- </project>
在student-api中繼承父pom的依賴,而且直接引入。
- <?xml version="1.0"?>
- <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.student.demo</groupId>
- <artifactId>student-demo</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </parent>
- <artifactId>student-api</artifactId>
- <dependencies>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>${dep.ver.fastjson}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- </dependency>
- <!-- google -->
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- </dependency>
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </dependency>
- <!-- google -->
- </dependencies>
- </project>
在student-service中添加繼承依賴,添加對student-api的依賴。
- <?xml version="1.0"?>
- <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.student.demo</groupId>
- <artifactId>student-demo</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </parent>
- <artifactId>student-service</artifactId>
- <dependencies>
- <dependency>
- <groupId>com.student.demo</groupId>
- <artifactId>student-api</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>${dep.ver.fastjson}</version>
- </dependency>
- <!-- oss相關依賴 -->
- <dependency>
- <groupId>com.aliyun.oss</groupId>
- <artifactId>aliyun-sdk-oss</artifactId>
- <version>2.0.7</version>
- <exclusions>
- <exclusion>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- oss相關依賴 結束 -->
- <!-- db -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- <!-- db -->
- <!-- mybatis -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- </dependency>
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper</artifactId>
- </dependency>
- <!-- mybatis -->
- <!-- spring配置 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- </dependency>
- <!-- spring配置 -->
- <!-- jackson -->
- <!-- <dependency> -->
- <!-- <groupId>org.codehaus.jackson</groupId> -->
- <!-- <artifactId>jackson-core-asl</artifactId> -->
- <!-- </dependency> -->
- <!-- <dependency> -->
- <!-- <groupId>org.codehaus.jackson</groupId> -->
- <!-- <artifactId>jackson-mapper-asl</artifactId> -->
- <!-- </dependency> -->
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-core</artifactId>
- <version>${jackson.version}</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>${jackson.version}</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-annotations</artifactId>
- <version>${jackson.version}</version>
- </dependency>
- <!-- jackson -->
- <!-- log -->
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- </dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>log4j-over-slf4j</artifactId>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>jcl-over-slf4j</artifactId>
- </dependency>
- <!-- log -->
- <!-- google -->
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </dependency>
- <!-- <dependency> -->
- <!-- <groupId>com.google.code.gson</groupId> -->
- <!-- <artifactId>gson</artifactId> -->
- <!-- </dependency> -->
- <!-- google -->
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- </dependency>
- <dependency>
- <groupId>commons-beanutils</groupId>
- <artifactId>commons-beanutils</artifactId>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- </dependency>
- <dependency>
- <groupId>org.javassist</groupId>
- <artifactId>javassist</artifactId>
- </dependency>
- <!-- aspectj -->
- <dependency>
- <groupId>aspectj</groupId>
- <artifactId>aspectjrt</artifactId>
- </dependency>
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjweaver</artifactId>
- </dependency>
- <!-- aspectj -->
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <exclusions>
- <exclusion>
- <artifactId>spring</artifactId>
- <groupId>org.springframework</groupId>
- </exclusion>
- <exclusion>
- <artifactId>netty</artifactId>
- <groupId>org.jboss.netty</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- dubbo -->
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- </dependency>
- <dependency>
- <groupId>cglib</groupId>
- <artifactId>cglib</artifactId>
- <version>3.1</version>
- </dependency>
- </dependencies>
- </project>
在student-web中繼承依賴,添加對student-api,student-service的依賴。注意子項目依賴版本都爲<version>1.0.0-SNAPSHOT</version>
- <?xml version="1.0"?>
- <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.student.demo</groupId>
- <artifactId>student-demo</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </parent>
- <artifactId>student-web</artifactId>
- <packaging>war</packaging>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </dependency>
- <dependency>
- <groupId>com.student.demo</groupId>
- <artifactId>student-api</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>com.student.demo</groupId>
- <artifactId>student-service</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>student-web</finalName>
- </build>
- </project>
2、Maven與SpringMVC的整合
1.第一部分已經在項目pom文件中配置了spring的相關依賴
2.在student-web項目根路徑上添加profiles源目錄及配置文件
注意是創建源文件不是普通文件夾。
而後在此源文件夾下創建普通文件夾props,用於存放配置文件。在props下新建db-config.properties資源文件,用於配置mysql,其配置內容爲:
1 database.database=mysql 2 database.driverClassName=com.mysql.jdbc.Driver 3 database.url=jdbc:mysql://172.0.0.1:3306/student_data?characterEncoding=utf8 4 database.user=root 5 database.password=root 6 database.show_sql=true
在mysql數據庫中創建一張簡單的student數據表,其結構爲:
3.在student-web的src/main/resources目錄中新建spring文件夾,新建spring-base.xml和spring-dispatcher.xml。其中值得說明一下的是,spring監聽器ContextLoaderListener和控制器DispatcherServlet會加載應用上下文。其中上下文參數contextConfigLocation的value值是根應用上下文路徑classpath:spring/spring-base.xml,它會被ContextLoaderListener加載bean定義。spring-base.xml中的內容爲:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd">
- <!-- 開啓aspectj自動註解 -->
- <aop:aspectj-autoproxy proxy-target-class="true" />
- <context:component-scan base-package="org.student" />
- <context:annotation-config />
- <!-- 配置文件加載 -->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="order" value="1" />
- <property name="ignoreUnresolvablePlaceholders" value="true" />
- <property name="locations">
- <list>
- <value>classpath:props/db-config.properties</value>
- </list>
- </property>
- </bean>
- <import resource="classpath:spring/spring-db.xml" />
- </beans>
一樣DispatcherServlet會從classpath:spring/spring-dispatcher.xml加載它的bean。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd">
- <mvc:annotation-driven>
- <mvc:message-converters>
- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter " />
- </mvc:message-converters>
- </mvc:annotation-driven>
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
- <!-- 至關於註冊了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter兩個bean,配置一些messageconverter。即解決了@Controller註解的使用前提配置 -->
- </beans>
4.在Web.xml中聲明DispatcherServlet
SpringMVC全部請求都會經過一個前端控制器DispatcherServlet,經過這個控制器將請求委託給應用程序的其它執行單元來處理,因此須要經過web.xml來註冊。打開student-web項目目錄下src/main/webapp/WEB-INF/web.xml,修改web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- id="schedule-console" version="3.0">
- <display-name>student-web</display-name>
- <!-- web.xml中加載順序是 context-param -> listener -> filter -> servlet -->
- <!-- spring基礎配置文件位置 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/spring-base.xml</param-value>
- </context-param>
- <!-- Spring監聽 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 設置servlet編碼開始 -->
- <filter>
- <filter-name>characterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>characterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 設置servlet編碼結束 -->
- <servlet>
- <servlet-name>dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/spring-dispatcher.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatcher</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
5.spring與Mybatis的整合
在student-web的src/main/resources目錄中新建mybatis文件夾,再在此目錄新建mybatis.xml和mapper文件夾。在spring文件夾下新建spring-db.xml用於spring加載mybatis配置。spring-db.xml內容以下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
- <!-- 採用druid做爲鏈接池 -->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
- init-method="init" destroy-method="close">
- <!-- 基本屬性 url、user、password -->
- <property name="driverClassName" value="${database.driverClassName}" />
- <property name="url" value="${database.url}" />
- <property name="username" value="${database.user}" />
- <property name="password" value="${database.password}" />
- <!-- 配置初始化大小、最小、最大 -->
- <property name="initialSize" value="1" />
- <property name="minIdle" value="1" />
- <property name="maxActive" value="20" />
- <!-- 配置獲取鏈接等待超時的時間 -->
- <property name="maxWait" value="60000" />
- <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 -->
- <property name="timeBetweenEvictionRunsMillis" value="60000" />
- <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 -->
- <property name="minEvictableIdleTimeMillis" value="300000" />
- <property name="validationQuery" value="SELECT 'x'" />
- <property name="testWhileIdle" value="true" />
- <property name="testOnBorrow" value="false" />
- <property name="testOnReturn" value="false" />
- <!-- 打開PSCache,而且指定每一個鏈接上PSCache的大小 -->
- <property name="poolPreparedStatements" value="true" />
- <property name="maxPoolPreparedStatementPerConnectionSize"
- value="20" />
- <!-- 配置監控統計攔截的filters -->
- <property name="filters" value="stat" />
- </bean>
- <!-- 配置mybatis的sqlSessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value="classpath:mybatis/mybatis.xml" />
- <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml" />
- </bean>
- <!-- mybatis mapper接口掃描 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="org.student.service.mapper" />
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
- </bean>
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 開啓@Transactional事務註解 -->
- <tx:annotation-driven transaction-manager="transactionManager" />
- </beans>
3、Dubbo的環境配置及與整合
咱們已經搭建好student-demo項目來做爲provider,爲其它服務提供接口,因此咱們須要先配置好dubbo提供者,PS:這裏不會講解zookeeper的搭建,請自行百度。其項目結構爲:
1.首先在student-web中配置dubbo
因爲咱們已經在student-demo中添加了dubbo和zookeeper的相關依賴,下一步字啊resources目錄中創建dubbo文件夾,再新建dubbo-producer.xml,同時在profiles的dev環境中添加dubbo-producer.properties配置文件,設置zookeeper註冊中心暴露服務地址,配置group分組,服務端口,註冊中心請求超時時間(毫秒)和版本。其中組別+接口地址+版本號惟一的標識了一個接口。文件內容爲:
1 student-registry-address=172.0.0.1:2181 2 student-group=student-min 3 student-service-port=22026 4 student-timeout=120000 5 student-version=1.0.0
這樣咱們就能夠來配置dubbo-producer.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <context:property-placeholder location="classpath:props/dubbo-producer.properties" ignore-unresolvable="true"/>
- <dubbo:application name="provider-student-demo"/>
- <dubbo:registry address="${student-registry-address}" protocol="zookeeper"/>
- <dubbo:protocol name="dubbo" port="${student-service-port}" accesslog="true" />
- </beans>
2.寫一個簡單的服務接口
1)首先在student-service中創建Student實體類
- package org.student.bean;
- public class Student {
- private Long id;
- private String name;
- private Integer age;
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- }
2)同時在student-api中創建與實體bean對應的DTO類實現Serializable接口,並重寫toString方法。通常爲了避免破壞實體類和數據庫表一一對應的接口,咱們會新建一個DTO實體類,而且加入一些冗餘的字段,做爲先後端的傳輸對象。
- package org.student.api.dto;
- import java.io.Serializable;
- public class StudentDTO implements Serializable{
- private static final long serialVersionUID = 1L;
- private Long id;
- private String name;
- private Integer age;
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "id = " + id + ", name = " + name + ",age = " + age;
- }
- }
3)在student-api中創建StudentApi接口,值得注意的是@service註解中的對象
- package org.student.api;
- import java.util.List;
- import org.student.api.dto.StudentDTO;
- public interface StudentApi {
- List<StudentDTO> listStudents();
- }
4)在dubbo-producer.xml中註冊StudentApi,添加配置:
<dubbo:service interface="org.student.api.StudentApi" ref="studentApi" group="${student-group}" version="${student-version}" timeout="${student-timeout}" />
5)從service業務邏輯層子模塊開始編寫接口實現,新建在student-service中新建StudentBiz實現StudentApi接口
- package org.student.service.biz;
- import java.util.List;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.student.api.StudentApi;
- import org.student.api.dto.StudentDTO;
- import org.student.bean.Student;
- import org.student.service.StudentService;
- import com.google.common.collect.Lists;
- @Service("studentApi")
- public class StudentBiz implements StudentApi{
- private Logger logger = LoggerFactory.getLogger(StudentBiz.class);
- @Autowired
- private StudentService studentService;
- @Override
- public List<StudentDTO> listStudents() {
- List<Student> listStudent = studentService.listStudent();
- List<StudentDTO> listStudentDTO = Lists.newArrayList();
- for(Student student: listStudent){
- StudentDTO studentDTO = new StudentDTO();
- studentDTO.setId(student.getId());
- studentDTO.setAge(student.getAge());
- studentDTO.setName(student.getName());
- listStudentDTO.add(studentDTO);
- }
- return listStudentDTO;
- }
- }
6)在student-service中建立service層接口StudentService
- package org.student.service;
- import java.util.List;
- import org.springframework.stereotype.Service;
- import org.student.bean.Student;
- @Service
- public interface StudentService {
- List<Student> listStudent();
- }
7)在student-service中實現StudentService接口
- package org.student.service.impl;
- import java.util.List;
- import javax.annotation.Resource;
- import org.springframework.stereotype.Service;
- import org.student.bean.Student;
- import org.student.service.StudentService;
- import org.student.service.mapper.StudentMapper;
- @Service("studentService")
- public class StudentServiceImpl implements StudentService{
- @Resource
- private StudentMapper studentMapper;
- @Override
- public List<Student> listStudent() {
- List<Student> listStudent = studentMapper.listStudent();
- return listStudent;
- }
- }
8)在student-service中建立數據層StudentMapper接口
- package org.student.service.mapper;
- import java.util.List;
- import org.student.bean.Student;
- public interface StudentMapper {
- List<Student> listStudent();
- }
9)在student-web中建立實體映射的XML文件,注意StudentMapper類與sql ID的對應關係
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="org.student.service.mapper.StudentMapper">
- <!--查詢字段-->
- <sql id="columns">
- a.id,
- a.name,
- a.age
- </sql>
- <!--查詢結果集-->
- <resultMap id="beanMap" type="org.student.bean.Student">
- <result property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="age" column="age"/>
- </resultMap>
- <!--根據主鍵獲取實體-->
- <select id="listStudent" resultMap="beanMap">
- SELECT
- <include refid="columns"/>
- FROM
- student a
- </select>
- </mapper>
dubbo提供者已建立完成,建立完成子應用結構爲
1)dubbo-consumer.properties的service-group應該與提供者一致,否者找不到提供者方的接口。
同時pom.xml中須要增長對的依賴
- <dependency>
- <groupId>com.student.demo</groupId>
- <artifactId>student-api</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </dependency>
- student-registry-address=172.0.0.1:2181
- student-service-group=student-min
- student-service-version=1.0.0
- student-service-timeout=120000
2)spring-dubbo-consumer.xml的配置及接口調用方式
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <dubbo:application name="consumer-student-test" />
- <dubbo:registry address="${student-registry-address}" protocol="zookeeper" />
- <dubbo:reference id="studentApi" interface="org.student.api.StudentApi"
- group="${student-service-group}" version="${student-service-version}"
- check="false" />
- </beans>
3)調用者的controller層
- package org.student.controller;
- import javax.annotation.Resource;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import org.student.api.StudentApi;
- @RestController
- @RequestMapping("/student")
- public class StudentController {
- private static Logger logger = LoggerFactory.getLogger(StudentController.class);
- @Resource
- private StudentApi studentApi;
- @RequestMapping(path = "/listStudent", method = RequestMethod.POST)
- public void listStudent(){
- logger.info("get students...");
- logger.info("the data are " + studentApi.listStudents());
- }
- }
4)服務器同時啓動provider和consumer,會看到啓動信息
- <span style="white-space:pre"> </span>查看zookeeper註冊中心發現提供者消費者都已成功註冊
- <span style="white-space:pre"> </span><img src="https://img-blog.csdn.net/20161110162252922?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="800" height="30" alt="" />
- <span style="white-space:pre"> </span><img src="https://img-blog.csdn.net/20161110162424347?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="800" height="90" alt="" />
- 因爲我並無寫view層來展現數據,如今只能經過控制檯的日誌信息來簡單測試接口。(logback的用法不作解釋)利用瀏覽器post請求訪問
- ://172.0.0.1:8080/student-test/student/listStudent,控制檯輸出爲
- [http-nio-8080-exec-2] INFO 2016-11-07 18:33:24.450 o.s.c.StudentController[24] - get students...
- [DubboServerHandler-172.28.19.7:22026-thread-2] INFO 2016-11-07 18:33:24.729 d.a.o.s.a.StudentApi[58] - [DUBBO] [2016-11-07 18:3:24] 172.28.19.7:56346 -> 172.28.19.7:22026 - student-min/org.student.api.StudentApi:1.0.0 listStudents() , dubbo version: 2.5.3, current host: 127.0.0.1
- [http-nio-8080-exec-2] INFO 2016-11-07 18:33:25.026 o.s.c.StudentController[25] - the data are [id = 1, name = 張三,age = 20]
4、新手在整合過程易犯的錯誤。
1.ClassNotFound異常
- 嚴重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)
- at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
- at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)
- at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:499)
- at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
- at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4764)
- at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
- at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
- at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
- at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
- at java.util.concurrent.FutureTask.run(FutureTask.java:266)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
- at java.lang.Thread.run(Thread.java:745)
eclipse會自動將deployment assembly指定的工程,打成jar包,放入到web-inf/lib目錄下,tomcat在發佈項目的時候沒有同時發佈maven依賴所添加的jar包,可是若是在deployment assembly沒有配置相應的依賴包,就會拋出異常。解決方法:項目 —> properties -> Deployment Assembly -> Add -> Java Build Path Entries -> 選擇Maven Dependencies -> Finish -> OK 把對應的Maven依賴包也發佈到tomcat,調試時會自動把那些jar發佈到指定目錄下,tomcat也能找到那些jar了。
另外,一樣的報錯可能因爲不一樣的緣由形成,若是沒有激活Maven profile也會報這種錯誤,激活profile的方式有不少,經過命令首先須要在pom.xml中用<profiles>配置(詳細配置請查看上文)再經過命令行:
mvn install -Pdev #激活dev環境
也能夠經過eclipse的配置:項目-->properties-->Maven-->填寫profile名-->apply-->finish
2.啓動student-web,spring建立bean對象失敗
- 十一月 07, 2016 8:19:24 下午 org.apache.catalina.core.StandardContext listenerStart
- 嚴重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.student.api.StudentApi': Cannot resolve reference to bean 'studentApi' while setting bean property 'ref'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'studentApi' is defined
- at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
- ...
- at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
- ...
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'studentApi' is defined
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
- at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1175)
- at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
- at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
- at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
- ...
spring有強大的註解幫助咱們簡化很大部分代碼,並下降的耦合。@Autowired或@Resource在 Bean 類中使用自動注入功能,可是 Bean 仍是在 XML 文件中經過 <bean> 進行定義 —— 也就是說,在 XML 配置文件中定義 Bean,經過@Autowired或@Resource爲 Bean 的成員變量、方法入參或構造函數入參提供自動注入的功能。之前經過在spring文件中配置<bean>的方式被移除,僅須要添加一行 <context:component-scan/> 配置就解決全部問題了——Spring XML 配置文件獲得了極致的簡化。<context:component-scan/> 的 base-package 屬性指定了須要掃描的類包,類包及其遞歸子包中全部的類都會被處理。解決:在spring-base.xml中添加
<context:component-scan base-package="org.student" />
3.消費者訪問dubbo接口被拒絕
- 嚴重: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is com.alibaba.dubbo.rpc.RpcException: Forbid consumer 172.0.0.1 access service org.student.api.StudentApi from registry 172.0.0.1:2181 use dubbo version 2.5.3, Please check registry access list (whitelist/blacklist).] with root cause
- com.alibaba.dubbo.rpc.RpcException: Forbid consumer 172.0.0.1 access service org.student.api.StudentApi from registry 172.0.0.1:2181 use dubbo version 2.5.3, Please check registry access list (whitelist/blacklist). at com.alibaba.dubbo.registry.integration.RegistryDirectory.doList(RegistryDirectory.java:579) at com.alibaba.dubbo.rpc.cluster.directory.AbstractDirectory.list(AbstractDirectory.java:73) at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.list(AbstractClusterInvoker.java:260) at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:219) at com.alibaba.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:72) at com.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:52) at com.alibaba.dubbo.common.bytecode.proxy0.listStudents(proxy0.java) at org.student.controller.StudentController.listStudent(StudentController.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498)
- ....
若是消費者訪問的接口路徑不正確,則沒法調用接口。出現這種錯誤是我沒有把消費者的組設置成提供者的組,解決方法是group保持一致
4.訪問全部接口都報404錯誤
- INFO 2016-11-08 12:42:28.507 o.s.w.s.DispatcherServlet[488] - FrameworkServlet 'dispatcher': initialization started
- INFO 2016-11-08 12:42:28.518 o.s.w.c.s.XmlWebApplicationContext[578] - Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Nov 08 12:42:28 CST 2016]; parent: Root WebApplicationContext
- INFO 2016-11-08 12:42:28.519 o.s.b.f.x.XmlBeanDefinitionReader[317] - Loading XML bean definitions from class path resource [spring/spring-dispatcher.xml]
- INFO 2016-11-08 12:42:28.657 o.s.b.f.s.DefaultListableBeanFactory[839] - Overriding bean definition for bean 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping' with a different definition: replacing [Root bean: class [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Root bean: class [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
- ...
- INFO 2016-11-08 12:42:29.545 o.s.w.s.m.m.a.RequestMappingHandlerAdapter[532] - Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Nov 08 12:42:28 CST 2016]; parent: Root WebApplicationContext
- INFO 2016-11-08 12:42:29.810 o.s.w.s.v.v.VelocityConfigurer[140] - ClasspathResourceLoader with name 'springMacro' added to configured VelocityEngine
- INFO 2016-11-08 12:42:30.193 o.s.w.s.DispatcherServlet[507] - FrameworkServlet 'dispatcher': initialization completed in 1684 ms
- WARN 2016-11-08 12:42:30.206 o.s.w.s.PageNotFound[1136] - No mapping found for HTTP request with URI [/student/listStudent] in DispatcherServlet with name 'dispatcher'
這個錯誤我找了好久才發現,並非簡單的訪問路徑錯誤。仔細查看日誌信息會發現spring加載dispatcher的過程,截取在web.xml中配置
<!--spring加載--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-base.xml</param-value> </context-param> ...
<!--springMVC加載-->
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-dispatcher.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
在Tomcat啓動時,web.xml中加載順序是 context-param -> listener -> filter -> servlet,ContextLoaderListener基於Web上下文級別的監聽器在啓動服務器時就建立ApplicationContext而且將配置的Spring Bean加載到XML中。DispatcherServlet是一個請求分發控制器,全部匹配的URL都會經過該Servlet分發執行,在建立Servlet對象時會初始化Spring MVC相關配置。spring-dispatcher.xml中定義了控制器映射,使用Controller+RequestMapping註解映射時,相關controller組件掃描要定義在spring-dispatcher.xml中,而非spring-base.xml中。依據這樣的分析,我去查看了student-test項目中spring-dispatcher.xml和spring-base.xml的配置,發現spring-base.xml配置了
<context:component-scan base-package="org.student"/>
而在spring-dispatcher.xml中未配置掃描路徑。因此spring沒法加載controller中的映射,天然會404了,解決方式則是在spring-dispatcher.xml中加上掃描。
博客推薦:
http://blessht.iteye.com/blog/2121845