我的 Aspect 课程将是,
@Configuration
@EnableAspectJAutoProxy
@Component
@Aspect
public class AspectClass {
@Before("execution(* com.pointel.aop.test1.AopTest.beforeAspect())")
public void logBefore(JoinPoint joinPoint) {
System.out.println("Before running the beforeAspect() in the AopTest.java class!");
System.out.println("Hijacked Method name : " + joinPoint.getSignature().getName());
System.out.println("************************");
}
}
我的其他java类
public class AopTest {
public void beforeAspect() {
System.out.println("This is beforeAspect() !");
}
}
我的主要课程是
public class MainMethod {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("ApplicationContext/applicationContext.xml");
AopTest test = (AopTest)context.getBean("bean1");
test.beforeAspect();
}
}
我的 applicationContext.xml 是,
<?xml version="1.0" encoding="UTF-8"?>
<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 ">
<bean id="bean1" class="com.pointel.aop.test1.AopTest" />
</beans>
在运行 Main 方法时, in this @Before("execution(* com.pointel.aop.test1.AopTest.beforeAspect())")
inAspectClass
不会在beforeAspect()
in之前执行。AopTest
好的答案绝对值得赞赏。