我们正在使用 aorg.springframework.beans.factory.BeanFactoryAware
来运行一系列命令。系统的所有服务都使用一项服务来进行审计日志记录。此审计日志服务需要一个唯一标识符,该标识符在我们的BeanFactoryAware
.
现在,这个唯一标识符正在每个函数调用中传递。一种选择是使用某种类型的上下文对象来传递。最好不要这样做,因为这会使系统中不了解系统的其他部分无法使用这些服务BeanFactoryAware
。
是否有一些 AOP 方法可以实现更清洁的解决方案?一种审计日志可以获取唯一标识符,而BeanFactoryAware
审计代码和审计代码之间的所有代码都不知道该值?
根据请求,这里有一些代码:
public class ChainRunner implements BeanFactoryAware
{
public int runChain(String chainName, Object initParameters, int msgLinkId) throws Exception {
OurContext ctx = createContext(initParameters);
ctx.setMsgLinkId(msgLinkId);
createChain(chainName).execute(ctx);
return retval;
}
}
public class AServiceImpl implements AService
{
private LoggingService logger;
public LoggingService getLogger() {
return logger;
}
public void setLogger(LoggingService logger) {
this.logger = logger;
}
public void aMethod(object params, int msgLinkId)
{
// lots of code snipped
logger().saveActionA( msgLinkId, otherParams);
}
}
public interface LoggingService
{
public void saveActionA(int msgLinkId, Object otherParams);
}
ChainRunner 将 msgLinkId 添加到链的上下文中。每条消息都会将 msgLinkId 从上下文中拉出,并将其作为整数参数传递给服务,例如 AServer.aMethod。最后,服务需要在 LoggerService 上记录活动,向其传递有关正在记录的内容的参数,其中第一个参数是 msgLinkId。
就目前而言,所有服务都通过 XML 配置进行连接:
<bean id="loggerService" class="com.nowhere.LoggingServiceImpl"/>
<bean id="aService" class="com.nowhere.AServiceImpl">
<property name="logger" ref="loggerService" />
</bean>
那么问题来了:是否可以通过AOP让msgLinkId下跳到LoggingService,而不必经过很多层的服务呢?