JSR-250 Annotations
•        The @Resource annotation is used in JSF (to dependency inject the managed bean) and in JAX-WS
client applications (to DI the endpoint).
•        Similarly, the @Resource can be used on a field or setter method in a Spring 2.5 application to
dependency inject a Spring container managed bean.
•        The @Resource annotation takes a name attribute.  The name specifies the name of a bean in the Spring
configuration file.
@Resource(name="orderDao1")
private OrderDao orderDao;

// or

@Resource(name="orderDao1")
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
•        In effect, the annotation functions like autowiring by name.
•        If no name is specified then the name of the bean will be derived from the name of the field.
•        If no name is provided and the name of the field does not match that in the configuration file, then
Spring uses type to discern how to wire.
•        As with other annotations, when using type, there is the possibility for ambiguity (and exception) if
more than one bean exists per type.
•        As seen already, there are several ways to provide pre/post processing in beans after they are created or
destroyed by the Spring container.
•        In the bean configuration file, initialization and destruction methods can be specified via the init-method
and destroy-method attributes.
•        A bean could also implement the BeanPostProcessor interface (providing
postProcessBeforeInitialization and postProcessAfterInitialization methods).

•        New @PostConstruct and @PreDestroy annotations offer similar capability.
•        These annotations can be used on any no-argument method in a bean class.
@PostConstruct
public void construct(){
System.out.println("I am alive");
}

@PreDestroy
public void destroy() {
System.out.println("I am going away");
}
•        The @Resource, @PostConstruct, and @PreDestroy annotations are defined in JSR-250 (common
annotations for Java).
•        Therefore, Spring 2.5 is merely supporting well known and now standard Java and Java EE annotations.
•        In fact, when using @Resource, for example, the code must import javax.annotation.Resource rather
than some Spring class/annotation.
JSR-250 Annotations
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials