Java – use annotations for tracking

I've been using a company code base with a strategy to write a large number of tracking records Therefore, each method has a piece of code that starts as follows:

String LOG_METHOD = "nameOfMethod(String,List<Long>):void";
if(logger.isTraceEnabled()) { 
    Object[] params = new Object[] { string,list };
    logger.trace(CompanyMessages.newMethodInstanceMessage(this,LOG_METHOD,params)); 
}

And end with this (in the finally clause or only at the end of the method:

if(logger.isTraceEnabled()) { 
    logger.trace(CompanyMessages.leaveMethodInstanceMessage(this,LOG_METHOD)); 
}

There's actually more code, but that's the basic idea This is chaotic code. Other encoders are constantly confused with their own interpretation. They do not use a specific companymessages class. This is the information that needs to be formatted and read by the monitoring tool So I'm looking for a way to get rid of all the above codes. I just need to provide all the methods that need to track records, such as @ logbefore ('loglevel ') & @ logafter ('record level')

I chose this solution because other developers don't have to learn anything new, but use comments instead of code I work in a server environment where we deploy hundreds of web applications and dozens of developers So I've been looking for a way to do this in web applications without a lot of extra coding or extra large libraries This means that I am looking for a small, stable AOP implementation that is easy to configure in every web application using annotations similar to those I have proposed Performance is also important What is the simplest example of implementing this with AOP?

Editor: I found something very similar I'm looking for, but there are some problems All classes that require logging must be configured, which will be more resource intensive than just using annotations Can the spring configuration be fixed using < AOP: AspectJ AutoProxy / >

Solution

It looks like object - oriented programming (AOP) can really help you It is a good solution to cross problems such as recording and tracking. It supports annotations as you need

Look at AspectJ or spring AOP

It will involve learning some AOP principles and APIs of your choice, but it's definitely worth the effort In particular, recording and tracking is the first AOP tutorial you will encounter. It is easy to do without going further

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>