这个过程比较折腾,先是一路搭建过来能够正确运行基于spring-test创建的单元测试,但发现在tomcat中无法正确运行,提示的错误不再赘述了,可能是中间某些dependency的版本冲突造成的。于是重新按照自己记录的过程(参见前几篇博客),重新走一遍整个过程,在tomcat上可以正确运行了,但郁闷的是基于spring-test编写的单元测试无法运行了,加入全部dependency后,又尝试了多种方法,单元测试反复提示
javax.validation.ValidationException: HV000183: Unable to load 'javax.el.ExpressionFactory'
既然tomcat中可以正常运行,那就是tomcat中的lib在单元测试中没有加入,于是尝试过逐个加入tomcat/lib/*.jar,但并无效果~~如下方法经测试无效:
1. 增加dependency: tomcat-el-api
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-el-api</artifactId>
<version>8.5.27</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>8.5.27</version>
<scope>provided</scope>
</dependency>
2. 增加dependency: javax.el
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
<scope>provided</scope>
</dependency><dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.el</artifactId> <version>2.2.4</version>
<scope>provided</scope>
3. tomcat/lib/*.jar逐个加入。
其实还有一种情况,因为后面增加的lib都是通过provided或者test scope增加的,而tomcat/lib是先行读入的,所以两者的优先顺序是不一样的,这可能造成在tomcat上可以正确运行,但在单元测试中可能永远通不过。后来在stackoverflow上偶然发现一个提到hibernate-validator版本的问题,发现真的是这样~~将hibernate-validator版本从6.0.7.Final降到5.2.5.Final可以正确运行了,比5.2.5.Final高的版本都不行!!完整的pom.xml如下:
将dubbo-config和spring-config分开,其中dubbo-config如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<dubbo:application name="bmserver"/>
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<dubbo:protocol name="rest" port="8080" server="servlet" contextpath="bmserver"></dubbo:protocol>
<beans:bean id="userServiceJava" class="com.abel.bmserver.UserServiceJava"></beans:bean>
<dubbo:service interface="com.abel.bigmeter.service.UserService" ref="userServiceJava"></dubbo:service>
<beans:bean id="userService" class="com.abel.bmserver.impl.UserServiceImpl"></beans:bean>
<dubbo:service interface="com.abel.bigmeter.service.UserService" ref="userService"></dubbo:service>
</beans:beans>
spring-config如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.abel.bmserver" />
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<beans:bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<beans:property name="driverClassName" value="${jdbc.driver}"></beans:property>
<beans:property name="url" value="${jdbc.url}"></beans:property>
<beans:property name="username" value="${jdbc.username}"></beans:property>
<beans:property name="password" value="${jdbc.password}"></beans:property>
<beans:property name="initialSize" value="0"></beans:property>
<beans:property name="maxActive" value="20"></beans:property>
<beans:property name="minIdle" value="0"></beans:property>
<beans:property name="maxWait" value="60000"></beans:property>
</beans:bean>
<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"></beans:property>
<beans:property name="configLocation" value="classpath:mybatis-config.xml"></beans:property>
</beans:bean>
<beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<beans:property name="dataSource" ref="dataSource"></beans:property>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<annotation-driven>
<message-converters register-defaults="false">
<beans:bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<beans:bean class="org.springframework.http.converter.StringHttpMessageConverter">
<beans:constructor-arg index="0" value="utf-8"/>
</beans:bean>
<beans:bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<beans:bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<beans:bean class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter" />
<beans:bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
<beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<beans:property name="objectMapper">
<beans:bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<beans:property name="featuresToDisable">
<util:constant static-field="com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES" />
</beans:property>
</beans:bean>
</beans:property>
</beans:bean>
</message-converters>
</annotation-driven>
<beans:bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<beans:property name="basePackage" value="com.abel.bmserver.mapper"></beans:property>
</beans:bean>
<beans:bean class="com.abel.bmserver.impl.LoginManager"></beans:bean>
<beans:bean class="com.abel.bmserver.impl.UserServiceImpl"></beans:bean>
</beans:beans>
web.xml基本没变化,就是增加了dubbo-config.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/servlet-context.xml, /WEB-INF/spring/appServlet/dubbo-context.xml</param-value>
</context-param>
<listener>
<listener-class>com.alibaba.dubbo.remoting.http.servlet.BootstrapListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
root-config.xml因为采用了eclipse插件spring-sts创建的spring mvc project,其内容除了主标签外都是空的:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
</beans>
单元测试代码示例:
package com.abel.bmserver.mapper
import com.abel.bigmeter.service.UserService
import com.abel.bigwater.model.LoginRequest
import com.abel.bmserver.impl.UserServiceImpl
import com.alibaba.fastjson.JSON
import org.junit.Test
import org.junit.runner.RunWith
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.util.DigestUtils
import java.time.LocalDateTime
import kotlin.test.assertTrue
@RunWith(SpringJUnit4ClassRunner::class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml",
"file:src/main/webapp/WEB-INF/spring/root-context.xml")
class MapperTest: AbstractJUnit4SpringContextTests() {
private val log: Logger = LoggerFactory.getLogger(MapperTest::class.java)
@Autowired
private var userMapper: UserMapper? = null
@Autowired
private var userService: UserServiceImpl? = null
@Test
fun testUserMapper() {
log.info("prepare to test UserMapper...")
val userList = userMapper?.listUser("1", null, null)
log.info("User count: ${userList?.count()}")
}
@Test
fun testUserMapperNameStr() {
log.info("prepare to test userMapper with nameStr")
val userList = userMapper?.listUser("1", "a%", null)
log.info("user count like Li%: ${userList?.count()}")
for (user in userList!!) {
log.info("user ${user.name} (${user.id})")
}
}
/**
* Test for login from web.
*/
@Test
fun testUserLoginSid() {
log.info("prepare to test user-login with sid.")
val lr = LoginRequest()
lr.sessionId = "4b4cb9f0-6dba-4511-8912-055ac5dc1d62"
val r1 = userService?.login(lr)
log.warn("user login with sessionId result: ${r1?.single?.id} (${r1?.single?.firmName}, ${r1?.single?.userName})")
}
@Test
fun testUserLoginSidFail() {
log.info("prepare to test user-login with non-exist sid.")
val lr = LoginRequest()
lr.sessionId = "1111-2222"
val r1 = userService?.login(lr)
log.warn("user login with sessionId result: ${r1?.single?.id} (${r1?.code}, ${r1?.error})")
assertTrue(r1?.code ?: 0> 0, "non-exist sessionId: ${lr.sessionId}")
}
@Test
fun testUserLoginUser() {
log.info("prepare to test user-login with user.")
val user = userMapper?.selectUser("anti")
val lr = LoginRequest()
lr.userId = "anti"
lr.timestamp = LocalDateTime.now().toString()
lr.clientHash = DigestUtils.md5DigestAsHex((user?.passHash + lr.timestamp).toByteArray())
lr.devId = "JUnit"
log.info("gson login request:\n ${JSON.toJSONString(lr)}")
val r1 = userService?.login(lr)
log.info("gson:\n ${JSON.toJSONString(r1)}")
log.warn("user login with userId & client-hash result: ${r1?.single?.id} (${r1?.single?.firmName}, ${r1?.single?.userName})")
}
@Test
fun testUserLoginUserFail() {
log.info("test user-login with user fail")
val lr = LoginRequest()
lr.userId = "anti"
lr.timestamp = LocalDateTime.now().toString()
lr.clientHash = DigestUtils.md5DigestAsHex(("1234" + lr.timestamp).toByteArray())
lr.devId = "JUnit"
val r1 = userService?.login(lr)
log.warn("user login with userId & client-hash result: ${r1?.single?.id} (${r1?.code}, ${r1?.error})")
assertTrue(r1?.code ?: 0> 0, "incorrect client-hash: ${lr.sessionId}")
}
}
至此就可以开始正式工作了,祝贺一下:D
再重新发布一下pom.xml,虽然用idea编写kotlin比较方便,但在dependency管理方面真不如eclipse,eclipse可以通过依赖层级关系来逐个查看依赖的冲突情况(由此来看maven在依赖冲突管理方面还是比较复杂的,其实到了这种情况心里还是有点小膈应):
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abel</groupId>
<artifactId>bmserver</artifactId>
<name>bmserver</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>3.2.9.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.7.6</org.slf4j-version>
<kotlin.version>1.2.10</kotlin.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.5.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.3</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.15</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>com.abel</groupId>
<artifactId>bigmeter-model</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.abel</groupId>
<artifactId>bigmeter-webapi</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>src/main/java</source>
<source>src/main/kotlin</source>
</sources>
</configuration>
</execution>
<execution>
<id>test-source</id>
<phase>generate-test-sources</phase>
<goals><goal>add-test-source</goal></goals>
<configuration>
<sources>
<source>src/test/java</source>
<source>src/test/kotlin</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<warName>bmserver</warName>
</configuration>
<executions>
<execution>
<id>make-war</id>
<phase>package</phase>
<goals><goal>war</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>