我已经构建了一个方面,它使用我自己的库中的一个类来工作。为了有用,该类需要可序列化,但是当让 PostSharp 做它的事情时,我收到了这个错误:
PostSharp 3.0 [3.0.35.0, 64 bit, CLR 4.5, Release] - Copyright (c) SharpCrafters
s.r.o., 2004-2012.
POSTSHARP : error LA0001: Cannot serialize the aspects: Cannot find a serializer
for type 'NextGen.Framework.Managers.LogMgr.NxLogMgr'..
POSTSHARP : message PS0122: Details of the previous error or warning:\nPostSharp
.Serialization.PortableSerializationException: Cannot find a serializer for type
'NextGen.Framework.Managers.LogMgr.NxLogMgr'.
POSTSHARP : message PS0122: at PostSharp.Serialization.SerializationWriter.Ge
tObjectInfo(Object obj)
POSTSHARP : message PS0122: at PostSharp.Serialization.SerializationWriter.Wr
iteObjectReference(Object value, Boolean writeInitializationDataInline)
POSTSHARP : message PS0122: at PostSharp.Serialization.SerializationWriter.Wr
iteValue(Object value, SerializationIntrinsicType intrinsicType, Boolean …Run Code Online (Sandbox Code Playgroud) 如何在 Spring AOP 中使用 before 建议作为安全措施。
例如,我有这个之前的建议(带有伪代码):
@Before("trigger()")
public void beforeMethod(JoinPoint point){
Method[] a = point.getSignature().getClass().getDeclaredMethods();
for(int i=0; i < a.length; i++){
//if method has such arguments then
if(a[i] 'has args String name, String role, Int Money'){
//And if these arguments meets such requirements
if(a[i].argument(Int.class) > 1000 or a[i].argument(String role).equals("normal"))
//Stop executing this method
a[i].stop
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这只是伪代码,所以它看起来可能不正确,但我希望您能有所了解。如果满足或不满足某些要求,是否有一些 JoinPoint 方法可以停止扫描方法?
我正在尝试集中管理某个方法的异常处理,但我似乎无法到达那里。
public class ExceptionInterceptor : IInterceptor
{
private readonly Logger _logger;
public ExceptionInterceptor(Logger logger)
{
_logger = logger;
Measure.Configure(new StatsdConfig());
}
public void Intercept(IInvocation invocation)
{
try
{
invocation.Proceed();
//if ((Task<System.Threading.Tasks.VoidTaskReturn>) invocation.ReturnValue.Status == "Failed")
//{
// throw new Exception(invocation.ReturnValue.Exception[0]);
//}
}
catch (Exception e)
{
var errorMessage =
String.Format(
"An error occurred while retrieving fund data. Error Message: {0} Inner Exception: {1}",
e.Message, e.InnerException != null ? e.InnerException.Message : "<None>");
_logger.Log(errorMessage);
Measure.Counter("Exception", 1);
Measure.Event("Exception", errorMessage);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
我将它连接到一个模块中,如下所示:
builder.RegisterType<DataConsumer>(). …Run Code Online (Sandbox Code Playgroud) 我有任务以某种方式监视 logback 库中的方法调用。我开始用 spring aop 做这件事。因此,例如,我应该在 ch.qos.logback.classic.Logger 类中捕获所有方法的执行。这是我的方面:
@Aspect
@Configuration
public class LogbackAspect {
@Before(value = "execution(* ch.qos.logback.classic.Logger.*(..))")
public void getInfo(JoinPoint joinPoint) {
System.out.println("+++++AOP " + joinPoint.getSignature().getName());
}
}
Run Code Online (Sandbox Code Playgroud)
但是暂时不行,那么,有没有可能用spring aop拦截方法执行呢?或者有一些更好的方法来做到这一点?
\xe2\x80\xa6 我们如何使用面向方面的编程来克服它们?
\n