referencing beans in different XML in spring

Source: Internet
Author: User

You want to refer to the bean defined in Spring-output.xml in Spring-common.xml by using a <ref bean="XXX"/> label, or a tag if it is referenced in the same XML file <ref local="XXX"/> .

 package com.spring.output;publicclass OutputHelper {        IOutputGenerator outputGenerator;        publicvoidgenerateOutput(){            this.outputGenerator.generateOutput();        }        publicvoidsetOutputGenerator(IOutputGenerator outputGenerator){            this.outputGenerator = outputGenerator;        }}

Spring-output.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-3.0.xsd ">            <bean id= "csvoutputgenerator" class=" Com.spring.output.impl.CsvOutputGenerator " />    <bean id= "jsonoutputgenerator" class=" Com.spring.output.impl.JsonOutputGenerator " /></Beans>

Spring-common.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-2.5.xsd ">            <bean id= "outputhelper" class=" Com.spring.output.OutputHelper ">        < property name="Outputgenerator" >            <ref Bean="Csvoutputgenerator"/>        </Property >    </Bean></Beans>

This is written when the context method is called:

  PackageCom.spring.hello;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.spring.output.OutputHelper; Public  class App {    Private StaticApplicationContext context; Public Static void Main(string[] args) {context =NewClasspathxmlapplicationcontext (NewString[] {"Spring-common.xml","Spring-output.xml"}); Outputhelper output = (outputhelper) Context.getbean ("Outputhelper");    Output.generateoutput (); }}

Csvoutputgenerator.java

package com.spring.output.impl;import com.spring.output.IOutputGenerator;publicclass CsvOutputGenerator implements IOutputGenerator {    publicvoidgenerateOutput() {        System.out.println("Creating CsvOutputGenerator  Output......");    }}

Jsonoutputgenerator.java

package com.spring.output.impl;import com.spring.output.IOutputGenerator;publicclass JsonOutputGenerator implements IOutputGenerator {    publicvoidgenerateOutput() {   System.out.println("Creating JsonOutputGenerator output.......");    }}

Ioutputgenerator.java

package com.spring.output;publicinterface IOutputGenerator {      publicvoidgenerateOutput();}

Copyright NOTICE: This article for Bo Master original article, reproduced please indicate this blog address!

referencing beans in different XML in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.