通过 切点注解 实现AOP:
1、声明切点注解:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})//该注解 是 方法注解
public @interface Invokelog {
}
2、声明切点类:
@Component
@Aspect
public class MyAspect {
@Pointcut("@annotation(com.package.aspet.Invokelog) )")//切点注解
public void pc(){//此方法是 切点pt():确定对com.package.service下的所有类的 所有方法 增强
}
@Before("pc()")
public void methodBefore(){//此方法:标注使用哪个切点;增强的业务
System.out.println("hello aop...");
}
}
3、业务类使用切点注解:
@Service("studentService")
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Invokelog
public Student getStudentById(int id) {
return studentDao.getStudentById(3);
}
}
总结:到底 用切点表达式 ? 还是用切点注解 ? 来实现AOP呢?
如果切点表达式有规律,则使用切点表达式;否则使用切点注解。