Java annotations

I used a lot of annotations in java, but I never wrote. I read several guides and I'm really confused.

They use annotations, such as meta-information, such as names, age, etc. This is really confusing because I want to do something else

http://www.developer.com/java/other/article.php/3556176/An-Introduction-to-Java-Annotations.htm

I want to control function calls.

eg

@Permission(user) public static void account(){ ... } 

Thus, my functions are called only if the user has permission, otherwise the user must be redirected to the login page.

I could not find any information, maybe I'm using the wrong keyword?

I hope you can find out

Thanks.

+4
source share
4 answers

You can do this, but with lots of extra code. The interception method call is part of AOP (aspect-oriented programming). You need to make proxy objects from your targets, and process the annotation in the call handler.

Fortunately, you do not need to do this - since you have a webapp, just use spring / spring -mvc / spring -security. Spring provides you with an AOP framework that you can use to determine the processing aspects of your permission logic.

+3
source

Not sure how you can do it yourself, but if you use Spring, they have something that can help.

http://static.springsource.org/spring-security/site/docs/3.0.7.RELEASE/reference/el-access.html

I use it in my current project and it works well

+2
source

Something similar should really be done in the function itself (or in some other part of the program). Please note that annotations contain data about a program that is not part of the program itself (see this link).

+1
source

I think that after this you are an AOP advisor that runs before your method. See here: http://java-questions.com/spring_aop.html

As an alternative to Spring, you can use AspectJ: http://www.andrewewhite.net/wordpress/2010/03/17/aspectj-annotation-tutorial/

+1
source

All Articles