This question already has an answer here:
这个问题在这里已有答案:
- Java method missing (ala Ruby) for decorating? 4 answers
缺少Java方法(ala Ruby)进行装饰? 4个答案
I spend most of my time programming in Python, so forgive me if my approach to this problem is short-sited:
我把大部分时间花在Python上,所以如果我对这个问题的处理方法是短暂的,请原谅我:
I want to have certain methods of a class require login credentials. Simply, each method should check whether the class variable user
is set, and if so, continue, but if not, spit out a "you need to login" message.
我希望某个类的某些方法需要登录凭据。简单地说,每个方法都应该检查是否设置了类变量user,如果是,则继续,但如果没有,则吐出“你需要登录”的消息。
In Python, I would just write a decorator to do this. How can I accomplish the same thing in java with as little redundant code as possible?
在Python中,我只想写一个装饰器来做这件事。如何使用尽可能少的冗余代码在java中完成同样的事情?
Thanks!
4 个解决方案
#1
12
One way to solve this in Java is to use an Aspect-oriented programming tool. One such tool is AspectJ. You will probably find that this type of problem is an example that is commonly used to motivate AOP.
在Java中解决这个问题的一种方法是使用面向方面的编程工具。一个这样的工具是AspectJ。您可能会发现此类问题是一个常用于激励AOP的示例。
AOP might be a pretty heavyweight way to solve this particular problem, so you may want to explore just adding appropriate checks to each method. There isn't any direct equivalent to Python's decorators in native Java.
AOP可能是解决此特定问题的一种非常重要的方法,因此您可能希望探索只为每个方法添加适当的检查。在本机Java中没有任何直接等同于Python的装饰器。
#2
8
The simplest thing to do is to write a method like "assertCredentials" and call that method at the start of every method that needs credentials. If credentials are not set, the method should throw an exception, which will abort the parent method.
最简单的方法是编写一个类似“assertCredentials”的方法,并在需要凭据的每个方法的开头调用该方法。如果未设置凭据,则该方法应抛出异常,这将中止父方法。
Java has annotations that can be used to decorate methods, etc., but I don't think using annotations in this case would simplify things.
Java有可用于装饰方法等的注释,但我认为在这种情况下使用注释不会简化事情。
#3
7
You can try Google Guice AOP which is relatively more lightweight than AOP framework like AspectJ.
你可以试试Google Guice AOP,它比像AspectJ这样的AOP框架相对更轻量级。
#1
12
One way to solve this in Java is to use an Aspect-oriented programming tool. One such tool is AspectJ. You will probably find that this type of problem is an example that is commonly used to motivate AOP.
在Java中解决这个问题的一种方法是使用面向方面的编程工具。一个这样的工具是AspectJ。您可能会发现此类问题是一个常用于激励AOP的示例。
AOP might be a pretty heavyweight way to solve this particular problem, so you may want to explore just adding appropriate checks to each method. There isn't any direct equivalent to Python's decorators in native Java.
AOP可能是解决此特定问题的一种非常重要的方法,因此您可能希望探索只为每个方法添加适当的检查。在本机Java中没有任何直接等同于Python的装饰器。
#2
8
The simplest thing to do is to write a method like "assertCredentials" and call that method at the start of every method that needs credentials. If credentials are not set, the method should throw an exception, which will abort the parent method.
最简单的方法是编写一个类似“assertCredentials”的方法,并在需要凭据的每个方法的开头调用该方法。如果未设置凭据,则该方法应抛出异常,这将中止父方法。
Java has annotations that can be used to decorate methods, etc., but I don't think using annotations in this case would simplify things.
Java有可用于装饰方法等的注释,但我认为在这种情况下使用注释不会简化事情。
#3
7
You can try Google Guice AOP which is relatively more lightweight than AOP framework like AspectJ.
你可以试试Google Guice AOP,它比像AspectJ这样的AOP框架相对更轻量级。