加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

浅谈spring ioc的注入方式及注入不同的数据类型

发布时间:2020-05-23 17:56:10 所属栏目:Java 来源:互联网
导读:关于Spring-IoC的简单使用参考:springioc的简单实例及bean的作用域属性解析1、通过set方法注入不同数据类型

关于Spring-IoC的简单使用参考:

spring ioc的简单实例及bean的作用域属性解析

1、通过set方法注入不同数据类型

测试类代码(set方式注入的属性一定要加set方法)

/**通过set方法注入示例*/
public class IoC_By_Set {
	/**注入Integer类型参数*/
	private Integer id;
	/**注入String类型参数*/
	private String name;
	/**注入实体Bean*/
	private User user;
	/**注入数组*/
	private Object[] array;
	/**注入List集合*/
	private List<Object> list;
	/**注入Set集合*/
	private Set<Object> set;
	/**注入Map键值对*/
	private Map<Object,Object> map;
	/**注入properties类型*/
	private Properties properties;
	/**注入空字符串*/
	private String emptyValue;
	/**注入null值*/
	private String nullValue = "";
	/**检测注入的属性是否全部正确*/
	public Boolean checkAttr() {
		if(id == null) {
			return false;
		} else {
			System.out.println("id:" + id);
		}
		System.out.println("--------------------------");
		if(name == null) {
			return false;
		} else {
			System.out.println("name:" + name);
		}
		System.out.println("--------------------------");
		if(user == null) {
			return false;
		} else {
			System.out.println("Bean:" + user.getId() + "|" + 
			          user.getUserName() + "|" + user.getPassWord());
		}
		System.out.println("--------------------------");
		if(array == null) {
			return false;
		} else {
			System.out.println("array:");
			for (Object object : array) {
				System.out.println(object.toString());
			}
		}
		System.out.println("--------------------------");
		if(list == null) {
			return false;
		} else {
			System.out.println("list:");
			for (Object object : list) {
				System.out.println(object.toString());
			}
		}
		System.out.println("--------------------------");
		if(set == null) {
			return false;
		} else {
			System.out.println("set:");
			for (Object object : set) {
				System.out.println(object.toString());
			}
		}
		System.out.println("--------------------------");
		if(map == null) {
			return false;
		} else {
			Set<Entry<Object,Object>> set = map.entrySet();
			System.out.println("map:");
			for (Entry<Object,Object> entry : set) {
				System.out.println(entry.getKey() + "|" + entry.getValue());
			}
		}
		System.out.println("--------------------------");
		if(properties == null) {
			return false;
		} else {
			Set<Entry<Object,Object>> set = properties.entrySet();
			System.out.println("properties:");
			for (Entry<Object,Object> entry : set) {
				System.out.println(entry.getKey() + "|" + entry.getValue());
			}
		}
		System.out.println("--------------------------");
		if(!"".equals(emptyValue))
		      return false;
		System.out.println("--------------------------");
		if(!(null == nullValue))
		      return false;
		System.out.println("--------------------------");
		System.out.println("全部正确!!!");
		return true;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public void setArray(Object[] array) {
		this.array = array;
	}
	public void setList(List<Object> list) {
		this.list = list;
	}
	public void setSet(Set<Object> set) {
		this.set = set;
	}
	public void setMap(Map<Object,Object> map) {
		this.map = map;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	public void setEmptyValue(String emptyValue) {
		this.emptyValue = emptyValue;
	}
	public void setNullValue(String nullValue) {
		this.nullValue = nullValue;
	}
}

applicationContext.xml配置

  <!-- set方式注入 -->
  <bean id="ioC_By_Set" class="com.bc.ioc.demo01.IoC_By_Set">
    <!-- 注入id属性 -->
    <property name="id" value="1"/>
    <!-- 使用<![CDATA[]]>标记处理XML特 殊字符 -->
    <property name="name">
      <!-- 也可以使用P&G -->
      <value><![CDATA[P&G]]></value>
    </property>
    <!-- 定义内部Bean注入 -->
    <property name="user">
      <bean class="com.bc.pojo.User">
        <property name="id" value="1"/>
        <property name="userName" value="内部Bean"/>
        <property name="passWord" value="233"/>
      </bean>
    </property>
    <!-- 注入数组类型 -->
    <property name="array">
      <array>
        <!-- 定义数组元素 -->
        <value>array01</value>
        <value>array02</value>
        <value>array03</value>
      </array>
    </property>
    <!-- 注入List类型 -->
    <property name="list">
      <list>
        <!-- 定义list中元素 -->
        <value>list01</value>
        <value>list02</value>
        <value>list03</value>
      </list>
    </property>
    <!-- 注入Set类型 -->
    <property name="set">
      <set>
        <!-- 定义set中元素 -->
        <value>set01</value>
        <value>set02</value>
        <value>set03</value>
      </set>
    </property>
    <!-- 注入Map类型 -->
    <property name="map">
      <map>
        <!-- 定义map中的键值对 -->
        <entry>
          <key>
            <value>mapKey01</value>
          </key>
          <value>mapValue01</value>
        </entry>
        <entry>
          <key>
            <value>mapKey02</value>
          </key>
          <value>mapValue02</value>
        </entry>
      </map>
    </property>
    <!-- 注入properties类型 -->
    <property name="properties">
      <props>
        <!-- 定义properties中的键值对 -->
        <prop key="propKey1">propValue1</prop>
        <prop key="propKey2">propValue2</prop>
      </props>
    </property>
    <!-- 注入空字符串 -->
    <property name="emptyValue">
      <value></value>
    </property>
    <!-- 注入null值 -->
    <property name="nullValue">
      <null/>
    </property>
  </bean>

测试代码

public class IoC_Test {
	private ApplicationContext ctx;
	@Before
	  public void load() {
		//读取applicationContext.xml配置文件
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	@Test
	  public void SetTest() {
		IoC_By_Set ioc = (IoC_By_Set) ctx.getBean("ioC_By_Set");
		ioc.checkAttr();
	}
}

控制台结果:

id:1
--------------------------
name:P&G
--------------------------
Bean:1|内部Bean|233
--------------------------
array:
array01
array02
array03
--------------------------
list:
list01
list02
list03
--------------------------
set:
set01
set02
set03
--------------------------
map:
mapKey01|mapValue01
mapKey02|mapValue02
--------------------------
properties:
propKey2|propValue2
propKey1|propValue1
--------------------------
--------------------------
--------------------------
全部正确!!!

2、通过构造方法注入各种类型属性

注意:使用JDK1.8版本请将spring相关jar包升级到4.x版本以上,否则不兼容构造方法注入

测试类代码

/** 通过构造方法注入示例 */
public class IoC_By_Constructor {
	private Integer id;
	private String name;
	private User user;
	private List<Object> list;
	public IoC_By_Constructor() {
	}
	public IoC_By_Constructor(Integer id,String name,User user,List<Object> list) {
		this.id = id;
		this.name = name;
		this.user = user;
		this.list = list;
	}
	/**检查是否注入成功*/
	public Boolean checkAttr() {
		if(id == null) {
			return false;
		} else {
			System.out.println("id:" + id);
		}
		System.out.println("----------------------------");
		if(name == null) {
			return false;
		} else {
			System.out.println("name:" + name);
		}
		System.out.println("----------------------------");
		if(user == null) {
			return false;
		} else {
			System.out.println("user:" + user.getId() + "|" + 
			          user.getUserName() + "|" + user.getPassWord());
		}
		System.out.println("----------------------------");
		if(list == null) {
			return false;
		} else {
			System.out.println("list:");
			for (Object object : list) {
				System.out.println(object.toString());
			}
		}
		System.out.println("----------------------------");
		System.out.println("全部正确!!!");
		return true;
	}
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读