Spring review--常用属性String、int、list、set、Map的注入

来源:互联网 发布:mac版本的greenvpn 编辑:程序博客网 时间:2024/05/29 18:48

依赖注入

  组件不做定位查询,组件依赖关系、装配全部交给容器全权负责。容器会把符合依赖关系的对象通过Java属性或者构造函数传递给所需要的对象。


常用属性String、int、list、set、Map的注入方法如下:

applicationContext.xml

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:tx="http://www.springframework.org/schema/tx"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean id="bean1" class="com.bjpowernode.spring.Bean1"><property name="strValue" value="hello"/> <property name="intValue" value="123"/><property name="listH" ><list><value>list1</value><value>list2</value></list></property>  <property name="setValue"><set><value>set1</value><value>set2</value></set></property><property name="mapValueMap" ><map><entry key="k1" value="v1"/><entry key="k2" value="v2"/></map></property></bean></beans></span>



Bean1.java

<span style="font-size:14px;">package com.bjpowernode.spring;import java.util.List;import java.util.Map;import java.util.Set;public class Bean1 {private String strValue;private int intValue;private List listH;private Set setValue;private String[] arrayValue;private Map mapValueMap;public String getStrValue() {return strValue;}public void setStrValue(String strValue) {this.strValue = strValue;}public int getIntValue() {return intValue;}public void setIntValue(int intValue) {this.intValue = intValue;}public List getListH() {return listH;}public void setListH(List listH) {this.listH = listH;}public Set getSetValue() {return setValue;}public void setSetValue(Set setValue) {this.setValue = setValue;}public String[] getArrayValue() {return arrayValue;}public void setArrayValue(String[] arrayValue) {this.arrayValue = arrayValue;}public Map getMapValueMap() {return mapValueMap;}public void setMapValueMap(Map mapValueMap) {this.mapValueMap = mapValueMap;}}</span>



InjectitonTest.java

<span style="font-size:14px;">package Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bjpowernode.spring.Bean1;import junit.framework.TestCase;public class InjectionTest extends TestCase {private BeanFactory factory;@Overrideprotected void setUp() throws Exception { factory =new ClassPathXmlApplicationContext("applicationContext.xml");//获取xml文件里面的bean信息}@Overrideprotected void tearDown() throws Exception {// TODO Auto-generated method stubsuper.tearDown();}public void testInjecton1(){Bean1 baBean1=(Bean1)factory.getBean("bean1");System.out.println("bean1.strValue="+baBean1.getStrValue());System.out.println("bean1.getIntValue="+baBean1.getIntValue());System.out.println("bean1.getListValue="+baBean1.getListH());System.out.println("bean1.getSetValue="+baBean1.getSetValue());System.out.println("bean1.getArrayValue="+baBean1.getArrayValue());System.out.println("bean1.getMapValueMap="+baBean1.getMapValueMap());}}</span>



0 0
原创粉丝点击