What is Bean Factory ?

  • It is represented by the interface org.springframework.beans.factory. It is the main and the basic way to access the Spring container
  • It is built upon Factory Design Pattern that contains a collection of beans. The BeanFactory holds Bean Definitions of multiple beans within itself and then instantiates the bean whenever asked for by clients.
  • It provides DI / IOC mechanism for the Spring. It is the actual container which instantiates, configures, and manages a number of beans. It loads the beans definitions and their property descriptions from some configuration source (for example, from XML configuration file).

Lets Implement

Using classpath –

Project structure be like below image

1

add a dependency on pom.xml file –

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>4.3.4.RELEASE</version>
</dependency>

bean class “Greetings.java

package com.spring.core;
public class Greetings {
    private String message;
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

beans.xml file is in classpath so can access using “ClassPathResource” class

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
<beans>
    <bean id="greet" class="com.spring.core.Greetings">
        <property name="message" value="Hello One"></property>
    </bean>
</beans>

Main class

package com.spring.core;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Application {
    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
        Greetings greet = (Greetings) factory.getBean("greet"); 
        System.out.println(greet.getMessage());
    }
}

Using File System –

2

Everything is like above, only class name will change because previously we are reading file from classpath and now reading from file system so we’ll use “FileSystemResource” class

package com.spring.core;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class Application {
    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
        Greetings greet = (Greetings) factory.getBean("greet"); 
        System.out.println(greet.getMessage());
    }
}

For more detail, Please watch video –

Thanks for reading 🙂

Please Subscribe our you tube channel Almighty Java

What is Spring Framework ?

The Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so you can focus on your application.

Spring enables you to build applications from “plain old Java objects” (POJOs) and to apply enterprise services non-invasively to POJOs. This capability applies to the Java SE programming model and to full and partial Java EE.

  • It is an open source Java platform and it was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.
  • It is an application framework and inversion of control container for the Java platform. The framework’s core features can be used by any Java application.
  • It is lightweight because of its POJO implementation. The Spring Framework doesn’t force the programmer to inherit any class or implement any interface. That is why it is said non-invasive.
  • It can be thought of as a framework of frameworks because it provides support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF etc.
  • Loosely coupled and integrated framework for developing enterprise applications in java.

Examples of how you, as an application developer, can benefit from the Spring platform:

  • Make a Java method execute in a database transaction without having to deal with transaction APIs.
  • Make a local Java method a remote procedure without having to deal with remote APIs.
  • Make a local Java method a management operation without having to deal with JMX APIs.
  • Make a local Java method a message handler without having to deal with JMS APIs.

Thanks for reading 🙂

Please Subscribe our you tube channel Almighty Java