关于在Spring Framework应用程序中使用Beans.xml配置文件 [英] About the use of Beans.xml configuration file in Spring Framework application

查看:136
本文介绍了关于在Spring Framework应用程序中使用Beans.xml配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Spring MVC。今天,试图了解如何实现一个JDBC DAO,我在Spring(Spring,而不是Spring MVC)中发现了这个Hello World,我开始看到它(因为我认为要实现一个DAO我必须创建一个单独的Spring执行数据访问权限的项目...)

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-INF文件夹,web.xml文件和我的web应用程序中的dispatcher 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 文件传递message变量的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>

所以我有一些问题:


  1. 此文件是配置我的 Bean Factory 的文件吗?我认为,以及我传递一个文本值作为变量的值我也可以注入一个bean作为另一个bean的依赖。

  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.

是不是?

在此示例中,我可以不使用 Beans.xml 文件并使用替代注释系统吗? / p>

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


推荐答案

1) .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:


< h2> 5.2.1配置元数据

如上图所示,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.


5.3 Bean概述



Spring IoC容器管理一个或多个bean。这些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 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中使用的连接数,或者该池的大小限制。

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:


5.2.1配置元数据



...

5.2.1 Configuration metadata

...

基于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的其他形式的元数据的信息
container,请参阅:

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的配置
从Spring 3.0开始,Spring $提供的许多功能b $ b JavaConfig项目成为Spring Framework核心的一部分。因此,
可以通过使用Java
而不是XML文件来定义应用程序类外部的bean。要使用这些新功能,请参阅
@Configuration @Bean @Import @DependsOn 注释。

另请阅读:

Spring无XML:Spring注释与Spring XML文件的基础

这篇关于关于在Spring Framework应用程序中使用Beans.xml配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆