Spring——Spring中的AOP实现
编写业务层接口
package com.muhan.service;
//service层接口
public interface UserService {
void add();
void delete();
void update();
void query();
}
编写业务层实现类
package com.muhan.service;
//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 query() {
System.out.println("查询了一条用户数据");
}
}
定义日志增加类实现
package com.muhan.log;
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()+"方法被执行了");
}
}
package com.muhan.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
//日志之前
public class AfterLog implements AfterReturningAdvice {
/*
o:返回值
method:被调用的方法
objects:被调用方法的参数
o1:被调用的目标对象
*/
public void afterReturning(Object o, Method method,
Object[] objects, Object o1) throws Throwable {
System.out.println(o1.getClass().getName()+"的"
+method.getName()+"方法被执行了,返回值为:"+o);
}
}
需要导入aop的织入包
https://mvnrepository.com/artifact/org.aspectj/aspectjweaver/1.8.9
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
编写Spring核心配置文件
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册UserServiceImpl的bean-->
<bean id="userService" class="com.muhan.service.UserServiceImpl"/>
<!--注册日志类的bean-->
<bean id="log" class="com.muhan.log.Log"/>
<bean id="afterLog" class="com.muhan.log.AfterLog"/>
<!--
使用Spring的aop切入
1.导入约束:
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation中添加:
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"
-->
<!--
2.<aop:comfig>
-->
<aop:config>
<!--
定义切入点:
expression表达式:表示要切入点的位置
语法:execution(类修饰 符类的全路径.方法(参数))
execution(* com.muhan.service.UserServiceImpl.*(..)):
表示所有的类修饰符,com.muhan.service.UserServiceImpl类,下的全部方法,参数不限
-->
<aop:pointcut id="pointcut" expression="execution(* com.muhan.service.UserServiceImpl.*(..))"/>
<!--
执行通知,增强
advice-ref:要横切进去的业务
pointcut-ref:切入点
-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
测试
import com.muhan.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");
UserService service = (UserService) context.getBean("userService");
/*
spring调用的真实对象是userService
在暗箱中:动态的修改userService,在方法的前后或者其他通知的地方增加了我们切入的代码
这样我们就可以实现只调用原来的对象,产生增加新的业务的功能
*/
service.add();
}
}
结果展示
AOP总结:
- AOP的本质就是动态代理
- AOP的底层实现机制就是横向编程
- 使用AOP的时候别忘记了导入一个包,进行aop植入的包:aspectjweaver
- 使用的时候千万别遗忘了切面
- 三种实现AOP的方法:使用SpringAPI来实现,使用自定义类来实现,使用注解实现