AOP
- AOP(Aspect Oriented Programming)意为:面向切面编程
- AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型
使用Spring实现AOP
方式一:配置增强类
- 导入依赖
<!--织入包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
2.编写要实现的接口
package com.yang.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void serach();
}
- 编写实现类
import com.yang.service.UserService;
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 serach() {
System.out.println("查询用户");
}
}
- 编写增强类,前置增强,后置增强
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogAfter implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法,返回了"+returnValue);
}
}
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()
+"的"+method.getName()+"方法被执行了");
}
}
- 注册到bean,相当于创建对象
<bean id="userService" class="com.yang.service.Impl.UserServiceImpl"/>
<bean id="log" class="com.yang.config.LogAfter"/>
<bean id="afterlog" class="com.yang.config.LogAfter"/>
- 添加AOP的配置文件
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
- 配置AOP
<!--aop的配置-->
<aop:config>
<!--切入点,增强那些-->
<aop:pointcut id="pc-userservice" expression="execution(* com.yang.service.Impl.UserServiceImpl.*(..))"/>
<!--环绕-->
<aop:advisor advice-ref="log" pointcut-ref="pc-userservice"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pc-userservice"/>
</aop:config>
- 测试
@Test
public void test2(){
// bean就是创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
方式二:切面配置,有增强方式的类(bean就是在创建对象)
“execution(* com.yang.service.Impl.UserServiceImpl.*(…))”
这是创对象的关键,一定要写对
- 导入依赖
- 编写接口 ,编写实现类
package com.yang.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void serach();
}
package com.yang.service.Impl;
import com.yang.service.UserService;
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 serach() {
System.out.println("查询用户");
}
}
- 编写增强类(含多个方法)
package com.yang.config;
public class zichuanglei {
public void before(){
System.out.println("=====qian=====");
}
public void after(){
System.out.println("=====hou=====");
}
}
- 配置到bean,完成 xml 的配置
<bean id="userService" class="com.yang.service.Impl.UserServiceImpl"/>
<bean id="ces" class="com.yang.config.zichuanglei"/>
<aop:config>
<!--配置切面,需要配置标签来控制-->
<aop:aspect ref="ces">
<aop:pointcut id="pc-userservice" expression="execution(* com.yang.service.Impl.UserServiceImpl.*(..))"/>
<!--方法来自于切面-->
<aop:before method="before" pointcut-ref="pc-userservice"/>
<aop:after method="after" pointcut-ref="pc-userservice"/>
</aop:aspect>
</aop:config>
</beans>
- 测试
@Test
public void test2(){
// bean就是创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
注解实现
- 倒包
- 编写接口及实现类
- 编写增强类
package com.yang.config;
import org.aspectj.lang.ProceedingJoinPoint;
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 AnnotationPointcut {
@Before("execution(* com.yang.service.Impl.UserServiceImpl.*(..))")
public void before(){
System.out.println("=====qian=====");
}
@After("execution(* com.yang.service.Impl.UserServiceImpl.*(..))")
public void after(){
System.out.println("=====hou=====");
}
@Around("execution(* com.yang.service.Impl.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕前");
System.out.println(joinPoint.getSignature());
Object proceed = joinPoint.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
- 注册bean
<bean id="userService" class="com.yang.service.Impl.UserServiceImpl"/>
<bean id="AnnotationPointcut" class="com.yang.config.AnnotationPointcut"/>
<!--让spring识别注解,开启识别-->
<aop:aspectj-autoproxy/>
<!--aop的配置-->
- 测试
MyBatis-Spring