欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring-DI

程序员文章站 2022-06-01 13:11:23
...

三、Spring-DI

3、DI(依赖注入 Dependence Injection):

3.1 set注入(使用set方法注入属性)

​ 前提:类的属性实现set方法:

public class TestDI {
    private String name;
    private TestBean testBean;

    public String getName() {
        return name;
    }

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

    public TestBean getTestBean() {
        return testBean;
    }

    public void setTestBean(TestBean testBean) {
        this.testBean = testBean;
    }
}

public class TestBean {

    public void test(){
        System.out.println("testBean");
    }
}

​ spring.xml配置,基本数据类型以及它的包装类和String类型都可以通过value进行注入,其他引用类型的数据通过ref引用其他bean对象注入到属性中,注意属性名要和property中的name对应:

<bean id="testDI" class="com.xxx.TestDI">
	<property name="name" value="I'm Test"/>
	<property name="testBean" ref="testBean"/>
</bean>
<bean id="testBean" class="com.xxx.TestBean"/>

​ 测试

public class App {
    public static void main( String[] args ){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        TestDI testDI = applicationContext.getBean(TestDI.class);
        testDI.getTestBean().test();
    }
}

​ 结果:

Spring-DI

3.2 构造注入(使用带参构造方法注入属性)

​ 前提:要有带参构造方法

public class TestConstructor {
    private String name;
    private String pwd;
    public TestBean testBean;

    public TestConstructor(String name, String pwd, TestBean testBean) {
        this.name = name;
        this.pwd = pwd;
        this.testBean = testBean;
    }
}

public class TestBean {

    public void test(){
        System.out.println("testBean");
    }
}

​ spring.xml:

​ 构造注入有三种指定方式:

​ 1、index 通过索引注入按照带参构造器中的顺序(String name (index=0) , String pwd, TestBean testBean)

​ 2、type通过类型注入也是按照构造器中的顺序注入

​ 3、name通过构造器中的参数名注入(要保持一致)

<bean id="testConstructor" class="com.xxx.TestConstructor">
    <constructor-arg index="1" value="hhhhh"/>
    <constructor-arg type="java.lang.String" value="111"/>
    <constructor-arg name="testBean" ref="testBean"/>
</bean>

<bean id="testBean" class="com.xxx.TestBean"/>

​ 测试:

public class App {
    public static void main( String[] args ){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        TestConstructor testConstructor = applicationContext.getBean(TestConstructor.class);
        System.out.println(testConstructor.name);
        System.out.println(testConstructor.pwd);
        testConstructor.testBean.test();
    }
}

​ 结果:
Spring-DI

3.3其他类型属性的注入
3.3.1 Array数组注入
public class TestArray {
    private Integer ids[];

    public void setIds(Integer[] ids) {
        this.ids = ids;
    }
    
    public Integer[] getIds() {
        return ids;
    }
    
    @Override
    public String toString() {
        return "TestArray{" +
                "ids=" + Arrays.toString(ids) +
                '}';
    }
}

spring.xml

    <bean id="testArray" class="com.xxx.TestArray">
        <property name="ids">
            <array>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </array>
        </property>
    </bean>

测试:

public class App {
    public static void main( String[] args ){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        TestArray testArray = applicationContext.getBean(TestArray.class);
        for(Integer id : testArray.getIds()){
            System.out.println(id);
        }
    }
}

结果:

Spring-DI

3.3.2 List集合注入
public class TestList {
    private List<String> stringList;

    public List<String> getStringList() {
        return stringList;
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    @Override
    public String toString() {
        return "TestList{" +
                "stringList=" + stringList +
                '}';
    }
}

​ spring.xml

    <bean id="testList" class="com.xxx.TestList">
        <property name="stringList">
            <list>
                <value>哈哈哈</value>
                <value>???</value>
                <value>哈哈哈</value>
            </list>
        </property>
    </bean>

​ 测试:

public class App {
    public static void main( String[] args ){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        TestList testList = applicationContext.getBean(TestList.class);
        System.out.println(testList);
    }
}

​ 结果:

Spring-DI

3.3.3 Set集合注入:
public class TestSet {
    private Set<String> set;

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    @Override
    public String toString() {
        return "TestSet{" +
                "set=" + set +
                '}';
    }
}

​ spring.xml

    <bean id="testSet" class="com.xxx.TestSet">
        <property name="set">
            <set>
                <value>哈哈哈</value>
                <value>???</value>
                <value>哈哈哈</value>
            </set>
        </property>
    </bean>

​ 测试:

public class App {
    public static void main( String[] args ){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        TestSet testSet = applicationContext.getBean(TestSet.class);
        System.out.println(testSet);
    }
}

​ 结果:
Spring-DI

3.3.4 Map注入
public class TestMap {
    private Map<String, Object> map;

    @Override
    public String toString() {
        return "TestMap{" +
                "map=" + map +
                '}';
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
}

​ spring.xml

    <bean id="testMap" class="com.xxx.TestMap">
        <property name="map">
            <map>
                <entry>
                    <key><value>哈哈哈</value></key>
                    <value>???</value>
                </entry>
                <entry>
                    <key><value>testBean</value></key>
                    <ref bean="testBean"></ref>
                </entry>
            </map>
        </property>
    </bean>
    <bean id="testBean" class="com.mage.test.TestBean"/>

​ 测试:

public class App {
    public static void main( String[] args ){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        TestMap testMap = applicationContext.getBean(TestMap.class);
        TestBean teseBean = (TestBean)testMap.getMap().get("testBean");
        System.out.println(testMap.getMap().get("哈哈哈"));
        teseBean.test();
    }
}

​ 结果:

Spring-DI

3.3.5 Properties注入
public class TestProperties {
    private Properties properties;

    @Override
    public String toString() {
        return "TestProperties{" +
                "properties=" + properties +
                '}';
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

​ spring.xml

    <bean id="testProperties" class="com.mage.test.TestProperties">
        <property name="properties">
            <props>
                <prop key="哈哈哈">???</prop>
                <prop key="???">哈哈哈</prop>
            </props>
        </property>
    </bean>

​ 测试:

public class App {
    public static void main( String[] args ){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        TestProperties testProperties = applicationContext.getBean(TestProperties.class);
        System.out.println(testProperties);
    }
}

​ 结果:

Spring-DI

推荐阅读