简体   繁体   English

使用Java Annotation不运行方法

[英]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: 以下是在应用程序中使用日志记录的一些好处:

  • Logging can generate detailed information about the operation of an application. 日志记录可以生成有关应用程序操作的详细信息。
  • Once added to an application, logging requires no human intervention. 一旦添加到应用程序,日志记录不需要人为干预。
  • Application logs can be saved and studied at a later time. 应用程序日志可以在以后保存和研究。
  • If sufficiently detailed and properly formatted, application logs can provide audit trails. 如果足够详细且格式正确,应用程序日志可以提供审计跟踪。
  • By capturing errors that may not be reported to users, logging can help support staff with troubleshooting. 通过捕获可能未向用户报告的错误,日志记录可以帮助支持人员进行故障排除。
  • By capturing very detailed and programmer-specified messages, logging can help programmers with debugging. 通过捕获非常详细和程序员指定的消息,日志记录可以帮助程序员进行调试。
  • Logging can be a debugging tool where debuggers are not available, which is often the case with multi-threaded or distributed applications. 日志记录可以是调试器不可用的调试工具,多线程或分布式应用程序通常就是这种情况。
  • Logging stays with the application and can be used anytime the application is run. 日志记录保留在应用程序中,可以在应用程序运行时随时使用。

Read more about logging here 阅读更多关于登录的信息

There are a lot of logging frameworks in java: java中有很多日志框架:

  1. Log4j Log4j的
  2. java.util.logging java.util.logging中
  3. Logback . Logback

And several facades, which provides abstraction for various logging frameworks: 还有几个外观,它们为各种日志框架提供了抽象:

  1. slf4j SLF4J
  2. commons-logging 共享记录

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM