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

And*_*ili 8 java configuration spring frameworks

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

http://www.tutorialspoint.com/spring/spring_hello_world_example.htm

好的,这是一个独立的应用程序,这不是一个Web应用程序,因此它没有Web应用程序结构(WEB-INF文件夹,web.xml文件和我在Web应用程序中的调度程序servlet配置文件)

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

例如,在此示例中,我使用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>
Run Code Online (Sandbox Code Playgroud)

所以我对你有一些疑问:

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

    这样对吗?

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

inf*_*k01 19

1)这个Beans.xml(实际上你可以随意命名)是一个Spring配置文件.它包含配置元数据.

从官方Spring文档:

5.2.1配置元数据

如上图所示,Spring IoC容器使用一种配置元数据形式; 此配置元数据表示您作为应用程序开发人员如何告诉Spring容器在应用程序中实例化,配置和组装对象.

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

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

从官方Spring文档:

5.3 Bean概述

Spring IoC容器管理一个或多个bean.这些bean是使用您提供给容器的配置元数据创建的,例如,以XML定义的形式.

在容器本身内,这些bean定义表示为BeanDefinition对象,其中包含(以及其他信息)以下元数据:

  • 包限定的类名:通常是正在定义的bean的实际实现类.

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

  • 引用 bean执行其工作所需的其他bean ; 这些引用也称为协作者依赖项.

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


使用官方文档中对其他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>
Run Code Online (Sandbox Code Playgroud)

2) 从官方Spring文档:

5.2.1配置元数据

...

基于XML的元数据不是唯一允许的配置元数据形式.Spring IoC容器本身完全与实际编写此配置元数据的格式分离.

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

  • 基于注释的配置:Spring 2.5引入了对基于注释的配置元数据的支持.

  • 基于Java的配置:从Spring 3.0开始,Spring JavaConfig项目提供的许多功能成为核心Spring Framework的一部分.因此,您可以使用Java而不是XML文件在应用程序类外部定义bean.要使用这些新功能,请参阅 @Configuration,@Bean,@Import@DependsOn注释.

另请阅读:
没有XML的Spring:Spring注释与Spring XML文件的基础知识

  • 为了完善您的第2部分答案,我认为您可以将大约95%的XML配置内容转换为Java._incessarily_缺失的主要内容是在查找注释/配置bean时要扫描的包的描述.(好吧,除非你是完全手动引导Spring的忠实粉丝.) (2认同)