這是我走的大概三天的坑,一直搞不清楚原因,今天總算把問題解決了。
首先說問題的源頭,是我使用SSM框架寫j2ee程序,要使用到AOP,于是就在網(wǎng)上查找了如何使用maven添加AOP相關(guān)的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方式注解需要相應(yīng)的包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj-version}</version>
</dependency>
<!--使用AspectJ方式注解需要相應(yīng)的包-->
<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相關(guān)jar包的位置,然后我在applicationContext.xml文件中配置了AOP檢測注解的語句,也就是:
<aop:aspectj-autoproxy/>
配置完之后再IDEA中無報錯,然后開始用tomcat部署,redeploy后結(jié)果就開始報錯,錯誤為:
Java.lang.ClassNotFoundException:org.aspectj.lang.annotation.Around
錯誤描述以及解決方法請見此博客
http://blog.csdn.net/u013147600/article/details/48132947
但問題是他的解決方法不是我的問題,因為我已經(jīng)將jar包引入maven了,而且之后我嘗試了更換版本號,將maven代碼修改一致等等,包括maven的scope配置項修改,都不好用,這個時候我見到了另一篇博文
http://blog.csdn.net/qqhjqs/article/details/51491516
里面一句話提醒了我
是否被打包---查看tomcat文件下對應(yīng)項目中的web-INF的lib下有沒有對應(yīng)的jar包,然后在看看pom.xml中是怎樣配置的
于是我查看了,默認(rèn)IDEA是將生成的文件和war包都放在target下面的,如果不確定可以去Project-structure的artifacts查看路徑,我的查看結(jié)果如下圖
也就是雖然在maven里已經(jīng)導(dǎo)入了,但是打包之后的并沒有導(dǎo)入,于是我查看了我的代碼和相關(guān)配置,又搜索了很多有關(guān)的信息,都發(fā)現(xiàn)自己并沒有問題,這個時候google中又有一篇博客被我看到了
https://www.oschina.net/question/726953_139122
看完之后終于恍然大悟,需要在Project Structure設(shè)置中將Maven管理的jar包放到lib目錄下,之后才會被打包,這個居然還要自己手動去做,耽誤了這么久,圖片如下
所以到此為止,基本所有在IDEA中maven可能報錯NoClassDefFoundError的解決方案都被我列出來了,希望能有所幫助,除了這個坑又是一條好漢!
