简体   繁体   English

在Spring 4中从另一个xml文件访问xml中定义的bean

[英]accessing a bean defined in an xml from another xml file in Spring 4

In a Spring web app I have some common beans such as dataSource , transactionManager , mailSender etc. defined in an xml file. Spring web app我在xml文件中定义了一些常见的bean,例如dataSourcetransactionManagermailSender等。 I also have many other xml files which I am using to start some tasks, and I am importing them in here: 我还拥有许多其他的xml文件,这些文件可用于启动某些任务,并将它们导入此处:

servlet.xml: servlet.xml:

<bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
        p:password="${jdbc.password}"
        p:initialSize="1"
        p:maxWait="30000"
        p:maxIdle="-1"
        p:maxActive="-1" />

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
</bean>
<import resource="aTaskStarter.xml"/>

Now in the aTaskStarter.xml file I would like to access those beans and pass them to the java class: 现在,在aTaskStarter.xml文件中,我想访问那些bean并将它们传递给java类:

aTaskStarter.xml: aTaskStarter.xml:

<bean id="dailyReportTask" class="com.package.task.MyTask">
    <property name="dataSource" ref="dataSource" />
</bean>

How can I do this? 我怎样才能做到这一点?

将常见内容放入第三个xml文件中,并将其包含在其他两个文件中:

<import resource="common.xml" />

You should just move up the import to before the bean definitions and all should function as you expect... 您应该只将导入移到Bean定义之前,并且所有功能都应按预期运行...

The documentation states 文档说明

It's often useful to split up container definitions into multiple XML files. 将容器定义分成多个XML文件通常很有用。 One way to then load an application context which is configured from all these XML fragments is to use the application context constructor which takes multiple Resource locations. 然后加载由所有这些XML片段配置的应用程序上下文的一种方法是使用具有多个Resource位置的应用程序上下文构造函数。 With a bean factory, a bean definition reader can be used multiple times to read definitions from each file in turn. 对于bean工厂,可以多次使用bean定义读取器依次读取每个文件中的定义。

Generally, the Spring team prefers the above approach, since it keeps container configurations files unaware of the fact that they are being combined with others. 通常,Spring团队更喜欢上面的方法,因为它使容器配置文件不知道它们与其他文件组合在一起的事实。 However, an alternate approach is to from one XML bean definition file, use one or more instances of the import element to load definitions from one or more other files. 但是,另一种方法是从一个XML bean定义文件中,使用import元素的一个或多个实例从一个或多个其他文件中加载定义。 Any import elements must be placed before bean elements in the file doing the importing. 必须在文件中将任何import元素放置在文件中的bean元素之前。 Let's look at a sample: 让我们看一个示例:

<beans>

  <import resource="services.xml"/>

  <import resource="resources/messageSource.xml"/>

  <import resource="/resources/themeSource.xml"/>

  <bean id="bean1" class="..."/>

  <bean id="bean2" class="..."/>
  . . .

In this example, external bean definitions are being loaded from 3 files, services.xml, messageSource.xml, and themeSource.xml. 在此示例中,将从3个文件(services.xml,messageSource.xml和themeSource.xml)加载外部bean定义。 All location paths are considered relative to the definition file doing the importing, so services.xml in this case must be in the same directory or classpath location as the file doing the importing, while messageSource.xml and themeSource.xml must be in a resources location below the location of the importing file. 所有位置路径都被视为相对于执行导入的定义文件而言,因此在这种情况下,services.xml必须与执行导入的文件位于同一目录或类路径位置,而messageSource.xml和themeSource.xml必须位于资源中导入文件位置下方的位置。 As you can see, a leading slash is actually ignored, but given that these are considered relative paths, it is probably better form not to use the slash at all. 如您所见,前斜杠实际上被忽略了,但是考虑到它们被视为相对路径,最好不要使用任何斜杠。

The contents of the files being imported must be fully valid XML bean definition files according to the DTD, including the top level beans element. 根据DTD,要导入的文件内容必须是完全有效的XML bean定义文件,包括顶级bean元素。

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

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