blank_popup
blank_popup

Reputation: 271

How can make annotation in Spring?

I have seen annotation in Spring framework. And I wonder how can I make annotation.

We have used many annotation(eg. @Controller, @Autowired, @RequestMapping and so on). Using such annotation, We do NOT code with reflection. But I cannot find tutorial for making annotation without reflection. In all tutorials, writers demonstrate annotation with reflection.

If I don't use reflection, CANNOT I make annotation?

Upvotes: 1

Views: 153

Answers (2)

Grim
Grim

Reputation: 1974

You can write Annotations from JDK5 and better. But only reflection (xand the static-scope) can read Annotations!

Annotations was a alternate to xdoclet-tags, they was part of Javadoc so your question is like "can i write documentation" - well yes.

There is a joke about Memory that only can be written but not be read, the WOM(write-only-memory).

If you do not like to use Reflection, a 3rd party library can read Annotations via reflection for you.

What you can do is to Annotate your Annotations.

Example:

@Autowired(required=false)
private MyService service;

Can be shorten by a custom annotation like this:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Autowired(required=false)
public @interface MyOptional {
}

Now you can change the code to

@MyOptional
private MyService service;

You see, you can use custom annotations without reflection-code.

Upvotes: 0

Kayaman
Kayaman

Reputation: 73528

Of course you can make an annotation without using reflection. You just won't be able to use it for anything. An annotation itself has no logic, it's just a marker that depends on code that will see the annotation and then perform some action. The easiest way to demonstrate that kind of code is with reflection.

Upvotes: 2

Related Questions