Spring references beans in different xml

Source: Internet
Author: User

Spring references beans in different xml

To reference the bean defined in the Spring-Common.xml in the Spring-Output.xml, you need to use Label. If it is referenced in the same xml file, use Label.

 package com.spring.output;public class OutputHelper {        IOutputGenerator outputGenerator;        public void generateOutput(){            this.outputGenerator.generateOutput();        }        public void setOutputGenerator(IOutputGenerator outputGenerator){            this.outputGenerator = outputGenerator;        }}

Spring-Output.xml


  
      
       
    
   
  

Spring-Common.xml


  
      
           
                
             
     
        
   
  

When calling the context method, write as follows:

 package com.spring.hello;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.output.OutputHelper;public class App {    private static ApplicationContext context;    public static void main(String[] args)    {        context = new ClassPathXmlApplicationContext(new String[] {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;public class CsvOutputGenerator implements IOutputGenerator {    public void generateOutput() {        System.out.println(Creating CsvOutputGenerator  Output......);    }}

JsonOutputGenerator. java

 package com.spring.output.impl;import com.spring.output.IOutputGenerator;public class JsonOutputGenerator implements IOutputGenerator {    public void generateOutput() {   System.out.println(Creating JsonOutputGenerator output.......);    }}

IOutputGenerator. java

package com.spring.output;public interface IOutputGenerator {      public void generateOutput();}

 

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.