Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
希川
container-mini
提交
6b1bd85b
C
container-mini
项目概览
希川
/
container-mini
通知
6
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
container-mini
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6b1bd85b
编写于
11月 06, 2021
作者:
感
感冒灵
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
<feat>: 单元测试
上级
e9dd90a7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
108 addition
and
0 deletion
+108
-0
src/main/java/cn/noexception/container/aop/aspectj/AspectJExpressionPointcutAdvisor.java
...ntainer/aop/aspectj/AspectJExpressionPointcutAdvisor.java
+4
-0
src/test/java/cn/noexception/test/AopTest.java
src/test/java/cn/noexception/test/AopTest.java
+104
-0
未找到文件。
src/main/java/cn/noexception/container/aop/aspectj/AspectJExpressionPointcutAdvisor.java
浏览文件 @
6b1bd85b
...
...
@@ -42,4 +42,8 @@ public class AspectJExpressionPointcutAdvisor implements PointcutAdvisor {
}
return
pointcut
;
}
public
void
setExpression
(
String
expression
)
{
this
.
expression
=
expression
;
}
}
src/test/java/cn/noexception/test/AopTest.java
0 → 100644
浏览文件 @
6b1bd85b
package
cn.noexception.test
;
import
cn.noexception.container.aop.AdvisedSupport
;
import
cn.noexception.container.aop.ClassFilter
;
import
cn.noexception.container.aop.MethodMatcher
;
import
cn.noexception.container.aop.TargetSource
;
import
cn.noexception.container.aop.aspectj.AspectJExpressionPointcut
;
import
cn.noexception.container.aop.aspectj.AspectJExpressionPointcutAdvisor
;
import
cn.noexception.container.aop.framework.ProxyFactory
;
import
cn.noexception.container.aop.framework.ReflectiveMethodInvocation
;
import
cn.noexception.container.aop.framework.adapter.MethodBeforeAdviceInterceptor
;
import
cn.noexception.test.bean.IUserService
;
import
cn.noexception.test.bean.UserServiceBeforeAdvice
;
import
cn.noexception.test.bean.UserServiceInterceptor
;
import
cn.noexception.test.bean.impl.UserService
;
import
org.aopalliance.intercept.MethodInterceptor
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.lang.reflect.InvocationHandler
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Proxy
;
public
class
AopTest
{
private
AdvisedSupport
advisedSupport
;
@Before
public
void
init
()
{
// 目标对象
IUserService
userService
=
new
UserService
();
// 组装代理信息
advisedSupport
=
new
AdvisedSupport
();
advisedSupport
.
setTargetSource
(
new
TargetSource
(
userService
));
advisedSupport
.
setMethodInterceptor
(
new
UserServiceInterceptor
());
advisedSupport
.
setMethodMatcher
(
new
AspectJExpressionPointcut
(
"execution(* cn.noexception.test.bean.IUserService.*(..))"
));
}
@Test
public
void
test_proxyFactory
()
{
advisedSupport
.
setProxyTargetClass
(
false
);
IUserService
proxy
=
(
IUserService
)
new
ProxyFactory
(
advisedSupport
).
getProxy
();
System
.
out
.
println
(
"测试结果:"
+
proxy
.
queryUserInfo
());
}
@Test
public
void
test_advisor
()
{
// 目标对象
IUserService
userService
=
new
UserService
();
AspectJExpressionPointcutAdvisor
advisor
=
new
AspectJExpressionPointcutAdvisor
();
advisor
.
setExpression
(
"execution(* cn.noexception.test.bean.IUserService.*(..))"
);
advisor
.
setAdvice
(
new
MethodBeforeAdviceInterceptor
(
new
UserServiceBeforeAdvice
()));
ClassFilter
classFilter
=
advisor
.
getPointcut
().
getClassFilter
();
if
(
classFilter
.
matches
(
userService
.
getClass
()))
{
AdvisedSupport
advisedSupport
=
new
AdvisedSupport
();
TargetSource
targetSource
=
new
TargetSource
(
userService
);
advisedSupport
.
setTargetSource
(
targetSource
);
advisedSupport
.
setMethodInterceptor
((
MethodInterceptor
)
advisor
.
getAdvice
());
advisedSupport
.
setMethodMatcher
(
advisor
.
getPointcut
().
getMethodMatcher
());
advisedSupport
.
setProxyTargetClass
(
true
);
IUserService
proxy
=
(
IUserService
)
new
ProxyFactory
(
advisedSupport
).
getProxy
();
System
.
out
.
println
(
"测试结果:"
+
proxy
.
queryUserInfo
());
}
}
@Test
public
void
test_proxy_method
()
{
// 目标对象(可替换成任何目标对象)
Object
targetObj
=
new
UserService
();
// AOP 代理
IUserService
proxy
=
(
IUserService
)
Proxy
.
newProxyInstance
(
Thread
.
currentThread
().
getContextClassLoader
(),
targetObj
.
getClass
().
getInterfaces
(),
new
InvocationHandler
()
{
// 方法匹配器
MethodMatcher
methodMatcher
=
new
AspectJExpressionPointcut
(
"execution(* cn.noexception.test.bean.IUserService.*(..))"
);
@Override
public
Object
invoke
(
Object
proxy
,
Method
method
,
Object
[]
args
)
throws
Throwable
{
if
(
methodMatcher
.
matches
(
method
,
targetObj
.
getClass
()))
{
// 方法拦截器
MethodInterceptor
methodInterceptor
=
invocation
->
{
long
start
=
System
.
currentTimeMillis
();
try
{
return
invocation
.
proceed
();
}
finally
{
System
.
out
.
println
(
"监控 - Begin By AOP"
);
System
.
out
.
println
(
"方法名称:"
+
invocation
.
getMethod
().
getName
());
System
.
out
.
println
(
"方法耗时:"
+
(
System
.
currentTimeMillis
()
-
start
)
+
"ms"
);
System
.
out
.
println
(
"监控 - End\r\n"
);
}
};
// 反射调用
return
methodInterceptor
.
invoke
(
new
ReflectiveMethodInvocation
(
targetObj
,
method
,
args
));
}
return
method
.
invoke
(
targetObj
,
args
);
}
});
String
result
=
proxy
.
queryUserInfo
();
System
.
out
.
println
(
"测试结果:"
+
result
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录