All IT Courses 50% Off
JAVA Tutorials

What are Spring Bean Configurations?

Spring Bean are the objects that are managed by Spring IoC and are also acts as a backbone of an application. There are three methods which provide configuration metadata to Spring Container:

  1. XML Based Configuration: Configuration XML file is used to create Beans.
  2. Annotation Based Configuration: By using annotations like @Service, @Component, @Scope.
  3. Java Based Configuration: By using @Configuration, @ComponentScan, @Bean.

XML Based Configuration:

Here we will use XML configuration to define beans. For this we need three maven dependencies:

spring-core: It contains the most basic classes required,  which are required to work with Spring modules.

spring-beans: It provides org.springframework.beans.factory.BeanFactory interface that is required for Spring beans.

spring-context: It provides org.springframework.context.ApplicationContext interface, which is required for additional features like bean lifecycle event listeners, AOP capabilities, etc.

Step 1: Create a maven dependency file pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd;
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.google.webmvc</groupId>
    <artifactId>SpringDemos</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging> 
    <name>SpringDemos</name>
    <url>http://maven.apache.org</url> 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>5.2.0.RELEASE</spring.version>
    </properties>
 
    <dependencies>
        <!-- Spring Dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</project>

Step 2: Define all the Spring beans in a single configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:context="https://www.springframework.org/schema/context"
    xsi:schemaLocation="https://www.springframework.org/schema/beans 
    https://www.springframework.org/schema/beans/spring-beans.xsd 
    https://www.springframework.org/schema/context 
    https://www.springframework.org/schema/context/spring-context.xsd"> 
    <bean id="operations"    class="com.google.spring.beans.Operations"></bean>
    <bean id="employee" class="com.google.spring.beans.Employee"></bean>
    <bean id="department"    class="com.google.spring.beans.Department"></bean>
</beans>

Step 3: Define all the beans in multiple configuration files and import the main xml file.

employee.xml

<beans>
    <bean id="employee" class="com.google.spring.beans.Employee"></bean> 
</beans>

department.xml

<beans> 
    <bean id="department" class="com.google.spring.beans.Department"></bean> 
</beans> 

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans> 
    <import resource="employee.xml"/>
    <import resource="department.xml"/> 
    <bean id="operations" class="com.google.spring.beans.Operations"></bean> 
</beans>

Step 4: Create a main java file

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
public class XmlConfigExample 
{
    public static void main( String[] args )
    {
    @SuppressWarnings("resource")
    ApplicationContext ctx = new
                  ClassPathXmlApplicationContext( "com/google/core/demo/beans/beans.xml" ); 
        Employee employee = ctx.getBean(Employee.class); 
        Department department = ctx.getBean(Department.class); 
        Operations operations = ctx.getBean(Operations.class); 
        System.out.println(department);
        System.out.println(employee); 
        operations.helloWorld();
    }
}

Java Based Configuration:

Spring Beans can be created using java configuration using annotations like @Bean, @ComponentScan, and @Configuration.

Component Scanning can be done in two steps:

  1. Using one of the four annotations: @Component, @Repository, @Service, @Controller.

EmployeeManagerImpl.java 

import org.springframework.stereotype.Service;
import com.google.spring.model.Employee;
import com.google.spring.service.EmployeeManager; 
@Service
public class EmployeeManagerImpl implements EmployeeManager { 
    @Override
    public Employee create() {
        Employee emp =  new Employee();
        emp.setId(1);
        emp.setName("ABC");
        return emp;
    }
}
  1. Including Bean Packages in @ComponentScan

AppConfig.java

@Configuration
@ComponentScan(basePackages = "com.google.spring.service")
public class AppConfig { 
}

Example:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.google.spring.model.Employee;
import com.google.spring.service.EmployeeManager; 
public class Main 
{
    public static void main( String[] args )
    {
        //Method 1
        //ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); 
        //Method 2
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(AppConfig.class);
        ctx.refresh(); 
        EmployeeManager empManager = ctx.getBean(EmployeeManager.class);
        Employee emp = empManager.create(); 
        System.out.println(emp);
    }
}

Spring application can also be created using @Bean and @Configuration annotations.

Step 1: Create a Java Bean Class

EmployeeManagerImpl.java

public class EmployeeManagerImpl implements EmployeeManager {
 
    @Override
    public Employee create() {
        Employee emp =  new Employee();
        emp.setId(1);
        emp.setName("ABC");
        return emp;
    }
}

Step 2: Create @Bean class

AppConfig.java 

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.spring.service.EmployeeManager;
import com.google.spring.service.impl.EmployeeManagerImpl; 
@Configuration
public class AppConfig { 
    @Bean
    public EmployeeManager employeeManager() {
        return new EmployeeManagerImpl();
    } 
}

Example:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.google.spring.model.Employee;
import com.google.spring.service.EmployeeManager;
 
public class Main 
{
    public static void main( String[] args )
    {
        //Method 1
        //ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); 
        //Method 2
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(AppConfig.class);
        ctx.refresh(); 
        EmployeeManager empManager = ctx.getBean(EmployeeManager.class);
        Employee emp = empManager.create(); 
        System.out.println(emp);
    }
}

Facebook Comments
Tags

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button
Close
Close