Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Spring JSR-250 comments


May 14, 2021 Spring


Table of contents


Spring JSR-250 comments

Spring also uses JSR-250-based comments, which include @PostConstruct, @PreDestroy, @Resource comments. Because you've got other options, and although these comments aren't really needed, let me give you a brief introduction to them.

@PostConstruct and @PreDestroy comments:

To define the installation and uninstall of a bean, let's simply declare using the init-method and/or destroy-method parameters. T he init-method property specifies a method that is called immediately during the instantiation phase of the bean. S imilarly, destroy-method specifies a method that is called only before a bean is removed from the container.

You can use @PostConstruct comments as an alternative to initializing callback functions, and @PreDestroy comments as an alternative to destroying callback functions, as explained in the following example.

Example

Let's put the Eclipse IDE to work, follow these steps to create a Spring application:

Steps Describe
1 Create a project called SpringExample, and create a package called com.tutorialspoint under the src folder of the project you created.
2 Use the Add External JARs option to add the required Spring library files, as explained in the Spring Hello World Example section.
3 Create Java-class HelloWorld and MainApp under the com.tutorialspoint package.
4 Create the Beans profile Beans file under the src .xml.
5 The final step is to create the contents of all Java files and bean profiles and run the application as explained below.

Here is the contents .java HelloWorld file:

package com.tutorialspoint;
import javax.annotation.*;
public class HelloWorld {
   private String message;
   public void setMessage(String message){
      this.message  = message;
   }
   public String getMessage(){
      System.out.println("Your Message : " + message);
      return message;
   }
   @PostConstruct
   public void init(){
      System.out.println("Bean is going through init.");
   }
   @PreDestroy
   public void destroy(){
      System.out.println("Bean will destroy now.");
   }
}

Below is the contents .java mainApp file. H ere you need to register a close hook registerShutdownHook() method, which is declared in the Abstract ApplicationContext class. This will ensure a perfect shutdown and call the associated destruction method.

package com.tutorialspoint;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      AbstractApplicationContext context = 
                          new ClassPathXmlApplicationContext("Beans.xml");
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
      context.registerShutdownHook();
   }
}

The following is .xml the profile Beans, which needs to be used in the initialization and destruction methods.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

   <bean id="helloWorld" 
       class="com.tutorialspoint.HelloWorld"
       init-method="init" destroy-method="destroy">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

Once you've made the first two changes in the source file and bean profile, let's run the application. If your application is all right, this will output the following message:

Bean is going through init.
Your Message : Hello World!
Bean will destroy now.

@Resource note:

You can use the @Resource comment in a field or setter method, which works the same way in Java EE 5. @ Resource comment uses a 'name' property, which is injected as a bean name. You could say that it follows the by-name automatic connection semantics, as shown in the following example:

package com.tutorialspoint;
import javax.annotation.Resource;
public class TextEditor {
   private SpellChecker spellChecker;
   @Resource(name= "spellChecker")
   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }
   public SpellChecker getSpellChecker(){
      return spellChecker;
   }
   public void spellCheck(){
      spellChecker.checkSpelling();
   }
}

If a 'name' is not explicitly specified, the default name is derived from the field name or setter method. In the case of a field, it uses the field name, and in the case of a setter method, it uses the bean property name.