这是我走的大概三天的坑,一直搞不清楚原因,今天总算把问题解决了。
首先说问题的源头,是我使用SSM框架写j2ee程序,要使用到AOP,于是就在网上查找了如何使用maven添加AOP相关的jar包,之后配置了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/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>comgroupId>
<artifactId>reecoartifactId>
<packaging>warpackaging>
<version>1.0-SNAPSHOTversion>
<name>reeco Maven Webappname>
<url>http://maven.apache.orgurl>
<repositories>
<repository>
<id>java.servlet.apiid>
<name>Repository hosting the java servlet api artifactsname>
<url>http://repo1.maven.org/maven2/javax/servlet/javax.servlet-api/3.0.1/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
<properties>
<junit-version>4.12junit-version>
<spring-version>4.3.2.RELEASEspring-version>
<mybatis-version>3.4.1mybatis-version>
<mybatis-spring-version>1.3.0mybatis-spring-version>
<druid-version>1.0.24druid-version>
<fastjson-version>1.2.16fastjson-version>
<mysql-connection-version>5.1.6mysql-connection-version>
<mybatis-generator-version>1.3.2mybatis-generator-version>
<pagehelper-version>4.0.1pagehelper-version>
<slf4j-version>1.7.12slf4j-version>
<log4j-version>1.2.17log4j-version>
<jstl-version>1.2jstl-version>
<taglibs-version>1.1.2taglibs-version>
<aspectj-version>1.8.5aspectj-version>
<operation-version>1.0operation-version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit-version}version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis-version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>${mybatis-spring-version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid-version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>${fastjson-version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql-connection-version}version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>${pagehelper-version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>${slf4j-version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-simpleartifactId>
<version>${slf4j-version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j-version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j-version}version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>${jstl-version}version>
dependency>
<dependency>
<groupId>taglibsgroupId>
<artifactId>standardartifactId>
<version>${taglibs-version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.0.1version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjrtartifactId>
<version>${aspectj-version}version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>${aspectj-version}version>
dependency>
<dependency>
<groupId>cglibgroupId>
<artifactId>cglibartifactId>
<version>2.2version>
dependency>
<dependency>
<groupId>aopalliancegroupId>
<artifactId>aopallianceartifactId>
<version>1.0version>
dependency>
dependencies>
<build>
<finalName>ssm-simplefinalName>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<excludes>
<exclude>generator*.*exclude>
excludes>
resource>
resources>
<plugins>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>${mybatis-generator-version}version>
plugin>
plugins>
build>
project>
其中注释的地方就是我添加AOP相关jar包的位置,然后我在applicationContext.xml文件中配置了AOP检测注解的语句,也就是:
<aop:aspectj-autoproxy/>
配置完之后再IDEA中无报错,然后开始用tomcat部署,redeploy后结果就开始报错,错误为:
Java.lang.ClassNotFoundException:org.aspectj.lang.annotation.Around
错误描述以及解决方法请见此博客
http://blog.csdn.net/u013147600/article/details/48132947
但问题是他的解决方法不是我的问题,因为我已经将jar包引入maven了,而且之后我尝试了更换版本号,将maven代码修改一致等等,包括maven的scope配置项修改,都不好用,这个时候我见到了另一篇博文
http://blog.csdn.net/qqhjqs/article/details/51491516
里面一句话提醒了我
是否被打包—查看tomcat文件下对应项目中的web-INF的lib下有没有对应的jar包,然后在看看pom.xml中是怎样配置的
于是我查看了,默认IDEA是将生成的文件和war包都放在target下面的,如果不确定可以去Project-structure的artifacts查看路径,我的查看结果如下图
也就是虽然在maven里已经导入了,但是打包之后的并没有导入,于是我查看了我的代码和相关配置,又搜索了很多有关的信息,都发现自己并没有问题,这个时候google中又有一篇博客被我看到了
https://www.oschina.net/question/726953_139122
看完之后终于恍然大悟,需要在Project Structure设置中将Maven管理的jar包放到lib目录下,之后才会被打包,这个居然还要自己手动去做,耽误了这么久,图片如下
所以到此为止,基本所有在IDEA中maven可能报错NoClassDefFoundError的解决方案都被我列出来了,希望能有所帮助,除了这个坑又是一条好汉!