我现在想要算一下我的方法。所以我使用Around方面,但它不起作用。既没有注释也没有XML。 Dubgger表明Aspect尚未被调用。不幸的是,没有例子有帮助。
TimeCountAspect.java
@Aspect
@Component
public class TimeCountAspect {
@Around("execution(* com.springapp.Calculation.Calculator.calculate(..))")
public Object timeCounterClass(ProceedingJoinPoint joinpoint) {
Object result = null;
try {
System.out.println("Preparing to calculate");
long start = System.currentTimeMillis(); // Before
result = joinpoint.proceed(); // Method invoke
long end = System.currentTimeMillis(); // After
System.out.println("Calculation took " + (end - start)
+ " milliseconds.");
} catch (Throwable t) {
System.out.println("Nothing happend!");
}
return result;
}
}
它完美地看到所有类,并且它们与XML中的bean声明相关联。 (IntelliJ Idea显示它)。 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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/"/>
<!-- Enable AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy/>
<aop:aspectj-autoproxy proxy-target-class="true">
<aop:include name="timeCounterAspect" />
</aop:aspectj-autoproxy>
<bean id="timeCounterAspect" class="com.springapp.Calculation.TimeCountAspect"/>
<aop:config>
<aop:aspect id="timeCount" ref="timeCounterAspect">
<aop:pointcut id="calculation" expression=
"execution(* com.springapp.Calculation.Calculator.calculate(..))"/>
<aop:around
pointcut-ref="calculation"
method="timeCounterClass"/>
</aop:aspect>
</aop:config>
................factories and other things............
</beans>
POM.XML 代码段
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspect.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspect.version}</version>
</dependency>
我想要应用于哪个方面的calculate()方法。 方法调用()调用方法 calculate()
public BigDecimal calculate(int numberOfThreads, int precision) {
................
return summ;
}
public BigDecimal call() throws Exception {
return calculate(Calculator.numberOfThreads, Calculator.precision);
}
答案 0 :(得分:2)
您正在使用 Spring AOP ,它在代理模型中有效。这意味着它会在实际的bean上创建一个包装代理。这种设置的众所周知的局限性是:
this
或super
来电)public
成员(private
/ protected
无法拦截)@Aspect
注释将其标记为方面,因此将其从自动代理中排除。您正在尝试拦截bean本身内的本地调用,这是使用此设置无法实现的。
要克服上述限制,您需要设置native AspectJ based weaving environment。