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:
-
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?
-
In this example, can I don’t use the Beans.xml file and using in place of the annotation system?
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:
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:
Simple example of using references to other beans from the official documentation:
2)
From the official Spring documentation:
Also read this:
Spring Without XML: The Basics of Spring Annotations vs. Spring XML Files