Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to use init-method and destroy-method attributes in Spring Configuration file ?.

Program to demonstrate how to use init-method and destroy-method attributes in Spring Configuration file
Step 1 - Create a POJO class here its Employee.java having getters, setters, constructor, init method , destroy method

package com.hubberspot.spring.InitDestroy;


public class Employee {

 private String name;
 private int age;

 public Employee() {

 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public void initialize() {
  System.out.println("Initializing the Employee bean ...");
 }

 public void destroy() {
  System.out.println("Destroying the Employee bean ...");  
 }

}




Step 2 - Create a Spring Configuration file having beans and init-method and destroy-method attributes.


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


 <bean id="employee" class="com.hubberspot.spring.InitDestroy.Employee"
  init-method="initialize" destroy-method="destroy">

  <property name="name" value="Jonty" />
  <property name="age" value="28" />

 </bean>
</beans>

Step 3 - Create a test class here its Test.java having AbstractApplicationContext class for providing registerShutdownHook method to execute destroy method.

package com.hubberspot.spring.InitDestroy;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which 
  // provides with the configuration for an application. 
  // It provides us with all the methods that BeanFactory 
  // provides. It loads the file resources in a older 
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object 
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of ApplicationContext
  // here called as ClassPathXmlApplicationContext because this 
  // bean factory reads the xml file placed in the classpath of 
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as spring.xml placed
  // at classpath of our application. 
  AbstractApplicationContext context = 
    new ClassPathXmlApplicationContext(("spring.xml"));

  // Using AbstractApplicationContext registerShutdownHook()
  // method to demonstrate destroy() method being called
  context.registerShutdownHook();

  // In order to get a object instantiated for a particular bean 
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed. 
  // Here getBean() returns an Object. We need to cast it back 
  // to the Employee object. Without implementing new keyword we 
  // have injected object of Employee just by reading an xml 
  // configuration file.
  Employee employee = (Employee)context.getBean("employee");

  System.out.println("The name of Employee is : " + employee.getName());
  System.out.println("The age of Employee is : " + employee.getAge());

 }

}




Output of the program :



How do I add an element and get an element in an ArrayList at specified index in Java ?.

Program to demonstrate how to add an element at specified index of java ArrayList object using add method in Java

package com.hubberspot.example;

import java.util.ArrayList;

public class ArrayListExample {

   public static void main(String[] args) {
 // Create an ArrayList Object of raw type
 ArrayList list = new ArrayList();

 // Add elements to it
 list.add(3); // adds at index 0
 list.add(2); // adds at index 1
 list.add("String Object"); // adds at index 3
 list.add(new Object()); // adds at index 4

 System.out.println("ArrayList displayed using index : ");
 System.out.println("----------------------------------------");

 for(int i = 0; i < list.size(); i++){
  System.out.println(list.get(i));
 }

 System.out.println("----------------------------------------");

 // add elements at position already used 
 list.add(0, 1); // inserts at index 0
 list.add(1, 2); // inserts at index 1
 list.add(2, 5); // inserts at index 2
 list.add(3, new Object()); // inserts at index 3
 list.add(4, "new string"); // inserts at index 4

 // Instead of overriding the values at indexes it shifts
 // old values by one index higher 
 // Output below displays the truth ... 

 System.out.println();
 System.out.println("ArrayList after using previous index : ");
 System.out.println("----------------------------------------");

 for(int i = 0; i < list.size(); i++){
  System.out.println(list.get(i));
 }

 System.out.println("----------------------------------------");

   }

}


Output of the program : 


 

How to provide constructor initialization to a bean using name attribute in configuration file of Spring Framework ?.

A simple example to demonstrate, how to provide constructor initialization to a bean using constructor-arg tag's name attribute in configuration file of Spring Framework introduced in Spring 3.0

1. Create a normal POJO class having constructor say 'Person' -

package com.hubberspot.spring;

public class Person {

 private String name;
 private int age;

 public Person(String name , int age) {
  this.name = name;
  this.age = age;
 }


 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 } 
}




2. Create a spring.xml file placed in classpath of your application -


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


 <bean id="person" class="com.hubberspot.spring.Person">

  <constructor-arg name="name" value="Jonty"></constructor-arg>
  <constructor-arg name="age" value="28"></constructor-arg>

 </bean>
</beans>


3. Create a Test class for the application (imp) -

package com.hubberspot.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class PersonTest {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which 
  // provides with the configuration for an application. 
  // It provides us with all the methods that BeanFactory 
  // provides. It loads the file resources in a older 
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object 
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of ApplicationContext
  // here called as ClassPathXmlApplicationContext because this 
  // bean factory reads the xml file placed in the classpath of 
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as spring.xml placed
  // at classpath of our application. 
  ApplicationContext context = 
    new ClassPathXmlApplicationContext(("spring.xml"));

  // In order to get a object instantiated for a particular bean 
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed. 
  // Here getBean() returns an Object. We need to cast it back 
  // to the Person object. Without implementing new keyword we 
  // have injected object of Person just by reading an xml 
  // configuration file.
  Person person = (Person)context.getBean("person");

  System.out.println("The name of person is : " + person.getName());
  System.out.println("The age of person is : " +person.getAge());

 }

}





Output of the program :




 
© 2021 Learn Java by Examples Template by Hubberspot