我从未使用Spring AOP并尝试配置我的第一个bean。我似乎正确配置它,但我得到一个例外,找不到bean。
我的方面是 -
@Aspect
@Component
public class IdentificationAspect {
@Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.*(..))")
public void logBefore(JoinPoint joinPoint) throws Throwable {
System.out.println("logBefore() is running!");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("******");
}
}
我的AOP找不到的是 -
package ru.sbt.filial.cards.aspect;
import org.springframework.stereotype.Component;
@Component
public class SomeBean {
public void printSmth() {
System.out.println("!!!!!!!!!!!");
}
}
我有以下异常 -
Caused by: java.lang.IllegalArgumentException: warning no match for this type name: ru.sbt.filial.cards.aspect.SomeBean [Xlint:invalidAbsoluteTypeName]
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:301)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:207)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getFallbackPointcutExpression(AspectJExpressionPointcut.java:358)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.matches(AspectJExpressionPointcut.java:255)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:208)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:262)
at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:294)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:117)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:87)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:68)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:356)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:319)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1629)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:162)
... 165 more
我有以下maven依赖项 -
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.3</version>
</dependency>
我的spring applicationContext.xml配置是 -
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<mvc:annotation-driven/>
<aop:aspectj-autoproxy/>
<bean id="someBean" class="ru.sbt.filial.cards.aspect.SomeBean">
</bean>
更新 我添加到我的Spring配置行
<context:component-scan base-package="ru.sbt.filial.cards.aspect"/>
但仍然是相同的消息。我还试图注释不同的方式 - 如果我没有指定bean,即写 -
@Before("execution(* ru.sbt.filial.cards.aspect.*.*(..))")
而不是
@Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.*(..))")
我在加载时没有错误,但是没有调用aop方法。
我也试着像这样注释
@Before("this(ru.sbt.filial.cards.aspect.SomeBean) and execution(* printSmth(..))")
但结果相同 - 此类型名称不匹配。还有更多的想法吗?
答案 0 :(得分:15)
我和你有同样的问题,我已经解决了我的问题。我给你两个建议,你可以尝试一下。 请将方面类中的@Before注释修改为以下之一:
@Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.logBefore(..))")
或
@Before("execution(* ru.sbt.filial.cards.aspect.SomeBean..*(..))")
希望它有所帮助。
答案 1 :(得分:1)
要成功运行,请在您的spring applicationContext.xml配置文件中添加
<context:component-scan base-package="ru.sbt.filial.cards.aspect"/>
在SomeBean类中使用@Component
注释
更新
尝试将方法名称添加到@Before:
@Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.logBefore(..))")
答案 2 :(得分:0)
你输了课。
@Before("execution(* ru.sbt.filial.cards.aspect.**SomeBean.***(..))")
以您的方式,“ somebean”是一个包,它后面的*是一个方法,您应该放置类 它们之间。 您可以这样做
@Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.* .*(..))")
或
@Before("execution(* ru.sbt.filial.cards.aspect.SomeBean..*(..))")