[英]Use Java Annotation not to run a method
I've got a method in my class only for testing purpose : 我的课程中只有一个方法用于测试目的:
private void printOut(String msg, Object value)
{
System.out.println(msg + value);
}
It is a wrapper method for System.out.println(); 它是System.out.println()的包装方法;
So I hope, with the use of Annotation, I can choose not to run this method during productive environment while still keep those diagnostic output ready if I am to switch back to debugging environment. 所以我希望,通过使用Annotation,我可以选择不在生产环境中运行此方法,同时如果我要切换回调试环境,仍然保持这些诊断输出准备就绪。
Which Annotation shall I put on top of the method name? 我应该在方法名称之上放置哪个注释?
As I stated above you should use logging. 如上所述,您应该使用日志记录。 Here are some of the benefits of using logging in an application: 以下是在应用程序中使用日志记录的一些好处:
Read more about logging here 阅读更多关于登录的信息
There are a lot of logging frameworks in java: java中有很多日志框架:
And several facades, which provides abstraction for various logging frameworks: 还有几个外观,它们为各种日志框架提供了抽象:
One solution is to use AspectJ to do this, as you can control the behavior based on annotations. 一种解决方案是使用AspectJ来执行此操作,因为您可以基于注释控制行为。
Here is a tutorial on how to use annotations as join points: 这是一个关于如何使用注释作为连接点的教程:
http://www.eclipse.org/aspectj//doc/next/adk15notebook/annotations-pointcuts-and-advice.html http://www.eclipse.org/aspectj//doc/next/adk15notebook/annotations-pointcuts-and-advice.html
What you could do is to have: 你能做的是:
@DebugMessage
private void printOut(String msg, Object value)
{ }
Then, in your aspect you could have the aspect do the println
call. 然后,在您的方面,您可以让方面执行println
调用。
This way, in production, the aspect isn't included, but, should you ever need to have this active, then just add the aspect, even to production code, to get some debugging. 这样,在生产中,方面不包括在内,但是,如果您需要将此方法设置为活动,那么只需添加方面,甚至是生产代码,以获得一些调试。
You should really follow uthark's advice and use a logging framework. 您应该遵循uthark的建议并使用日志框架。
Those have been specifically designed for this situation. 那些是专为这种情况而设计的。
Doing something "funky" will probably cause problems later on. 做一些“时髦”的事情可能会在以后引起问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.