【发布时间】:2016-03-06 17:50:36
【问题描述】:
我是spring aop的新手。我有以下课程。 1.界面
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.方面类 @方面 公共类时间AOP { 长开始时间 = 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 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 信息:刷新 org.springframework.context.annotation.AnnotationConfigApplicationContext@5d099f62:启动日期 [Sun Mar 06 12:27:05 EST 2016];上下文层次的根 汇款前 账户 1 余额:10.0 账户2余额:20.0 汇款后 账户1余额:5.0 账户 2 余额:25.0
AOP 永远不会执行。任何人都可以帮助我吗??
【问题讨论】:
-
你的切面类是spring bean吗(有@Component注解)?
-
不行,Aspect 类不是 spring bean,我需要把它做成 bean 吗??
-
是的,看看第 10.2.2 节 (docs.spring.io/spring-framework/docs/current/…)
-
我将@Component 添加到切面类中。但是,当我运行该程序时,我收到以下错误消息。
-
org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.context.event.internalEventListenerProcessor”的bean时出错:bean初始化失败;嵌套异常是 java.lang.IllegalArgumentException: error at ::0 切入点中的正式未绑定
标签: java spring spring-aop