In this article, we will explore about the JSR-250 Annotations with the Spring Framework. JSR 250, as a Java Specification Request, has the objective to define a set of annotations that address common semantic concepts and therefore can be used by many Java EE and Java SE components. This is to avoid redundant annotations across those components. JSR-250 Annotations were released on the 11th 05 2006. As Declarative annotation-driven configuration is more and more used in Java frameworks and applications, e.g. Spring makes more components of its framework configurable via annotations, the importance of JSR 250 is likely to increase in the future.
component class, or to fields or methods of the component class.
Even though this annotation is not marked Inherited, deployment tools are required to examine all superclasses of any component class to discover all uses of this annotation in all superclasses. All such annotation instances specify resources that are needed by the application component. Note that this annotation may appear on private fields and methods of superclasses; the container is required to perform injection in these cases as well.
@Resource takes a “name” attribute, and by default Spring will interpret that value as the bean name to be injected. In other words, it follows by-name semantics as demonstrated in this example:
public class Circle { private Point center; @Resource(name="pointB") public void setCenter(Point center) { this.center = center; } }
public class Circle { private Point center; @Resource public void setCenter(Point center) { this.center = center; } }
The name provided with the annotation will be resolved as a bean name by the BeanFactory of which the CommonAnnotationBeanPostProcessor is aware.
See the full Example
methods of the component class.
The @PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection. The method annotated with @PostConstruct MUST be invoked even if the class does not request any resources to be injected. Only one method can be annotated with this annotation. The method on which the @PostConstruct annotation is applied MUST fulfill all of the following criteria – – The method MUST NOT have any parameters except in the case of EJB interceptors in which case it takes an InvocationC ontext object as defined by the EJB specification. – The return type of the method MUST be void. – The method MUST NOT throw a checked exception. – The method on which @PostConstruct is applied MAY be public, protected, package private or private. – The method MUST NOT be static except for the application client. – The method MAY be final. – If the method throws an unchecked exception the class MUST NOT be put into service except in the case of EJBs where the EJB can handle exceptions and even recover from them.
public class Circle { @PostConstruct public void initializeCircle() { //populates the circle data cache upon initialization... System.out.println("Init of Circle"); } }
methods of the component class.
public class Circle { @PreDestroy public void destroyCircle() { //clears the circle related cache upon destruction.. System.out.println("Destroy of Circle"); } }
Spring Related Topics you may like
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…