0

We are using a org.springframework.beans.factory.BeanFactoryAware to run a chain of commands. All the services of the system use one service to do the audit logging. This audit logging service needs an unique identifier that is set in our implementation of the BeanFactoryAware.

Right now this unique identifier is being passed down in each and every function call. One option is to have some type of context object that is pass around. It is preferiable NOT to do that because that would make the services less useable by other parts of the system that doesn't know about the BeanFactoryAware system.

Is there some AOP way of implementing a cleaner solution? One where the audit logging can get the unique identifier without all the code between the BeanFactoryAware and the audit code knowing the value?


Per the request, here is some code:

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 adds the msgLinkId to the chain's context. Each message will pull the the msgLinkId out of the context and pass it as an integer parameter down into a service, such as AServer.aMethod. Finally the service needs to log activity on the LoggerService, passing it parameters about what is being logged, where the first parameter is msgLinkId.

As it stands now, all the services are wired up via XML configuration:

<bean id="loggerService" class="com.nowhere.LoggingServiceImpl"/>   

<bean id="aService" class="com.nowhere.AServiceImpl">
    <property name="logger"  ref="loggerService" />
</bean> 

So the question is: Is it possible to use AOP to get msgLinkId to jump down to LoggingService without having to pass it through many layers of services?

Sam Carleton
  • 1,339
  • 7
  • 23
  • 45
  • Could your logging service have a method that takes a Log object and encapsulate the id in that? – Romski Jun 23 '14 at 23:21
  • How about some source code which better communicates your situation and requirements? I would like to see how the objects you mention play together and where the unique ID is stored, because the object knowing the ID is the one you need to retrieve it from. It sounds like easy to do in an aspect, but please give us something to start with. – kriegaex Jun 24 '14 at 14:33

0 Answers0