2014-01-13 45 views
0

有人能帮助我,为什么它不工作,AOP @Before方法不是要求

的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(); 

    } 

} 

APP-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> 

-My电流输出为 -

addCustomer() is running 

-Expected缺货put-

logBefore() is running! 
hijacked : addCustomer 
****** 
addCustomer() is running 

请咨询,我做错了什么

回答

2

@Component这样可以生成豆

@Aspect 
@Component 
public class LoggingAspect { 

并注册标注您LoggingAspect类。或者,在LoggingAspect的上下文文件中添加一个<bean>元素。

+1

谢谢你救我的时候:) – Suranga