开发者

Are Annotations generally realized with AOP and can they "do" Depedency Injection?

How are annotations in Spring Framework realized? Are they realized by using Dependency Injection or Aspect Oriented Programming?

I thought they use AOP, well, because something annotated will do something which is specified somewhere else. On the othe开发者_JAVA百科r hand, something like @Autowired @Qualifier("beanID") IS Dependency Injection - well, maybe Annotations are based on AOP and can realize Depedency Injection? Is this correct?

I was just thinking about it, because I was reading about the main parts of spring: di and aop and I wondered where the annotations belonged to.


Annotations don't do anything, code can do things with Annotations, that's a huge difference.

I suggest you read Annotations from the Java Tutorial to understand what annotations are about.

Basically there are three ways to deal with annotations (there might be more, but I know of three):

  • Pre-compilation annotation processing
  • AspectJ Pointcut matching (matching at compile-time, evaluating at runtime)
  • Reflection (using AnnotatedElement.getAnnotation(annotationtype))

Spring uses Reflection to read annotation values and then either inject dependencies ore add AOP advices (and many more things). But if you use static AspectJ compilation with Spring, some of the same annotations are used in pointcuts.


In the context of Spring (well, not only, but this is what your question is about), annotations are mere metadata -- information associated with the code. They are simply a convenient (or not) replacement for the XML metadata, that does the same thing: states that a given setter should be called with a given parameter, for instance. The main difference between annotations and XML metadata is that annotations are bundled with the code, therefore they can serve as convenient defaults, while you're still able to overwrite them with XML bean definitions.

In order to have the "job done" it's never enough to have your code annotated (as it's never enough to write an XML application context descriptor. You need some sort of bean factory / application context that is able to read that metadata and do whatever is required.


Some JSR-250 Annotations are recognized within Spring and a dynamic proxy is created such as @PostConstruct & @PreDestroy. This is done during container startup. DI type annotations are discovered by context directives such as and . Since DI happens before the bean post processing, a proxy is not necessary.

Annotations in Spring Projects (over 470) - http://gordondickens.com/wordpress/2011/05/20/annotation-reference-for-spring-projects/


They are sortable - Click on the link under the content to get to the Google Spreadsheet.

This will allow you to sort them.

By the way, there are now over 500 annotations (added Hibernate 3 recently).

I plan on adding more detail when I get more focussed time, including:

  • Annotation parameters
  • Description of Annotation's use
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜