我在 spring AOP 上有一个“Hello word”应用程序并由 XML 配置,它看起来像这样:
public class CustomerBoImpl {
public CustomerBoImpl() {
super();
}
protected void addCustomer(){
System.out.println("addCustomer() is running ");
}
}
public class App {
public static void main(String[] args) throws Exception {
ApplicationContext appContext =
new ClassPathXmlApplicationContext("Spring-Customer.xml");
CustomerBoImpl customer =
(CustomerBoImpl) appContext.getBean("customerBo");
customer.addCustomer();
}
}
我的弹簧配置如下所示:
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<!-- this switches on the load-time weaving -->
<context:load-time-weaver aspectj-weaving="on" />
<bean id="customerBo" class="com.mkyong.saad.CustomerBoImpl"
scope="singleton" />
<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.saad.LoggingAspect" />
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect">
<!-- @Before -->
<aop:pointcut id="pointCutBefore"
expression="execution(* com.mkyong.saad.CustomerBoImpl.addCustomer(..))" />
<aop:before method="logBefore" pointcut-ref="pointCutBefore" />
<!-- @After -->
<aop:pointcut id="pointCutAfter"
expression="execution(* com.mkyong.saad.CustomerBoImpl.addCustomer(..))" />
<aop:after method="logAfter" pointcut-ref="pointCutAfter" />
</aop:aspect>
</aop:config>
由于受保护的方法,这不起作用,所以我尝试将加载时间编织与 aop.xml 一起使用,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
<weaver>
<!-- only weave classes in our application-specific packages -->
<include within="com.mkyong.saad.*"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="com.mkyong.saad.LoggingAspect"/>
</aspects>
</aspectj>
方面的源代码:
public class LoggingAspect {
public void logBefore(JoinPoint joinPoint) {
System.out.println("logBefore() is running!");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("******");
}
public void logAfter(JoinPoint joinPoint) {
System.out.println("logAfter() is running!");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("******");
}
}
但它不起作用,只有当我更改为注释配置时。求救