Java Spring@AspectJ:未应用建议
我正在尝试在调用Java Spring@AspectJ:未应用建议,java,spring,aspectj,Java,Spring,Aspectj,我正在尝试在调用start()之前编写代码 这是我想要建议的TestClass: package com.test; public class TestClass { public static void main(String[] args) { new TestClass().start(); } private void start() { System.out.println("Test started"); } }
start()之前编写代码代码>
这是我想要建议的TestClass:
package com.test;
public class TestClass {
public static void main(String[] args) {
new TestClass().start();
}
private void start() {
System.out.println("Test started");
}
}
这是一个方面,包括建议:
package com.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAspect {
@Before("call(void com.test.TestClass.start())")
public void logBefore(JoinPoint joinPoint) {
System.out.println("logBefore() is running");
}
}
这是我的spring.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<context:annotation-config/>
<context:component-scan base-package="com.test"/>
这方面:
package com.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LogAspect {
@Before("execution(void com.test.TestClass.start(..))")
public void logBefore(JoinPoint joinPoint) {
System.out.println("logging before "
+ joinPoint.getSignature().getName());
}
}
以及Spring-Config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<bean id="myTest" class="com.test.TestClassImpl" />
<bean id="logAspect" class="com.test.LogAspect" />
</beans>
感谢您的帮助。Acall
AspectJ切入点在从另一个类调用该方法时应用。当您自己直接调用它时,您应该使用执行切入点,因此更改:
@Before("call(void com.test.TestClass.start())")
到
也让Spring
创建bean,而不是自己直接创建一个
旁白:注意,您可以将Spring
XML文件与src/resources
中的源文件分开,它们将由ClassPathXmlApplicationContext
拾取,还可以通过Spring应用程序上下文实例化Test
类,不是通过调用new
。虽然我确实使用Maven来进行实际的编织,但我确实让您的代码正常工作了。@Reimeus:请将您的Maven项目发送到我的电子邮件地址好吗?我想弄清楚。你只需要POM,这是一个
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<bean id="myTest" class="com.test.TestClassImpl" />
<bean id="logAspect" class="com.test.LogAspect" />
</beans>
@Before("call(void com.test.TestClass.start())")
@Before("execution(void com.test.TestClass.start())")