Java代理模式及spring aop实现原理
发布日期:2021-05-15 00:00:44 浏览次数:18 分类:精选文章

本文共 9180 字,大约阅读时间需要 30 分钟。

Spring AOP������������

1. ���������Spring AOP���

traditionally, ������������������������������������HTTP������������Servlet���Service���DAO������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������AOP��������������������������������������������������������������������������������������� ���������������������������������������������������������������������������

2. Spring AOP���������������

Spring AOP������������������������������������������������������������������������������������������������������

  • ������������������������������������������
  • ���������������������������������������������
  • ������������������������������������������
  • ���������������������������������������

3. Spring AOP���������������

���Spring AOP������������������������������������������������������������������

  • @Aspect������������������������
  • @Pointcut������������������������������������������������������������������������
  • @JointPoint���������������������������������������������������������������������������������������
  • @Advice������������������������������������������
    • @Before������������������������������������������
    • @After������������������������������������������
    • @Around���������������������������������������������
    • @AfterReturning������������������������������������������������

4. JoinPoint������������API������

JoinPoint���Spring AOP���������������������������������������������������������������������������API���������

@Before("declareJoinPointerExpression()")
public void beforeMethod(JoinPoint joinPoint) {
System.out.println("���������������������" + joinPoint.getSignature().getName());
System.out.println("���������������������������������������" + joinPoint.getSignature().getDeclaringType().getSimpleName());
System.out.println("���������������������������������" + joinPoint.getSignature().getDeclaringTypeName());
System.out.println("���������������������������" + Modifier.toString(joinPoint.getSignature().getModifiers()));
Object[] args = joinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
System.out.println("���" + (i + 1) + "���������������" + args[i]);
}
System.out.println("���������������������" + joinPoint.getTarget());
System.out.println("���������������������" + joinPoint.getThis());
}
  • @Before������������������������������������������������
  • @Around������������������������������������������������������������������������������

������@Around���������������������������������������������������������������������

@Around("declareJoinPointerExpression()")
public Object aroundMethod(ProceedingJoinPoint pjd) {
Object result = null;
try {
System.out.println("���������������������...");
result = pjd.proceed();
System.out.println("���������������������������...");
} catch (Throwable e) {
System.out.println("���������������������������...");
throw new RuntimeException(e);
}
return result;
}

���������������������@Before���@After���������������������������@Around������������������������������������������������

5. ������������������������������

���Spring AOP������������������JoinPoint������������������������������������

private SysUserOperLog getAnnotationLog(JoinPoint joinPoint) throws Exception {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
return method.getAnnotation(SysUserOperLog.class);
}
return null;
}

������������������������������������������������������������������������������������������������������

6. ������������������

���������������������������������������������������������������������������������������������������������������

  • ���������������������������������������.extension������������������������������
  • ���������������������������������������������������
  • ���������������������������������������

7. ������������

���������������������������������������������������������������

public interface AgentInterface {
void order(String foodName);
}
public class Customer implements AgentInterface {
@Override
public void order(String foodName) {
System.out.println("������������" + foodName);
}
}
public class DeliverAgent implements AgentInterface {
private Customer customer;
DeliverAgent(Customer customer) {
this.customer = customer;
}
@Override
public void order(String foodName) {
customer.order(foodName);
System.out.println("������������������");
}
}
public class Test {
public static void main(String[] args) {
Customer customer = new Customer();
DeliverAgent deliverAgent = new DeliverAgent(customer);
String foodName = "���������";
customer.order(foodName);
System.out.println("*********************");
deliverAgent.order(foodName);
}
}

8. ������������

������������������������������������������������������������������������������������������������

8.1 JDK������������

������Proxy.newProxyInstance���������������������

public interface AgentInterface {
String order(String foodName);
void test();
void test2();
}
public class Customer implements AgentInterface {
@Override
public String order(String foodName) {
return foodName;
}
@Override
public void test() {}
@Override
public void test2() {}
}
public class Test {
public static void main(String[] args) {
Customer customer = new Customer();
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("order".equals(method.getName())) {
System.out.println("������������������������������������");
String result = (String) method.invoke(customer, args);
System.out.println("������������������������������������");
return result + "-������������";
} else {
return method.invoke(customer, args);
}
}
};
AgentInterface proxyInstance = (AgentInterface) Proxy.newProxyInstance(customer.getClass().getClassLoader(), customer.getClass().getInterfaces(), handler);
String order = proxyInstance.order("���������");
System.out.println(order);
}
}
8.2 CGLIB������������

������������������������������������������

public class AgentCustomer extends Customer {
private MethodInterceptor methodInterceptor;
public AgentCustomer(MethodInterceptor methodInterceptor) {
this.methodInterceptor = methodInterceptor;
}
@Override
public String order(String foodName) {
try {
Method method = Customer.class.getMethod("order", String.class);
Object result = methodInterceptor.intercept(this, method, new Object[]{foodName}, null);
return (String) result;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return super.order(foodName);
}
@Override
public void test() {
try {
Method method = Customer.class.getMethod("order", String.class);
methodInterceptor.intercept(this, method, new Object[]{}, null);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
super.test();
}
}

9. ���������������������������

������������Proxy.newProxyInstance���������������

public class AgentCustomer implements AgentInterface {
private InvocationHandler invocationHandler;
AgentCustomer(InvocationHandler invocationHandler) {
this.invocationHandler = invocationHandler;
}
@Override
public String order(String foodName) {
try {
Method method = AgentInterface.class.getMethod("order", String.class);
String result = (String) invocationHandler.invoke(this, method, new Object[]{foodName});
return result;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
@Override
public void test() {
try {
Method method = AgentInterface.class.getMethod("test", String.class);
invocationHandler.invoke(this, method, null);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@Override
public void test2() {
try {
Method method = AgentInterface.class.getMethod("test2", String.class);
invocationHandler.invoke(this, method, null);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}

10. ������

Spring AOP���������������������������������������������������������������������������������������������������������������������������������������������������������������������JDK���������������CGLIB������������������������������������������������������������������������������

上一篇:Java自定义注解结合反射获取注解中字段
下一篇:Java位运算,负数的二进制表示形式,int类型最大值为什么是2的31次方-1

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年05月03日 22时14分40秒