在https://blog.csdn.net/qq_41534115/article/details/84329611这一篇文章中,大家对spring已经有了一定的认识
那么接下来,我来讲解AOP的相关操作
在上一篇文章的基础上配置spring.xml文件
<!--日志的bean 通知:前置通知、后置通知、环绕通知-->
<bean id="log" class="com.jie.util.LogManager"></bean>
<!--配置切面和切入点信息-->
<aop:config proxy-target-class="true">
<!--
配置切入点
id:切入点名称
expression:指定切入点的表达式
(* com.jie.service.impl.*.*(..))
第一个*:代表方法的返回值类型 (任意返回类型)
com.jie.service.impl:包名
第二个*:表示impl包下的所有类
第三个*:表示类下的所有方法
(..):表示任意参数的类型
-->
<aop:pointcut id="pt" expression="execution(* com.jie.service.impl.*.*(..))"></aop:pointcut>
<!--把切面和切入点关联起来,这个过程就是织入-->
<aop:advisor advice-ref="log" pointcut-ref="pt"></aop:advisor>
</aop:config>
<bean id="myLog" class="com.jie.util.Log"></bean>
<aop:config>
<!--切入点-->
<aop:pointcut id="pt1" expression="execution(* com.jie.dao.impl.*.*(..))"></aop:pointcut>
<!--自定义切面-->
<aop:aspect ref="myLog">
<!--通知:aop:before前置通知-->
<aop:before method="before" pointcut-ref="pt1"></aop:before>
<!--aop:after-returning:执行成功后通知、aop:after-throwing抛出异常后通知-->
<aop:after-returning method="after" pointcut-ref="pt1"></aop:after-returning>
<!--环绕通知-->
<aop:around method="around" pointcut-ref="pt1"></aop:around>
</aop:aspect>
</aop:config>
然后添加util包
LogManger类是使用的spring的切面
package com.jie.util;
/**
* 日至切面
* */
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class LogManager implements MethodBeforeAdvice, AfterReturningAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(method.getName()+"方法执行前日志--------"+System.currentTimeMillis());
}
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println(method.getName()+"方法执行后日志--------"+System.currentTimeMillis());
}
}
Log类,自定义切面
package com.jie.util;
import org.aspectj.lang.ProceedingJoinPoint;
public class Log {
public void before(){
System.out.println("这是方法之前执行的!");
}
public void after(){
System.out.println("这是方法之后执行的!");
}
public void afterThrowing(){
System.out.println("这是方法之后执行的异常日志!");
}
public void around(ProceedingJoinPoint joinPoint){
System.out.println("这是方法之前执行的环绕通知!");
try {
joinPoint.proceed();
}catch (Throwable throwable){
throwable.printStackTrace();
}
System.out.println("这是方法之后执行的环绕通知!");
}
}
TestUser测试类
package com.jie;
import com.jie.domain.User;
import com.jie.service.UserService;
import com.jie.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class TestUser {
@Test
public void test1(){
BeanFactory factory=new ClassPathXmlApplicationContext("/spring.xml");
UserServiceImpl userService=(UserServiceImpl) factory.getBean("userService");
// UserService userService1=(UserService)factory.getBean("userService");
userService.hello();
// userService1.hello();
// userService.hello(22);
// User user=(User)factory.getBean("user");
// userService.hello(user);
// List list = userService.getList();
// userService1.hello(list);
}
}
结果