这是我走的大概三天的坑,一直搞不清楚原因,今天总算把问题解决了。
首先说问题的源头,是我使用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.0</modelVersion>
<groupId>com</groupId>
<artifactId>reeco</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>reeco Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>java.servlet.api</id>
<name>Repository hosting the java servlet api artifacts</name>
<url>http://repo1.maven.org/maven2/javax/servlet/javax.servlet-api/3.0.1/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<properties>
<junit-version>4.12</junit-version>
<spring-version>4.3.2.RELEASE</spring-version>
<mybatis-version>3.4.1</mybatis-version>
<mybatis-spring-version>1.3.0</mybatis-spring-version>
<druid-version>1.0.24</druid-version>
<fastjson-version>1.2.16</fastjson-version>
<mysql-connection-version>5.1.6</mysql-connection-version>
<mybatis-generator-version>1.3.2</mybatis-generator-version>
<pagehelper-version>4.0.1</pagehelper-version>
<slf4j-version>1.7.12</slf4j-version>
<log4j-version>1.2.17</log4j-version>
<jstl-version>1.2</jstl-version>
<taglibs-version>1.1.2</taglibs-version>
<aspectj-version>1.8.5</aspectj-version>
<operation-version>1.0</operation-version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</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-test</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring-version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid-version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connection-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${taglibs-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-version}</version>
</dependency>
<!--使用AspectJ方式注解需要相应的包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj-version}</version>
</dependency>
<!--使用AspectJ方式注解需要相应的包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj-version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-simple</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>generator*.*</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<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的解决方案都被我列出来了,希望能有所帮助,除了这个坑又是一条好汉!