我继承了一个使用 Spring AOP 的 webapps 代码库。 Maven 用于编译它,在 Tomcat 6.x 服务器上运行。我尝试通过 Eclipse 的“Run As”运行该项目,还尝试在命令行上运行 mvn clean install
,然后更新 tomcat webapps
文件夹。两者都曾经给我同样的异常(exception)。
严重:上下文初始化失败
org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/rest-servlet.xml] 中定义名称为“com.xyz.ABCAspect#0”的 bean 创建时出错:找不到匹配的工厂方法:工厂方法'的方面()'。检查具有指定名称的方法是否存在并且它是静态的。
我是 Spring AOP 的新手,实际上是一般的 AOP。经过一些研究,当我通过 Eclipse 运行服务器时,通过将项目转换为 AspectJ
项目,我能够解决这个问题。这样做之后,我不再收到这些错误。
这对我来说似乎很神奇。因为我在进行手动编译/部署时仍然面临同样的问题,所以我希望能够归零并解决这个问题。
这是我的 WEB-INF/rest-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<aop:aspectj-autoproxy/>
<bean class="com.xyz.rest.aop.ABCAspect" factory-method="aspectOf">
<property name="converter" ref="defaultConverter"/>
</bean>
这是我的ABCAspect
:
@Aspect
public class ABCAspect {
private Converter converter;
@Around("execution(@com.xyz.converter.DTOType * *(..)) && @annotation(dtoType)")
public Object convertType(ProceedingJoinPoint pjp, DTOType dtoType) throws Throwable {
//...
}
...
}
这是我的 pom.xml
的片段:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal> <!-- use this goal to weave all your main classes -->
<goal>test-compile</goal> <!-- use this goal to weave all your test classes -->
</goals>
</execution>
</executions>
<configuration>
<source>1.6</source>
<target>1.6</target>
<outxml>true</outxml>
<showWeaveInfo>false</showWeaveInfo>
<Xlint>warning</Xlint>
<verbose>true</verbose>
</configuration>
</plugin>
让我感到困惑的是,在我声明我的项目是AspectJ
性质之后发生了什么如此神奇的事情? eclipse 停止了提示。
感谢任何指点。
最佳答案
我找到了解决方案。发生这种情况是因为我包装了 <plugins>
在<pluginManagement>
标签。
在我删除它之后,我的 aspectj 插件开始正常工作,就像开始执行一样。有关此检查的详细信息this stackoverflow question .
关于java - 在 Maven 编译项目中使用 Spring AOP 获取 "No factory method ' aspectOf( )'"发现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22259760/