forked from DerekYRC/mini-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
270 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.springframework.aop; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/6 | ||
*/ | ||
public class AdvisedSupport { | ||
|
||
private TargetSource targetSource; | ||
|
||
private MethodInterceptor methodInterceptor; | ||
|
||
private MethodMatcher methodMatcher; | ||
|
||
public TargetSource getTargetSource() { | ||
return targetSource; | ||
} | ||
|
||
public void setTargetSource(TargetSource targetSource) { | ||
this.targetSource = targetSource; | ||
} | ||
|
||
public MethodInterceptor getMethodInterceptor() { | ||
return methodInterceptor; | ||
} | ||
|
||
public void setMethodInterceptor(MethodInterceptor methodInterceptor) { | ||
this.methodInterceptor = methodInterceptor; | ||
} | ||
|
||
public MethodMatcher getMethodMatcher() { | ||
return methodMatcher; | ||
} | ||
|
||
public void setMethodMatcher(MethodMatcher methodMatcher) { | ||
this.methodMatcher = methodMatcher; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.springframework.aop; | ||
|
||
/** | ||
* 被代理的目标对象 | ||
* | ||
* @author derekyi | ||
* @date 2020/12/6 | ||
*/ | ||
public class TargetSource { | ||
|
||
private final Object target; | ||
|
||
public TargetSource(Object target) { | ||
this.target = target; | ||
} | ||
|
||
public Class<?>[] getTargetClass() { | ||
return this.target.getClass().getInterfaces(); | ||
} | ||
|
||
public Object getTarget() { | ||
return this.target; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/springframework/aop/framework/AopProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.springframework.aop.framework; | ||
|
||
/** | ||
* AOP代理抽象 | ||
* | ||
* @author derekyi | ||
* @date 2020/12/5 | ||
*/ | ||
public interface AopProxy { | ||
|
||
Object getProxy(); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.springframework.aop.framework; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.springframework.aop.AdvisedSupport; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
|
||
/** | ||
* JDK动态代理 | ||
* | ||
* @author derekyi | ||
* @date 2020/12/5 | ||
*/ | ||
public class JdkDynamicAopProxy implements AopProxy, InvocationHandler { | ||
|
||
private final AdvisedSupport advised; | ||
|
||
public JdkDynamicAopProxy(AdvisedSupport advised) { | ||
this.advised = advised; | ||
} | ||
|
||
/** | ||
* 返回代理对象 | ||
* | ||
* @return | ||
*/ | ||
@Override | ||
public Object getProxy() { | ||
return Proxy.newProxyInstance(getClass().getClassLoader(), advised.getTargetSource().getTargetClass(), this); | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
if (advised.getMethodMatcher().matches(method, advised.getTargetSource().getTarget().getClass())) { | ||
//代理方法 | ||
MethodInterceptor methodInterceptor = advised.getMethodInterceptor(); | ||
return methodInterceptor.invoke(new ReflectiveMethodInvocation(advised.getTargetSource().getTarget(), method, args)); | ||
} | ||
return method.invoke(advised.getTargetSource().getTarget(), args); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/org/springframework/aop/framework/ReflectiveMethodInvocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.springframework.aop.framework; | ||
|
||
import org.aopalliance.intercept.MethodInvocation; | ||
|
||
import java.lang.reflect.AccessibleObject; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/6 | ||
*/ | ||
public class ReflectiveMethodInvocation implements MethodInvocation { | ||
|
||
private final Object target; | ||
|
||
private final Method method; | ||
|
||
private final Object[] arguments; | ||
|
||
public ReflectiveMethodInvocation(Object target, Method method, Object[] arguments) { | ||
this.target = target; | ||
this.method = method; | ||
this.arguments = arguments; | ||
} | ||
|
||
@Override | ||
public Object proceed() throws Throwable { | ||
return method.invoke(target, arguments); | ||
} | ||
|
||
@Override | ||
public Method getMethod() { | ||
return method; | ||
} | ||
|
||
@Override | ||
public Object[] getArguments() { | ||
return arguments; | ||
} | ||
|
||
@Override | ||
public Object getThis() { | ||
return target; | ||
} | ||
|
||
@Override | ||
public AccessibleObject getStaticPart() { | ||
return method; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/org/springframework/test/aop/DynamicProxyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.springframework.test.aop; | ||
|
||
import org.junit.Test; | ||
import org.springframework.aop.AdvisedSupport; | ||
import org.springframework.aop.MethodMatcher; | ||
import org.springframework.aop.TargetSource; | ||
import org.springframework.aop.aspectj.AspectJExpressionPointcut; | ||
import org.springframework.aop.framework.JdkDynamicAopProxy; | ||
import org.springframework.test.common.WorldServiceInterceptor; | ||
import org.springframework.test.service.WorldService; | ||
import org.springframework.test.service.WorldServiceImpl; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/6 | ||
*/ | ||
public class DynamicProxyTest { | ||
|
||
@Test | ||
public void testJdkDynamicProxy() throws Exception { | ||
WorldService worldService = new WorldServiceImpl(); | ||
|
||
AdvisedSupport advisedSupport = new AdvisedSupport(); | ||
TargetSource targetSource = new TargetSource(worldService); | ||
WorldServiceInterceptor methodInterceptor = new WorldServiceInterceptor(); | ||
MethodMatcher methodMatcher = new AspectJExpressionPointcut("execution(* org.springframework.test.service.WorldService.explode(..))").getMethodMatcher(); | ||
advisedSupport.setTargetSource(targetSource); | ||
advisedSupport.setMethodInterceptor(methodInterceptor); | ||
advisedSupport.setMethodMatcher(methodMatcher); | ||
|
||
WorldService proxy = (WorldService) new JdkDynamicAopProxy(advisedSupport).getProxy(); | ||
proxy.explode(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/java/org/springframework/test/common/WorldServiceInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.springframework.test.common; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/6 | ||
*/ | ||
public class WorldServiceInterceptor implements MethodInterceptor { | ||
@Override | ||
public Object invoke(MethodInvocation invocation) throws Throwable { | ||
System.out.println("Do something before the earth explodes"); | ||
Object result = invocation.proceed(); | ||
System.out.println("Do something after the earth explodes"); | ||
return result; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/org/springframework/test/service/WorldService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.springframework.test.service; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/6 | ||
*/ | ||
public interface WorldService { | ||
|
||
void explode(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/org/springframework/test/service/WorldServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.springframework.test.service; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/6 | ||
*/ | ||
public class WorldServiceImpl implements WorldService { | ||
|
||
@Override | ||
public void explode() { | ||
System.out.println("The Earth is going to explode"); | ||
} | ||
} |