方式一:使用Spring的API实现
首先导入依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
编写service类
public interface userService {
public void add();
public void update();
public void delete();
public void get();
}
public class UserServiceImpl implements userService{
@Override
public void add() {
System.out.println("添加一个用户");
}
@Override
public void update() {
System.out.println("更新一个用户");
}
@Override
public void delete() {
System.out.println("删除一个用户");
}
@Override
public void get() {
System.out.println("查询一个用户");
}
}
编写
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {
System.out.println(method.getName()+"方法被执行,返回结果为:");
}
}
public class BeforeLog implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
在xml文件中注册
<bean id="userService" class="grandzio.service.UserServiceImpl"/>
<bean id="logbefore" class="grandzio.log.BeforeLog"/>
<bean id="afterreturnLog" class="grandzio.log.AfterLog"/>
<!-- 配置aop需要导入aop的约束
xmlns:aop = "http://www.springframework.org/schema/aop"
xsi:schemaLocation= http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
-->
<!-- 方式一:使用原生Spring API 接口 -->
<aop:config>
<!-- 切入点 expression表达式,execution(要执行的位置 * * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* grandzio.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="logbefore" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterreturnLog" pointcut-ref="pointcut"/>
<!-- 执行环绕增加-->
</aop:config>
测试:
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
userService adddr = (userService) context.getBean("userService");
adddr.add();
}
}
运行结果:
方法二:自定义类
public class DiyPointCut {
public void before(){
System.out.println("=============方法执行前===========");
}
public void after(){
System.out.println("=============方法执行后===========");
}
}
<!-- 方式二:自定义类-->
<bean id="diy" class="grandzio.diy.DiyPointCut"/>
<aop:config>
<!-- aspect切面 ref是要引用的类-->
<aop:aspect ref="diy">
<!-- 切入点-->
<aop:pointcut id="point" expression="execution(* grandzio.service.UserServiceImpl.*(..))"/>
<!-- 通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
运行测试:
方式三:使用注解实现!
@Aspect //标注这个类是一个切面
public class AnnotationPointcut {
//需要写切入点
@Before("execution(* grandzio.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("-----------方法执行前-------------");
}
@After("execution(* grandzio.service.UserServiceImpl.*(..))")
public void After(){
System.out.println("---------------方法执行后----------");
}
}
<!-- 方式三-->
<bean id="annotationPointCut" class="grandzio.diy.AnnotationPointcut"/>
<!-- 开启注解支持!自动代理 -->
<aop:aspectj-autoproxy/>
测试: