问题描述
我正在学习 Spring MVC.今天,想了解如何实现一个 JDBC DAO,我发现了这个Hello World".在 Spring(Spring,而不是 Spring MVC)中,我开始看到它(因为我认为要实现 DAO,我必须创建一个单独的 Spring Project 来执行对数据的访问......)
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
好的,这是一个独立的应用程序,这不是一个 Web 应用程序,因此它没有我的 Web 中的 Web 应用程序结构(WEB-INF 文件夹、web.xml 文件和调度程序 servlet 配置文件)应用程序)
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)
在本例中,我有一个 Beans.xml 配置文件,用于为不同的 bean 分配唯一 ID,并控制具有不同值的对象的创建,而不会影响任何 Spring 源文件...
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...
例如在这个例子中,我使用 Beans.xml 文件来传递Hello World".消息"的消息值变量,所以我可以打印这个值而不影响 HelloWorld.java 和 MainApp.java 文件
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:
这个文件是配置我的Bean Factory的文件吗?我认为,除了将文本值作为变量的值传递之外,我还可以注入一个 bean 作为另一个 bean 的依赖项.
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.
对吗?
在这个例子中,我可以不使用Beans.xml文件来代替注解系统吗?
In this example, can I don't use the Beans.xml file and using in place of the annotation system?
推荐答案
1) 这个Beans.xml(其实你可以随便命名)是一个Spring 配置文件.它拥有一个 配置元数据.
1) This Beans.xml (actually you can name it whatever you want) is a Spring configuration file. It holds a configuration metadata.
来自官方 Spring 文档:
From the official Spring documentation:
如上图所示,Spring IoC 容器消耗了一个配置元数据的形式;此配置元数据表示作为应用程序开发人员,您如何告诉 Spring 容器在您的应用程序中实例化、配置和组装对象.
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.
传统上以简单直观的 XML 格式提供配置元数据,但这不是唯一允许的配置元数据形式(请参阅第二个问题的答案)
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)
是的,你是对的:你可以注入另一个 bean 作为参考.
And yes, you are right: you can inject another bean as a reference.
来自官方 Spring 文档:
From the official Spring documentation:
Spring IoC 容器管理一个或多个 bean.这些豆子是使用您提供给容器,例如,以 XML 定义的形式.
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.
在容器本身中,这些 bean 定义表示为BeanDefinition 对象,其中包含(以及其他信息)以下元数据:
Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata:
一个包限定的类名:通常是被定义的bean的实际实现类.
A package-qualified class name: typically the actual implementation class of the bean being defined.
Bean 行为配置元素,用于说明 bean 在容器中的行为方式(范围、生命周期回调等)第四次).
Bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).
对其他 bean 的引用,这是 bean 完成工作所需的;这些引用也称为 合作者或依赖关系.
References to other beans that are needed for the bean to do its work; these references are also called collaborators or dependencies.
要在新创建的对象中设置的其他配置设置,例如,在管理一个 bean 的 bean 中使用的连接数连接池,或池的大小限制.
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.
使用官方文档中其他 bean 的引用的简单示例:
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)来自 Spring 官方文档:
2) From the official Spring documentation:
...
基于 XML 的元数据不是唯一允许的配置形式元数据.Spring IoC 容器本身与实际写入此配置元数据的格式.
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.
有关在 Spring 中使用其他形式的元数据的信息容器,见:
For information about using other forms of metadata with the Spring container, see:
基于注解的配置:Spring 2.5 引入了对基于注解的配置的支持元数据.
Annotation-based configuration: Spring 2.5 introduced support for annotation-based configuration metadata.
基于 Java 的配置:从 Spring 3.0 开始,Spring 提供了许多特性JavaConfig 项目成为核心 Spring Framework 的一部分.这样你可以使用 Java 定义应用程序类外部的 bean而不是 XML 文件.要使用这些新功能,请参阅@Configuration
、@Bean
、@Import
和 @DependsOn
注释.
另请阅读:
没有 XML 的 Spring: Spring Annotations 与 Spring XML 文件的基础知识
这篇关于关于Spring Framework应用中Beans.xml配置文件的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!