有人可以帮助我,为什么它不起作用,
的CustomerService
package com.aop.sample;
public interface CustomerService {
void addCustomer();
String addCustomerReturnValue();
void addCustomerThrowException() throws Exception;
void addCustomerAround(String name);
}
CustomerServiceImpl
package com.aop.sample;
import org.springframework.stereotype.Component;
@Component
public class CustomerServiceImpl implements CustomerService{
public void addCustomer(){
System.out.println("addCustomer() is running ");
}
public String addCustomerReturnValue(){
System.out.println("addCustomerReturnValue() is running ");
return "abc";
}
public void addCustomerThrowException() throws Exception {
System.out.println("addCustomerThrowException() is running ");
throw new Exception("Generic Error");
}
public void addCustomerAround(String name){
System.out.println("addCustomerAround() is running, args : " + name);
}
}
LoggingAspect
package com.aop.sample;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before("execution(* com.aop.sample.CustomerService.addCustomer(..))")
public void logBefore(JoinPoint joinPoint) {
System.out.println("logBefore() is running!");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("******");
}
}
TestRun
package com.aop.sample;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestRun {
public static void main(String[] args) {
final ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
"/META-INF/spring/app-context.xml");
CustomerService customerService = (CustomerService) appContext.getBean("customerServiceImpl");
customerService.addCustomer();
}
}
应用-context.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"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Example configuration to get you started.</description>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="com.aop.sample" />
</beans>
- 我的当前输出是 -
addCustomer() is running
-Expected Out put -
logBefore() is running!
hijacked : addCustomer
******
addCustomer() is running
请告知,我做错了什么
答案 0 :(得分:2)
使用LoggingAspect
注释您的@Component
课程,以便生成一个bean
@Aspect
@Component
public class LoggingAspect {
并注册。或者,在<bean>
的上下文文件中添加LoggingAspect
元素。
答案 1 :(得分:2)
如果您不想使用注释,请将其添加到app-context.xml。
<bean name="loggingAspect" class="com.aop.sample.LogginAspect"/>
http://docs.spring.io/spring/docs/3.1.0.M1/spring-framework-reference/html/aop.html#aop-at-aspectj