About the use of Beans.xml configuration file in Spring Framework application About the use of Beans.xml configuration file in Spring Framework application spring spring

About the use of Beans.xml configuration file in Spring Framework application


1) This Beans.xml (actually you can name it whatever you want) is a Spring configuration file. It holds a configuration metadata.

From the official Spring documentation:

5.2.1 Configuration metadata

As the preceding diagram shows, the Spring IoC container consumes a form of configuration metadata; this configuration metadata represents how you as an application developer tell the Spring container to instantiate, configure, and assemble the objects in your application.

Configuration metadata is traditionally supplied in a simple and intuitive XML format, but it is not the only allowed form of configuration metadata (see the answer to your second question)

And yes, you are right: you can inject another bean as a reference.

From the official Spring documentation:

5.3 Bean overview

A Spring IoC container manages one or more beans. These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML definitions.

Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata:

  • A package-qualified class name: typically the actual implementation class of the bean being defined.

  • Bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).

  • References to other beans that are needed for the bean to do its work; these references are also called collaborators or dependencies.

  • Other configuration settings to set in the newly created object, for example, the number of connections to use in a bean that manages a connection pool, or the size limit of the pool.


Simple example of using references to other beans from the official documentation:

<?xml version="1.0" encoding="UTF-8"?><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.xsd">    <bean id="exampleBean" class="examples.ExampleBean">        <!-- setter injection using the nested <ref/> element -->        <property name="beanOne">            <ref bean="anotherExampleBean"/>        </property>        <!-- setter injection using the neater 'ref' attribute -->        <property name="beanTwo" ref="yetAnotherBean"/>        <property name="integerProperty" value="1"/>    </bean>    <bean id="anotherExampleBean" class="examples.AnotherBean"/>    <bean id="yetAnotherBean" class="examples.YetAnotherBean"/></beans>

2)From the official Spring documentation:

5.2.1 Configuration metadata

...

XML-based metadata is not the only allowed form of configuration metadata. The Spring IoC container itself is totally decoupled from the format in which this configuration metadata is actually written.

For information about using other forms of metadata with the Spring container, see:

  • Annotation-based configuration: Spring 2.5 introduced support for annotation-based configuration metadata.

  • Java-based configuration: Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus you can define beans external to your application classes by using Java rather than XML files. To use these new features, see the @Configuration, @Bean, @Import and @DependsOn annotations.

Also read this:
Spring Without XML: The Basics of Spring Annotations vs. Spring XML Files


I created a folder /src/main/resources/ and placed this beans.xml file there:

<?xml version="1.0" encoding="UTF-8"?><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-4.3.xsd"> <bean id="employee" class="HelloSpring.Employee"> <property name="name" value="test spring"></property> </bean></beans>

It worked!

I accessed it like this:

package HelloSpring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Customer {    public static void main(String[] args) {        ApplicationContext ctx = new ClassPathXmlApplicationContext("./beans.xml");        Employee obj = (Employee) ctx.getBean("employee");        obj.displayName();    }}