Loading

Spring xml中进行autowired的方式

可以在xml文件中进行autowired;

xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-4.1.xsd">
    <bean id="name" class="java.lang.String">
        <constructor-arg>
            <value>stringBean</value>
        </constructor-arg>
    </bean>
    <bean id="name2" class="java.lang.String">
        <constructor-arg>
            <value>stringBean2</value>
        </constructor-arg>
    </bean>
    <bean id="instrument" class="com.stono.sprtest.Harmonica"></bean>
    <!-- byName注入,可以将Instrument注入,但是无法注入java.lang.String对象 -->
    <bean id="singer" class="com.stono.sprtest.Singer" autowire="byName"></bean>
    <!-- byType注入,可以将Instrument注入,但是无法注入java.lang.String对象 -->
    <bean id="singer2" class="com.stono.sprtest.Singer" autowire="byType"></bean>
    <!-- 加入这个之后singer2将无法正常注入,有两个bean符合byType条件; 必须增加autowire-candidate=false条件 -->
    <bean id="cymbal" class="com.stono.sprtest.Cymbal" autowire-candidate="false"></bean>
    <!-- 使用constructor注入,可以实现java.lang.String类型的注入,而且String名称可以不是name -->
    <bean id="singer3" class="com.stono.sprtest.Singer" autowire="constructor"></bean>
    <!-- 使用default注入,没有实现bean的注入 -->
    <bean id="singer4" class="com.stono.sprtest.Singer" autowire="default"></bean>
</beans>

 

main:

package com.stono.sprtest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppBeans4 {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("appbeans4.xml");
        Singer singer = (Singer) context.getBean("singer");
        System.out.println(singer);
        InstrumentI instrument = (InstrumentI) context.getBean("instrument");
        System.out.println(instrument);
        Singer singer2 = (Singer) context.getBean("singer2");
        System.out.println(singer2);
        String s = (String) context.getBean("name");
        System.out.println(s);
        Singer singer3 = (Singer) context.getBean("singer3");
        System.out.println(singer3);
        Singer singer4 = (Singer) context.getBean("singer4");
        System.out.println(singer4);
        
    }
}

 

bean:

package com.stono.sprtest;

public class Singer {
    private InstrumentI instrument;
    private String name;
    public Singer() {
    }
    public Singer(InstrumentI instrument, String name) {
        this.instrument = instrument;
        this.name = name;
    }
    public InstrumentI getInstrument() {
        return instrument;
    }
    public String getName() {
        return name;
    }
    public void setInstrument(InstrumentI instrument) {
        this.instrument = instrument;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Singer [instrument=" + instrument + ", name=" + name + "]";
    }
}

 

posted @ 2015-09-28 09:52  stono  阅读(793)  评论(0编辑  收藏  举报