spring基础应用(2)——依赖注入

依赖注入

当spring中一个对象A需要依赖另一个对象B的时候,我们需要在A里面引入一个B,同时需要给B注入一个对象,在这里我们先说的是手动装配,跟后面的自动装配autowired暂时没有关系。
通常手动装配一个对象我们有两种方法:

  • 构造方法注入
  • set方法注入

接下来我们依次来看,先看构造方法注入

构造方法注入

示例1:constructor-arg里面是ref

public class BeanOne {
    public BeanOne(BeanTwo beanTwo){
    }
}
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="beanOne" class="com.ali.testspring.BeanOne">
        <constructor-arg ref="beanTwo"></constructor-arg>
    <!-- inject any dependencies required by this locator bean -->
    </bean>
    <bean id="beanTwo" class="com.ali.testspring.BeanTwo">
        <!-- inject any dependencies required by this locator bean -->
    </bean>

</beans>

示例2:
当需要给基本数据类型和字符串注入的时候

public class ExampleBean {

    // Number of years to calculate the Ultimate Answer
    private final int years;

    // The Answer to Life, the Universe, and Everything
    private final String ultimateAnswer;

    public ExampleBean(int years, String ultimateAnswer) {
        this.years = years;
        this.ultimateAnswer = ultimateAnswer;
    }
}
//可以用type和value
<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg type="int" value="7500000"/>
    <constructor-arg type="java.lang.String" value="42"/>
</bean>

//可以用index和value
<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg index="0" value="7500000"/>
    <constructor-arg index="1" value="42"/>
</bean>

//也可以用name和value
<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg name="years" value="7500000"/>
    <constructor-arg name="ultimateAnswer" value="42"/>
</bean>

set方法注入

示例1:给普通类里面的属性赋值

<bean id="exampleBean" class="examples.ExampleBean">
    <!-- setter injection using the nested ref element -->
    <property name="beanOne">
        <ref bean="anotherExampleBean"/>
    </property>

    <!-- setter injection using the neater ref attribute -->
    <property name="beanTwo" ref="yetAnotherBean"/>
    <property name="integerProperty" value="1"/>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {

    private AnotherBean beanOne;

    private YetAnotherBean beanTwo;

    private int i;

    public void setBeanOne(AnotherBean beanOne) {
        this.beanOne = beanOne;
    }

    public void setBeanTwo(YetAnotherBean beanTwo) {
        this.beanTwo = beanTwo;
    }

    public void setIntegerProperty(int i) {
        this.i = i;
    }
}

示例2:给内部类里面的属性赋值

<bean id="outer" class="com.ali.testspring.Outer">
    <!-- instead of using a reference to a target bean, simply define the target bean inline -->
    <property name="target">
         <bean class="com.ali.testspring.Outer.Person"> <!-- this is the inner bean -->
             <property name="name" value="Fiona Apple"/>
             <property name="age" value="25"/>
         </bean>
    </property>
</bean>
public class Outer {
    private Person target;

    public void setTarget(Person target) {
        this.target = target;
    }

    public Person getTarget() {
        return target;
    }

    static class  Person{
        private String name;
        private String age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }
    }
}

示例3:为list、set、map、properties赋值

 <bean id="testMap" class="com.ali.testspring.TestMap">
        <!-- results in a setAdminEmails(java.util.Properties) call -->
        <property name="properties1">
            <props>
                <prop key="administrator">administrator@example.org</prop>
                <prop key="support">support@example.org</prop>
                <prop key="development">development@example.org</prop>
            </props>
        </property>
        <!-- results in a setSomeList(java.util.List) call -->
        <property name="list1">
            <list>
                <value>a list element followed by a reference</value>
                <ref bean="dao1" />
            </list>
        </property>
        <!-- results in a setSomeMap(java.util.Map) call -->
        <property name="map1">
            <map>
                <entry key="an entry" value="just some string"/>
                <entry key="a ref" value-ref="dao1"/>
            </map>
        </property>
        <!-- results in a setSomeSet(java.util.Set) call -->
        <property name="set1">
            <set>
                <value>just some string</value>
                <ref bean="dao1" />
            </set>
        </property>
    </bean>

package com.ali.testspring;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class TestMap {

    public List list1;
    public Set set1;
    public Map map1;
    public Properties properties1;

    public List getList1() {
        return list1;
    }

    public void setList1(List list1) {
        this.list1 = list1;
    }

    public Set getSet1() {
        return set1;
    }

    public void setSet1(Set set1) {
        this.set1 = set1;
    }

    public Map getMap1() {
        return map1;
    }

    public void setMap1(Map map1) {
        this.map1 = map1;
    }

    public Properties getProperties1() {
        return properties1;
    }

    public void setProperties1(Properties properties1) {
        this.properties1 = properties1;
    }
}

package com.ali.testspring;

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

public class Test {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
        TestMap testMap=(TestMap)context.getBean("testMap");
        System.out.println(testMap.getList1().size());   //2
        System.out.println(testMap.getMap1().size());    //2
        System.out.println(testMap.getProperties1().size());    //3
        System.out.println(testMap.getSet1().size());   //2

    }
}

posted @ 2022-01-05 19:27  提莫_队长  阅读(28)  评论(0编辑  收藏  举报