定义接口
package service;
public interface UserService {
void add();
void delete();
void update();
void select();
}
接口实现类
package service;
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("添加了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
public void update() {
System.out.println("更新了一个用户");
}
public void select() {
System.out.println("查找了一个用户");
}
}
1.通过原生Spring API接口实现
定义方法执行前通知
package Log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"的"+method.getName()+"方法被执行了");
}
}
定义方法执行后通知
package Log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class MethodAfterAdvice implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(method.getName()+"被执行了,返回结果为"+returnValue);
}
}
配置xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="service.UserServiceImpl"/>
<bean id="log" class="Log.Log"/>
<bean id="after" class="Log.MethodAfterAdvice"/>
<!-- 方式1使用原生Spring API接口-->
<aop:config>
<!-- 切入点:expression:表达式,execution(要执行的位置 * * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* service.UserServiceImpl.*(..))"/>
<!-- 增加执行环绕-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="after" pointcut-ref="pointcut"/>
</aop:config>
</beans>
2.通过自定义类
自定义类实现方法前调用函数和方法后调用函数
package diy;
public class DiyClass {
//方法执行前调用函数
public void before(){
System.out.println("=====before======");
}
//方法执行后调用函数
public void after(){
System.out.println("=====after======");
}
}
配置xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="service.UserServiceImpl"/>
<!-- 方式二自定义类-->
<bean id="div" class="diy.DiyClass"/>
<aop:config>
<!-- 定义切入面-->
<aop:aspect ref="div">
<!-- 定义切入点-->
<aop:pointcut id="point" expression="execution(* service.UserServiceImpl.*(..))"/>
<!-- 定义通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
通过注解实现
定义切面
package annotationAspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AnnotationAspect {
@Before("execution(* service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=======before before=====");
}
@After("execution(* service.UserServiceImpl.*(..))")
public void After(){
System.out.println("=======After After=====");
}
@Around("execution(* service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint pj) throws Throwable {
System.out.println("环绕前");
Signature signature = pj.getSignature();
System.out.println(signature);
Object proceed = pj.proceed();//执行方法
System.out.println("环绕后");
System.out.println(proceed);
}
}
配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="service.UserServiceImpl"/>
<!-- 方式三,通过注解实现aop-->
<bean id="annotationAspect" class="annotationAspect.AnnotationAspect"/>
<!-- 开启注解支持-->
<aop:aspectj-autoproxy/>
</beans>
统一测试文件
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
import service.UserServiceImpl;
public class Test1 {
@Test
public void test1(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService) context.getBean("userService");
System.out.println(service.hashCode());
service.add();
System.out.println(service.hashCode());
}
}