关于Spring Framework应用中Beans.xml配置文件的使用

我正在学习 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 配置文件)应用程序)

在本例中,我有一个 Beans.xml 配置文件,用于为不同的 bean 分配唯一 ID,并控制具有不同值的对象的创建,而不会影响任何 Spring 源文件...

例如在这个例子中,我使用 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>

所以我有一些问题要问你:

  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文件来代替注解系统吗?

higays1 回答:关于Spring Framework应用中Beans.xml配置文件的使用

1) 这个Beans.xml(其实你可以随便命名)是一个Spring 配置文件.它拥有一个 配置元数据.

来自官方 Spring 文档:

如上图所示,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 格式提供配置元数据,但这不是唯一允许的配置元数据形式(请参阅第二个问题的答案)

是的,你是对的:你可以注入另一个 bean 作为参考.

来自官方 Spring 文档:

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 对象,其中包含(以及其他信息)以下元数据:

  • 一个包限定的类名:通常是被定义的bean的实际实现类.

  • A package-qualified class name: typically the actual implementation class of the bean being defined.

Bean 行为配置元素,用于说明 bean 在容器中的行为方式(范围、生命周期回调等)第四次).

对其他 bean 的引用,这是 bean 完成工作所需的;这些引用也称为 合作者依赖关系.

要在新创建的对象中设置的其他配置设置,例如,在管理一个 bean 的 bean 中使用的连接数连接池,或池的大小限制.


使用官方文档中其他 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 容器本身与实际写入此配置元数据的格式.

有关在 Spring 中使用其他形式的元数据的信息容器,见:

  • 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配置文件的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持前端之家!

本文链接:https://www.f2er.com/3175493.html

大家都在问