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 Java based configuration in Spring Framework without using XML based configuration ?.

A simple application to demonstrate how to use Java based configuration in Spring Framework without using XML based configuration.

The following steps / classes are taken for this simple application to work.

1. A simple POJO bean as Address
package com.hubberspot.spring.configuration;


public class Address {

 private String street;
 private String city;
 private String state;

 public Address() {

 }


 public String getStreet() {
  return street;
 }


 public void setStreet(String street) {
  this.street = street;
 }


 public String getCity() {
  return city;
 }


 public void setCity(String city) {
  this.city = city;
 }


 public String getState() {
  return state;
 }


 public void setState(String state) {
  this.state = state;
 }
 
 public void initialize() {
  System.out.println("Initialization of Address ... ");
 }
 
 public void destroy() {
  System.out.println("Destruction of Address ... ");
 }

}




2. A simple POJO bean as Employee having being injected by Address object.
package com.hubberspot.spring.configuration;

import javax.inject.Inject;

// Create a POJO class Employee which has a 
// Address Object reference as instance variable
public class Employee {

 private String name;
 private int age;

 @Inject
 private Address address;

 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 Address getAddress() {
  return address;
 }

 public void setAddress(Address address) {
  this.address = address;
 }

 public void init() {
  System.out.println("Initialization of Employee ... ");
 }

 public void clean() {
  System.out.println("Destruction of Employee ... ");
 }

}



3. Java Based Configuration class replacing Spring Configuration XML.
package com.hubberspot.spring.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// @Configuration makes this Java class as configuration
// for the Spring Framework. It declares two beans here 
// instead of providing it in spring configuration file
// we can provide it in Java class

@Configuration
public class EmployeeConfiguration {

 // @Bean registers Address as a Spring bean

 @Bean(initMethod="initialize", destroyMethod= "destroy")
 public Address address() {

  Address address = new Address();
  address.setStreet("Town Hall Street");
  address.setCity("Pune");
  address.setState("Maharashtra");
  return address;
 }

 // @Bean registers Employee as a Spring bean

 @Bean(initMethod="init", destroyMethod= "clean")
 public Employee employee() {
  Employee employee = new Employee();
  employee.setName("Jonty");
  employee.setAge(28);
  employee.setAddress(address());
  return employee;
 }

}



4. A simple Spring xml file having components scan tag


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" 
 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.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!-- This tag helps Spring to automatically scan annotations based components -->
 <context:component-scan base-package="com.hubberspot.spring.configuration">
 </context:component-scan>

</beans>


5. Test class for running the example

package com.hubberspot.spring.configuration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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.
  // Since we are using Java based configuration method so instead
  // of using concrete implementation of ApplicationContext
  // which is ClassPathXmlApplicationContext we are using 
  // AnnotationConfigApplicationContext to load and register
  // annotated configuration classes. Instead of xml we provide
  // name of the java configuration Class object (instance of this Class)

  AbstractApplicationContext context = 
    new AnnotationConfigApplicationContext(EmployeeConfiguration.class);

  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");

  if(employee.getAddress()==null){
   System.out.println("The Employee Name : " + employee.getName());
   System.out.println("The Employee Age : " + employee.getAge());
   System.out.println("The Employee Address : " + "is not provided");
  }
  else{
   System.out.println("The Employee Name : " + employee.getName());
   System.out.println("The Employee Age : " + employee.getAge());
   System.out.println("The Employee Address : " +   
     employee.getAddress().getStreet() + " " +
     employee.getAddress().getCity() + " " +
     employee.getAddress().getState());
  }
 }
}



Note : To run above example application classpath must include all Spring Jars and CGLIB jar [cglib-nodep-x.x.x.jar]

Output of the program :


 
© 2021 Learn Java by Examples Template by Hubberspot