【问题标题】:About the use of Beans.xml configuration file in Spring Framework application关于Spring Framework应用中Beans.xml配置文件的使用
【发布时间】:2013-01-03 08:32:15
【问题描述】:

我正在学习 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 应用程序结构(WEB-INF 文件夹、web.xml 文件和调度程序 servlet 配置文件)应用程序)

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

例如,在本例中,我使用 Beans.xml 文件为“message”变量传递“Hello World”消息值,因此我可以在不影响 HelloWorld 的情况下打印该值。 java 和 MainApp.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>

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

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

    对吗?

  2. 在这个例子中,我可以不使用 Beans.xml 文件来代替注解系统吗?

【问题讨论】:

    标签: java spring configuration frameworks


    【解决方案1】:

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

    来自官方 Spring 文档:

    5.2.1 配置元数据

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

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

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

    来自官方 Spring 文档:

    5.3 Bean 概览

    Spring IoC 容器管理一个或多个 bean。这些豆子是 使用您提供给 容器,例如,以 XML 定义的形式。

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

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

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

    • 对其他 bean 的引用,这是 bean 完成工作所需的;这些引用也称为collaborators or dependencies。

    • 要在新创建的对象中设置的其他配置设置,例如,在管理一个 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>
    

    2) 来自 Spring 官方文档:

    5.2.1 配置元数据

    ...

    基于 XML 的元数据不是唯一允许的配置形式 元数据。 Spring IoC 容器本身与 实际写入此配置元数据的格式。

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

    • Annotation-based configuration: Spring 2.5 引入了对基于注解的配置的支持 元数据。

    • Java-based configuration: 从 Spring 3.0 开始,Spring 提供了许多特性 JavaConfig 项目成为核心 Spring Framework 的一部分。这样你 可以使用 Java 定义应用程序类外部的 bean 而不是 XML 文件。要使用这些新功能,请参阅 @Configuration、@Bean、@Import 和 @DependsOn 注释。

    另请阅读:
    Spring Without XML: The Basics of Spring Annotations vs. Spring XML Files

    【讨论】:

    • 为了完善您的第 2 部分答案,我认为您可以将大约 95% 的 XML 配置内容转换为 Java。 必然缺少的主要内容是查找注解/配置bean 时要扫描的包的描述。 (好吧,除非你是完全手动引导 Spring 的忠实粉丝。)
    • @DonalFellows +1 以获取更多详细信息。
    【解决方案2】:

    我创建了一个文件夹/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>
    

    成功了!

    我是这样访问的:

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

    【讨论】:

      猜你喜欢
      • 2013-10-10
      • 2019-05-25
      • 1970-01-01
      • 2020-01-07
      • 2019-10-19
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多