码迷,mamicode.com
首页 > 编程语言 > 详细

Spring中引用不同xml中的bean

时间:2015-07-07 14:47:56      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

想要在Spring-Common.xml中引用 Spring-Output.xml中定义的bean 需要使用<ref bean="XXX"/>标签,如果是同一个xml文件中引用则使用<ref local="XXX"/>标签。

 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

<?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>

在调用的上下文方法的时候这样写:

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

版权声明:本文为博主原创文章,转载请注明本博客地址!

Spring中引用不同xml中的bean

标签:

原文地址:http://blog.csdn.net/jkxqj/article/details/46788489

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!