Spring学习笔记-构造和Set方法注入Bean及集合和null值的注入

2022-07-04 08:48:55

目录

理论

代码及演示


理论

注入Bean:可以通过构造方法注入Bean,通过Set方法注入Bean;

集合类型有:List、Set、Map、Properties;

以及特殊的null值的注入;

通过构造方法注入Bean

<bean id="bean" class="com...Bean">
    <constructor-arg index="0" name="anotherBean" type="com...AnotherBean" ref="anotherBean />
    <constructor-arg index="1" name="stringValue" type="java.lang.String" value="stringValue1" />
</bean>

通过set方法注入Bean

<bean id="bean" class="com...Bean">
    <property name="anotherBean" ref="anotherBean" />
    <property name="string" value="setValue1"/>
</bean>

集合类型Bean注入

<bean id="bean" class="com...Bean">
    <property name="litOrSet">
        <list>     <!-- <set> -->
            <value>aaaa</value>
            <value>bbbb</value>
        </list>    <!-- </set> -->
    </property>
</bean>
<bean id="bean" class="com...Bean">
    <property name="mapOrProp">
        <map>    <!-- <props> -->
            <entry key="aaa" value="111" />
            <prop key="aaa">111</prop>
        </map>    <!-- </props> -->
    </property>
</bean>

注入null值

<bean id="bean" class="com..Bean">
    <property name="nullValue">
        <null/>
    </property>
</bean>

代码及演示

程序运行截图如下:

下面给出txt文本:

bean = Bean{anotherBean=injectbean.demo.AnotherBean@5c84624f, string='通过构造方法注入Bean', anotherBean1=injectbean.demo.AnotherBean@5c84624f, string1='通过set方法注入Bean', stringList=[第一个List值, 第二个List值, 第三个List值], anotherBeanList=[injectbean.demo.AnotherBean@5c84624f, injectbean.demo.AnotherBean@5c84624f], stringMap={key1=value1, key2=value2}, anotherBeanMap={injectbean.demo.AnotherBean@5c84624f=injectbean.demo.AnotherBean@5c84624f}, stringSet=[第一个Set值, 第二个Set值, 第三个Set值], anotherBeanSet=[injectbean.demo.AnotherBean@5c84624f], properties={heheda=dadada}}
---------华丽的分割线---------
bean.getStringList() = [第一个List值, 第二个List值, 第三个List值]
bean.getStringSet() = [第一个Set值, 第二个Set值, 第三个Set值]
bean.getStringMap() = {key1=value1, key2=value2}
bean.getAnotherBeanList() = [injectbean.demo.AnotherBean@5c84624f, injectbean.demo.AnotherBean@5c84624f]
bean.getAnotherBeanSet() = [injectbean.demo.AnotherBean@5c84624f]
bean.getAnotherBeanMap() = {injectbean.demo.AnotherBean@5c84624f=injectbean.demo.AnotherBean@5c84624f}
bean.getProperties() = {heheda=dadada}
bean.getAnotherBean2() = null

程序结构如下:

源码如下:

AnotherBean.java

package injectbean.demo;

public class AnotherBean {

}

Bean.java

package injectbean.demo;

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

public class Bean {

    @Override
    public String toString() {
        return "Bean{" +
                "anotherBean=" + anotherBean +
                ", string='" + string + '\'' +
                ", anotherBean1=" + anotherBean1 +
                ", string1='" + string1 + '\'' +
                ", stringList=" + stringList +
                ", anotherBeanList=" + anotherBeanList +
                ", stringMap=" + stringMap +
                ", anotherBeanMap=" + anotherBeanMap +
                ", stringSet=" + stringSet +
                ", anotherBeanSet=" + anotherBeanSet +
                ", properties=" + properties +
                '}';
    }

    //通过构造方法注入Bean
    private AnotherBean anotherBean;
    private String string;

    public Bean(AnotherBean anotherBean, String string) {
        this.anotherBean = anotherBean;
        this.string = string;
    }

    public AnotherBean getAnotherBean() {
        return anotherBean;
    }

    public void setAnotherBean(AnotherBean anotherBean) {
        this.anotherBean = anotherBean;
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }


    //通过set方法注入Bean
    private AnotherBean anotherBean1;
    private String string1;

    public AnotherBean getAnotherBean1() {
        return anotherBean1;
    }

    public void setAnotherBean1(AnotherBean anotherBean1) {
        this.anotherBean1 = anotherBean1;
    }

    public String getString1() {
        return string1;
    }

    public void setString1(String string1) {
        this.string1 = string1;
    }

    //集合类型Bean的注入
    private List<String> stringList;
    private List<AnotherBean> anotherBeanList;

    private Map<String, String> stringMap;
    private Map<AnotherBean, AnotherBean> anotherBeanMap;

    private Set<String> stringSet;
    private Set<AnotherBean> anotherBeanSet;

    private Properties properties;

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

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

    public List<AnotherBean> getAnotherBeanList() {
        return anotherBeanList;
    }

    public void setAnotherBeanList(List<AnotherBean> anotherBeanList) {
        this.anotherBeanList = anotherBeanList;
    }

    public Map<String, String> getStringMap() {
        return stringMap;
    }

    public void setStringMap(Map<String, String> stringMap) {
        this.stringMap = stringMap;
    }

    public Map<AnotherBean, AnotherBean> getAnotherBeanMap() {
        return anotherBeanMap;
    }

    public void setAnotherBeanMap(Map<AnotherBean, AnotherBean> anotherBeanMap) {
        this.anotherBeanMap = anotherBeanMap;
    }

    public Set<String> getStringSet() {
        return stringSet;
    }

    public void setStringSet(Set<String> stringSet) {
        this.stringSet = stringSet;
    }

    public Set<AnotherBean> getAnotherBeanSet() {
        return anotherBeanSet;
    }

    public void setAnotherBeanSet(Set<AnotherBean> anotherBeanSet) {
        this.anotherBeanSet = anotherBeanSet;
    }

    public Properties getProperties() {
        return properties;
    }

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

    //null值注入
    private AnotherBean anotherBean2;

    public AnotherBean getAnotherBean2() {
        return anotherBean2;
    }

    public void setAnotherBean2(AnotherBean anotherBean2) {
        this.anotherBean2 = anotherBean2;
    }
}

spring.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 class="injectbean.demo.AnotherBean" id="anotherBean" />
    <bean class="injectbean.demo.Bean" id="bean">
        <constructor-arg index="0" type="injectbean.demo.AnotherBean" ref="anotherBean"/>
        <constructor-arg index="1" type="java.lang.String" value="通过构造方法注入Bean" />

        <property name="anotherBean1" ref="anotherBean" />
        <property name="string1" value="通过set方法注入Bean" />

        <property name="stringList">
            <list>
                <value>第一个List值</value>
                <value>第二个List值</value>
                <value>第三个List值</value>
            </list>
        </property>
        <property name="anotherBeanList">
            <list>
                <ref bean="anotherBean" />
                <ref bean="anotherBean" />
            </list>
        </property>

        <property name="stringSet">
            <set>
                <value>第一个Set值</value>
                <value>第二个Set值</value>
                <value>第三个Set值</value>
            </set>
        </property>
        <property name="anotherBeanSet">
            <set>
                <ref bean="anotherBean" />
                <ref bean="anotherBean" />
            </set>
        </property>

        <property name="stringMap">
            <map>
                <entry key="key1" value="value1" />
                <entry key="key2" value="value2" />
            </map>
        </property>

        <property name="anotherBeanMap">
            <map>
                <entry key-ref="anotherBean" value-ref="anotherBean" />
            </map>
        </property>

        <property name="properties">
            <props>
                <prop key="heheda">dadada</prop>
            </props>
        </property>

        <property name="anotherBean2">
            <null/>
        </property>
    </bean>
</beans>

DemoApplicationTests.java

package injectbean.demo;

        import org.junit.Test;
        import org.junit.runner.RunWith;
        import org.springframework.boot.test.context.SpringBootTest;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Test
    public void contextLoads() {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        Bean bean = context.getBean("bean", Bean.class);
        System.out.println("bean = " + bean);

        System.out.println("---------华丽的分割线---------");

        System.out.println("bean.getStringList() = " + bean.getStringList());
        System.out.println("bean.getStringSet() = " + bean.getStringSet());
        System.out.println("bean.getStringMap() = " + bean.getStringMap());
        System.out.println("bean.getAnotherBeanList() = " + bean.getAnotherBeanList());
        System.out.println("bean.getAnotherBeanSet() = " + bean.getAnotherBeanSet());
        System.out.println("bean.getAnotherBeanMap() = " + bean.getAnotherBeanMap());
        System.out.println("bean.getProperties() = " + bean.getProperties());
        System.out.println("bean.getAnotherBean2() = " + bean.getAnotherBean2());
    }

}
  • 作者:IT1995
  • 原文链接:https://it1995.blog.csdn.net/article/details/88962245
    更新时间:2022-07-04 08:48:55