Spring/Maven Exception - java.lang.ClassNotFoundException: org.springplugins.web.SelectedAnnotationHandlerMapping
附:由Maven管理的基于Spring架构的Web应用引起的Exception介绍如何将Maven 官方repository(mvnrepository.com)中缺少的jar包加入到本地Maven的repository中。
案例:研究Spring3.X MVC新特性
1. 在maven的pom配置文件中导入spring 的常规依赖和Spring的mvc依赖:
Spring的常规依赖:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
spring mvc依赖
dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
由于还用到了JSP和JSTL标记库,并使用的是Tomcat6.0。导入其它相关包
<!-- JSTL tag library -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<!-- javax.servelt.HttpServletRequest -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
2.编码结束后,部署到Eclipse的Tomcat中。
在启动Tomcat6.0后,出现如下Exception:java.lang.ClassNotFoundException: org.springplugins.web.SelectedAnnotationHandlerMapping
然后由于项目是基于Maven的,就去maven的repository中(mvnrepository.com)中找这个缺少的类在哪个地方,找了半天,都找不到,包括去公司的nexus私服上,也找不到,后来在jarfinder中找到了这个jar,这个缺少的类是在springplugins.jar中。既然找到了这个类,要做的就是将这个类加到项目中,于是将这个jar加到本地的Maven Dependency中就是一个不错的选择。
如果想要将springplugins.jar加到Maven Dependency中,可以分为如下几步
1) mvn install:install-file -Dfile=path-to-your-artifact-jar -DgroupId=your.groupId -DartifactId=your-artifactId -Dversion=your-version -Dpackaging=jar
如:mvn install:install-file -Dfile=D:\springplugins.jar -DgroupId=com.wsheng -DartifactId=wsheng-springplugins -Dversion=1.0 -Dpackaging=jar
(需要注意的是,根据-Dfile=D:\springplugins.jar要求在命令行执行该命令时,目录必须在D盘下:可以简单的理解成:Jar包在哪里,就在哪里执行该命令是一个很不错的选择)
执行后可以看到类似下面的信息:
[INFO] Installing c:\springplugins.jar to c:\maven-repo\swang\ebox\com\wsheng\wsheng-springplugins\1.0\wsheng-springplugins-1.0.jar
[INFO] Installing C:\Users\swang6\AppData\Local\Temp\mvninstall989911758677694999.pom to c:\maven-repo\swang\ebox\com\wsheng\wsheng-springplugins\1.0\wsheng-springplugins-1.0.pom
看到这信息和Build Successfully的信息后,证明jar包已经成功加到本地的repository中。
2). 可以去maven的本地repository中check该jar包是否存在。
3). 在maven的pom.xml中导入该jar包
<dependency>
<groupId>com.wsheng</groupId>
<artifactId>wsheng-springplugins</artifactId>
<version>1.0</version>
<type>jar</type>
</dependency>