手动实现-proxy
切面类
package com.mu.aop_xml.jdk_proxy.service.impl;
/**
* 增强功能与切入点的结合
*/
public class MyAspect {
public void before() {
System.out.println("开启事务");
}
public void after(){
System.out.println("提交事务");
}
}
目标类
package com.mu.aop_xml.jdk_proxy.service;
public interface UserService {
void addUser();
void updateUser();
void deleteUser();
int queryById(int id);
}
package com.mu.aop_xml.jdk_proxy.service.impl;
import com.mu.aop_xml.jdk_proxy.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("新增用户");
try {
int a=10/0;
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void updateUser() {
System.out.println("更新用户");
}
@Override
public void deleteUser() {
System.out.println("删除用户");
}
@Override
public int queryById(int id) {
System.out.println("根据id查找并返回 id");
return id;
}
}
工厂
package com.mu.aop_xml.jdk_proxy.service.impl;
import com.mu.aop_xml.jdk_proxy.service.UserService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class MyBeanFactory {
public static UserService createUserService(){
// 1.创建目标类对象
UserService userService = new UserServiceImpl();
// 2.声明切面类对象
MyAspect myAspect = new MyAspect();
// 3.把切面类两个方法 应用到 目标类
/* ClassLoader loader, 类加载器
Class<?>[] interfaces, 接口,接口的方法会被拦截
InvocationHandler h) 处理 */
UserService userService1= (UserService) Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(),
userService.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
myAspect.before();
Object object = method.invoke(userService, args);
myAspect.before();
System.out.println("拦截的返回值为"+object);
return object;
}
}
);
return userService1;
}
}
测试
package com.mu.aop_xml.jdk_proxy.test;
import com.mu.aop_xml.jdk_proxy.service.UserService;
import com.mu.aop_xml.jdk_proxy.service.impl.MyBeanFactory;
/**
* 自用实现AOP编程,使用jdk代理实现 必须是接口+实现类的形式 只是实现类使用字节码增强框架cglib
* @date 2019/09/26
*/
public class Test1 {
public static void main(String[] args) {
UserService userService = MyBeanFactory.createUserService();
userService.addUser();
System.out.println("---------------华丽的分割线----------------");
userService.queryById(22);
}
}
手动实现-cglib
切面类
package com.mu.aop_xml.cglib.service;
/**
* 增强功能与切入点的结合
*/
public class MyAspect {
public void before() {
System.out.println("开启事务");
}
public void after(){
System.out.println("提交事务");
}
}
目标类
package com.mu.aop_xml.cglib.service;
public class StudentService {
public void addStudent(){
System.out.println("新增学生");
}
public void updateStudent(){
System.out.println("更新学生");
}
public void deleteStudent(){
System.out.println("删除学生");
}
public int queryStudent(int id){
System.out.println("根据id查找学生");
return id;
}
}
工厂
package com.mu.aop_xml.cglib.service;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* cglig 动态代理 没有接口的类 或者有接口的实现类 代理类为实现类的子类
*/
public class MyBeanFactory {
public static StudentService createStudentService() {
//目标类
StudentService studentService = new StudentService();
//切面类
MyAspect myAspect = new MyAspect();
//把切面类中的方法应道目标类中 切面 cglib
//创建增强对象
Enhancer enhancer = new Enhancer();
//设置父类
enhancer.setSuperclass(studentService.getClass());
//设置回调【拦截】
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
/*
* proxy 代理对象 为studentService的子类
* */
myAspect.before();
// Object object = method.invoke(studentService, args); 等价于下面的那个方法
Object object = methodProxy.invokeSuper(proxy, args);
System.out.println("拦截的对象为" + object);
myAspect.after();
System.out.println("华丽的分割线-----------------------------");
return object;
}
});
StudentService studentService1 = (StudentService) enhancer.create();
return studentService1;
}
}
测试
package com.mu.aop_xml.cglib.test;
import com.mu.aop_xml.cglib.service.MyBeanFactory;
import com.mu.aop_xml.cglib.service.StudentService;
public class Test1 {
public static void main(String[] args) {
StudentService studentService = MyBeanFactory.createStudentService();
studentService.addStudent();
studentService.deleteStudent();
studentService.updateStudent();
studentService.queryStudent(88);
}
}