简体   繁体   English

关于在Spring Framework应用程序中使用Beans.xml配置文件

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

I am studying Spring MVC. 我正在学习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...) 今天,试图理解如何实现JDBC DAO,我在Spring(Spring,而不是Spring MVC)中找到了这个“Hello World”,我开始看到它(因为我认为要实现DAO我必须创建一个单独的Spring执行数据访问的项目...)

http://www.tutorialspoint.com/spring/spring_hello_world_example.htm 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) 好的,这是一个独立的应用程序,这不是一个Web应用程序,因此它没有Web应用程序结构(WEB-INF文件夹,web.xml文件和我在Web应用程序中的调度程序servlet配置文件)

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配置文件,用于为不同的bean分配唯一的ID,并控制具有不同值的对象的创建,而不会影响任何Spring源文件......

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 例如,在此示例中,我使用Beans.xml文件为“message”变量传递“Hello World”消息值,因此我可以打印此值而不会影响HelloWorld.javaMainApp.java文件

<?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 ? 此文件是配置我的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. 我认为,除了我传递一个文本值作为变量的值,我还可以将bean作为另一个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? 在此示例中,我可以不使用Beans.xml文件并使用代替注释系统吗?

1) This Beans.xml (actually you can name it whatever you want) is a Spring configuration file . 1)这个Beans.xml (实际上你可以随意命名)是一个Spring配置文件 It holds a configuration metadata . 它包含配置元数据

From the official Spring documentation: 从官方Spring文档:

5.2.1 Configuration metadata 5.2.1配置元数据

As the preceding diagram shows, the Spring IoC container consumes a form of configuration metadata; 如上图所示,Spring IoC容器使用一种配置元数据形式; this configuration metadata represents how you as an application developer tell the Spring container to instantiate, configure, and assemble the objects in your application. 此配置元数据表示您作为应用程序开发人员如何告诉Spring容器在应用程序中实例化,配置和组装对象。

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) 传统上,配置元数据以简单直观的XML格式提供, 但它不是唯一允许的配置元数据形式 (请参阅第二个问题的答案)

And yes, you are right: you can inject another bean as a reference. 是的,你是对的:你可以注入另一个bean作为参考。

From the official Spring documentation: 从官方Spring文档:

5.3 Bean overview 5.3 Bean概述

A Spring IoC container manages one or more beans. Spring IoC容器管理一个或多个bean。 These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML definitions. 这些bean是使用您提供给容器的配置元数据创建的,例如,以XML定义的形式。

Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata: 在容器本身内,这些bean定义表示为BeanDefinition对象,其中包含(以及其他信息)以下元数据:

  • 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; 引用 bean执行其工作所需的其他bean ; 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. 要在新创建的对象中设置的其他配置设置,例如,在管理连接池的Bean中使用的连接数,或池的大小限制。


Simple example of using references to other beans from the official documentation: 使用官方文档中对其他bean的引用的简单示例:

<?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: 2)从官方Spring文档:

5.2.1 Configuration metadata 5.2.1配置元数据

... ...

XML-based metadata is not the only allowed form of configuration metadata . 基于XML的元数据不是唯一允许的配置元数据形式 The Spring IoC container itself is totally decoupled from the format in which this configuration metadata is actually written. Spring IoC容器本身完全与实际编写此配置元数据的格式分离。

For information about using other forms of metadata with the Spring container, see: 有关在Spring容器中使用其他形式的元数据的信息,请参阅:

  • Annotation-based configuration : Spring 2.5 introduced support for annotation-based configuration metadata. 基于注释的配置 :Spring 2.5引入了对基于注释的配置元数据的支持。

  • Java-based configuration : Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. 基于Java的配置 :从Spring 3.0开始,Spring JavaConfig项目提供的许多功能成为核心Spring Framework的一部分。 Thus you can define beans external to your application classes by using Java rather than XML files. 因此,您可以使用Java而不是XML文件在应用程序类外部定义bean。 To use these new features, see the @Configuration , @Bean , @Import and @DependsOn annotations. 要使用这些新功能,请参阅@Configuration@Bean@Import@DependsOn注解。

Also read this: 另请阅读:
Spring Without XML: The Basics of Spring Annotations vs. Spring XML Files 没有XML的Spring:Spring注释与Spring XML文件的基础知识

I created a folder /src/main/resources/ and placed this beans.xml file there: 我创建了一个文件夹/src/main/resources/并将这个beans.xml文件放在那里:

<?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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM