什么是AOP
AOP:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率
导入AOP依赖
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
方法一:使用SpringAIP的接口
代码实现
UserService
package com.Service;
public interface UserService {
void add();
void delete();
void update();
void select();
}
UserServiceImpl
package com.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 ("用户查询");
}
}
Log
package com.logs;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
/**
*method:要自信的目标对象的方法
* objects:参数
* o:目标对象
*/
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println (o.getClass ().getName () + "的" + method.getName () + "被执行了");
}
}
AfterLog
package com.logs;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println ("执行了" + method.getName () + "方法,返回结果为:" + o);
}
}
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-->
<bean id="userService" class="com.Service.UserServiceImpl"/>
<bean id="log" class="com.logs.AfterLog"/>
<bean id="afterLog" class="com.logs.AfterLog"/>
<!--配置AOP:需要导入aop的约束-->
<!--使用原生SpringAPI接口-->
<aop:config>
<!--切入点:expression:表达式 execution(要执行的位置)-->
<aop:pointcut id="pointcut" expression="execution(* com.Service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
test
import com.Service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void Test() {
ApplicationContext context = new ClassPathXmlApplicationContext ("ApplicationContext.xml");
UserService userService = (UserService) context.getBean ("userService");
userService.add ();
}
}
自定义来实现
DiyPointCut
package com.diy;
public class DiyPointCut {
public void before() {
System.out.println ("方法执行前");
}
public void after() {
System.out.println ("方法执行后");
}
}
ApplicationContext.xml(前面代码与方式一一样)
<?xml version="1.0" encoding="UTF-8"?>
<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-->
<bean id="userService" class="com.Service.UserServiceImpl"/>
<bean id="log" class="com.logs.AfterLog"/>
<bean id="afterLog" class="com.logs.AfterLog"/>
<!--方式二:自定义-->
<bean id="diy" class="com.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.Service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:before method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
注解实现AOP
AnnotationPointCut
package com.diy;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//使用注解方式实现AOP
@Aspect//标注这个类时一个切面
public class AnnotationPointCut {
@Before("execution(* com.Service.UserServiceImpl.*(..))")
public void before() {
System.out.println ("方法执行前");
}
@After("execution(* com.Service.UserServiceImpl.*(..))")
public void after() {
System.out.println ("方法执行后");
}
}
ApplicationContext
<?xml version="1.0" encoding="UTF-8"?>
<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-->
<bean id="userService" class="com.Service.UserServiceImpl"/>
<bean id="log" class="com.logs.AfterLog"/>
<bean id="afterLog" class="com.logs.AfterLog"/>
<bean id="annotationPointCut" class="com.diy.AnnotationPointCut"/>
<aop:aspectj-autoproxy/>
</beans>