Spring JSR-250 Annotations with Example

By | June 22, 2021

In this post, We will learn about the Spring JSR-250 Annotations with Examples using a Demo Project.

Spring Framework also supports JSR-250 annotations that include @Resource, @PostConstruct, and @PreDestroy annotations.

These annotations are not really required because We already have other alternates or options to achieve  the same functionalities that we achieve using these annotations

@Resource Annotation

We can use @Resource annotation on or before fields or setter methods. The @Resource annotation usually takes a ‘name’ attribute that will be interpreted as the bean name to be injected.

If We do not specify the ‘name’ attribute explicitly then the default name is derived from the class field name or setter method. In the case of a field, it takes the field name and in the case of a setter method, it takes the bean property name.

@PostConstruct and @PreDestroy Annotations:

To set up and teardown functionalities for a Spring bean, we simply declare the <bean> with init-method and/or destroy-method parameters. The init-method attribute usually specifies a method that is to be called on the bean immediately after instantiation. On another side, We can use a destroy-method attribute to specify a method that is called just before a bean is removed or destroyed from the spring container.

We can usually use @PostConstruct annotation as an alternate of initialization callback and @PreDestroy annotation as an alternate of destruction callback.

pom.xml

Employee.java

Java Class which is registered as Spring bean in Spring Container.

Address.java

applicationContext.xml

ClientTest.java

Client Program with the main method

If you run ClientTest.java as Java Application then it will give the below output: 

That’s all about Spring JSR-250 Annotations with Example

You May Also Like:

Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *