Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am studying Spring MVC. Today, trying to understand how implement a JDBC DAO, I have found this "Hello World" in Spring (Spring, not Spring MVC) and I begin to see it (because I think that to realize a DAO I have to create a separate Spring Project that execute the access to the data...)

http://www.tutorialspoint.com/spring/spring_hello_world_example.htm

OK, this is a standalone application and this is not a web application, so it doesn't have the web application structure (WEB-INF folder, web.xml file and the dispatcher servlet configuration file that I have in my web app)

In this example I have a Beans.xml configuration file that is used to assign unique IDs to different beans and to control the creation of objects with different values without impacting any of the Spring source files...

For example in this example I use the Beans.xml file to pass the "Hello World" message value for "message" variable and so I can print this value without impacting HelloWorld.java and MainApp.java files

<?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-3.0.xsd">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

So I have some question for you about it:

  1. Is this file the file that configure my Bean Factory? I think that, as well as I pass a text value as the value of a variable I could also inject a bean as a dependency of another bean.

    Is it right?

  2. In this example, can I don't use the Beans.xml file and using in place of the annotation system?

like image 670
AndreaNobili Avatar asked Jan 19 '13 21:01

AndreaNobili


1 Answers

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

like image 181
informatik01 Avatar answered Oct 13 '22 02:10

informatik01