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 @Component annotation for automatically configuring a Spring bean without using configuration xml file ?.

Program to demonstrate how to use @Component annotation for automatically configuring a Spring bean without using configuration xml file.


1. Create a class by name Side.java having @Component annotation and place it in the package 'com.hubberspot.spring.components'. This package will have our all the java files and this package entry goes into Spring frameworks configuration xml in the 'context:component-scan' tag. By providing this entry we are telling Spring container to automatically scan this package and configure the classes as components/bean

package com.hubberspot.spring.components;

import org.springframework.stereotype.Component;


// We use @Component Annotation to make a class
// being automatically treated as a Spring bean
// without making any entry for it in Spring
// Configuration xml file 

@Component
public class Side {

 private int length;
 private int pointX;
 private int pointY;


 public int getLength() {
  return length;
 }

 public void setLength(int length) {
  this.length = length;
 }

 public int getPointX() {
  return pointX;
 }

 public void setPointX(int pointX) {
  this.pointX = pointX;
 }


 public int getPointY() {
  return pointY;
 }

 public void setPointY(int pointY) {
  this.pointY = pointY;
 }



}




2. Create a class Square.java having @Component annotation and being @Autowired by Side bean defined at step 1 -

package com.hubberspot.spring.components;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//We use @Component Annotation to make a class
//being automatically treated as a Spring bean
//without making any entry for it in Spring
//Configuration xml file 

@Component
public class Square {

 // Square has Side as its one 
 // of the property which is 
 // being autowired to it by placing 
 // @Autowired annotation over it


 @Autowired
 private Side side;


 public Side getSide() {
  return side;
 }


 public void setSide(Side side) {
  this.side = side;
 }



}




3. Create a Spring Configuration file having 'context:component-scan' tag with base-package="com.hubberspot.spring.components". This attribute provides Spring the information about the beans that needs to be scan in the package



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

    <!-- In order to turn on Auto Scanning in Spring we need to provide a tag 
        by name <context:component-scan> see the tag below .... -->

    <context:component-scan base-package="com.hubberspot.spring.components" />

</beans>


4. Run the Test class

package com.hubberspot.spring.components;

import org.springframework.context.ApplicationContext;
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. 
  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 Square object. Without implementing new keyword we 
  // have injected object of Square just by reading an xml 
  // configuration file.
  Square square = (Square)context.getBean("square");

  System.out.println("The length of Square is : " + 

square.getSide().getLength());
  System.out.println("The x co-ordinate of Square is : " + 

square.getSide().getPointX());
  System.out.println("The y co-ordinate of Square is : " + 

square.getSide().getPointY());

 }

}




Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot