BeanFactory Example in Spring - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In order to instantiate the Spring core container, we can we create an object of the BeanFactory or ApplicationContext implementation classes by passing the Spring configuration.
The Spring Framework comes with two distinct types of containers:

BeanFactory Interface
ApplicationContext Interface

BeanFactory Interface
ApplicationContext Interface
BeanFactory is an interface, which is coming from the org.springframework.beans.factory package. This is a root interface for accessing the spring bean container. The name itself suggested that the BeanFactory is an implementation of the Factory design pattern.
The BeanFactory interface is implemented by objects that hold a number of bean definitions, and each bean is uniquely identified by the name or id. Based on the bean definition the factory will create and return the bean instances.
The BeanFactory interface provides the basic endpoint for the spring core container towards the applications can access the core container services.
There are several implementations of BeanFactory interface. But the most useful one is org.springframework.context.support.ClassPathXmlApplicationContext.
In the below example, we are going to implement the Spring application step by step, using maven.
Step 1 :
Create pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.onlinetutorialspoint</groupId>
   <artifactId>SpringCoreExmples</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>SpringCoreExmples</name>
   <url>http://maven.apache.org</url>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
      <!-- Spring framework -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring</artifactId>
         <version>2.5.6</version>
      </dependency>
   </dependencies>
</project>
Step 2 :
Create Employee class :
package com.onlinetutorialspoint.springcoreapplication;

public class Employee {

    private int employeeId;
    private String employeeName;

    public Employee(int employeeId, String employeeName) {
        this.employeeId = employeeId;
        this.employeeName = employeeName;
    }

    public int getEmployeeId() {
        return employeeId;
    }

    public String getEmployeeName() {
        return employeeName;
    }

}
<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-2.5.xsd">
    <bean id="employee" class="com.onlinetutorialspoint.springcoreapplication.Employee">
         <constructor-arg value="200" />
         <constructor-arg value="chandrashekhar" />
    </bean>
</beans>
package com.onlinetutorialspoint.springcoreapplication;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {

    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("Spring-Bean.xml");
        Employee employee = (Employee) beanFactory.getBean("employee");
        System.out.println("employee Id : " + employee.getEmployeeId());
        System.out.println("employee Name : " + employee.getEmployeeName());
    }
}
On the above example, by creating the ClassPathXmlApplicationContext object we obtain the BeanFactory. By using the getBean() method available in the BeanFactory, we can get the actual bean by passing the bean name, which is already defined in the Spring-Bean.xml.
Done!
Happy Learning 🙂


BeanFactory Example with Maven

File size: 17 KB
Downloads: 1041


Dependency Injection (IoC) in spring with Example
Spring Collection Map Dependency Example
@Qualifier annotation example in Spring
Spring Collection Dependency List Example
Spring Bean Autowire By Constructor Example
Types of Spring Bean Scopes Example
Spring Bean Autowire ByName Example
Spring AOP ThrowsAdvice Example XML
spring expression language example
Spring AOP Example Before and After Advice XML
Spring Bean Autowire ByType Example
How to Get All Spring Beans Details Loaded in ICO
Spring AOP Around Advice Example XML
@Component,@Service,@Repository,@Controller in spring
Spring MVC HelloWorld

Dependency Injection (IoC) in spring with Example
Spring Collection Map Dependency Example
@Qualifier annotation example in Spring
Spring Collection Dependency List Example
Spring Bean Autowire By Constructor Example
Types of Spring Bean Scopes Example
Spring Bean Autowire ByName Example
Spring AOP ThrowsAdvice Example XML
spring expression language example
Spring AOP Example Before and After Advice XML
Spring Bean Autowire ByType Example
How to Get All Spring Beans Details Loaded in ICO
Spring AOP Around Advice Example XML
@Component,@Service,@Repository,@Controller in spring
Spring MVC HelloWorld






prasanna
October 24, 2018 at 4:18 pm - Reply 

well written easy to understand










prasanna
October 24, 2018 at 4:18 pm - Reply 

well written easy to understand




well written easy to understand