2016-03-06 127 views
1

我是新来的春天aop。我有以下课程。 1.接口春季AOP不能正常工作

public interface AccountService { 

public void transferMoney(
     long sourceAccountId, long targetAccountId, double amount); 

public void depositMoney(long accountId, double amount) throws Exception; 

public Account getAccount(long accountId); 

} 

2.方面类 @Aspect 公共类TimeAOP { 长开始时间= 0;

@Pointcut("execution(* *.transferMoney(..))")// the pointcut expression 
private void anyOldTransfer() {}// the pointcut signature 

@After("anyOldTransfer()") 
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { 
    long elapsedTime = System.nanoTime() - startTime; 
    String className = target.getClass().getCanonicalName(); 
    String methodName = method.getName(); 
    System.out.println("Execution of " + className + "#" + methodName 
      + " ended in " + new BigDecimal(elapsedTime).divide(
      new BigDecimal(1000000)) + " milliseconds"); 

} 

@Before("anyOldTransfer()") 
public void before(Method method, Object[] args, Object target) throws Throwable { 
    System.out.println("Starting"); 
    startTime = System.nanoTime(); 
} 

}

3.配置类

@Configuration 
@EnableAspectJAutoProxy 
@ComponentScan(basePackages = "com.xxx") 
public class Ch2BeanConfiguration { 
@Bean 
public AccountService accountService() { 
    AccountServiceImpl bean = new AccountServiceImpl(); 
    bean.setAccountDao(accountDao()); 
    return bean; 
} 

@Bean 
public AccountDao accountDao() { 
    AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl(); 
    //depedencies of accountDao bean will be injected here... 
    return bean; 
} 
} 

4.测试类

public class Main { 
public static void main(String[] args){ 
    AnnotationConfigApplicationContext applicationContext = 
      new AnnotationConfigApplicationContext(Ch2BeanConfiguration.class); 
    AccountService accountService = applicationContext.getBean("accountService", 
      AccountService.class); 
    System.out.println("Before money transfer"); 
    System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance()); 
    System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance()); 
    accountService.transferMoney(1, 2, 5.0); 
    System.out.println("After money transfer"); 
    System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance()); 
    System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance()); 
} 
} 

输出: 2016年3月6日下午12时27分05秒org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 信息:刷新org.spring[email protected]5d099f62:启动日期[太阳Mar 06 12:27:05 EST 2016];上下文结构 根汇款 账户余额1之前:10.0 账户余额2:20.0 汇款 账户余额1后:5.0 账户余额2:25.0

的AOP是永远不会执行。任何人都可以帮助我?

+0

是您的方面类是一个Spring bean(有@Component注释)? –

+0

不,Aspect类不是一个spring bean,是否需要将它作为一个bean? –

+0

是的,请参阅第10.2.2节(http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/aop.html) –

回答

0

既然你不使用你需要从@Configuration返回一个看点豆XML

@Bean 
public TimeAOP timeAspect() { 
    return new TimeAOP(); 
} 
+0

我试过了,但我收到以下错误消息org.springframework.beans.factory.BeanCreationException:创建名为org.springframework的bean时出错。 context.event.internalEventListenerProcessor':bean初始化失败;嵌套的异常是java.lang.IllegalArgumentException:在:: 0的错误正式在切入点处未绑定 –

+0

谢谢。我想到了。建议的参数不正确。 –