在做spring aop练习中发现使用@Aspect不起作用,问题代码:
package org.spring.ioc.core.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
public class MyAspect {
@Before("execution(* org.spring.ioc.core.aop.dao.*.*(..))")
public void before(){
System.out.println("在执行dao中实现类之前调用");
}
}
经过查找发现,原来在使用@Aspect的时候也需要将其注入到IOC容器中,使用在原来代码的基础上加上@Component即可,修改后为:
package org.spring.ioc.core.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {
@Before("execution(* org.spring.ioc.core.aop.dao.*.*(..))")
public void before(){
System.out.println("在执行dao中实现类之前调用");
}
}