spring普通属性注入
程序员文章站
2022-05-24 10:45:36
...
package com.yx.spring.bean;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Bean1 {
private String strValue;
private int intValue;
private List listValue;
private Set setValue;
private String[] arrayValue;
private Map mapValue;
public String[] getArrayValue() {
return arrayValue;
}
public void setArrayValue(String[] arrayValue) {
this.arrayValue = arrayValue;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public List getListValue() {
return listValue;
}
public void setListValue(List listValue) {
this.listValue = listValue;
}
public Map getMapValue() {
return mapValue;
}
public void setMapValue(Map mapValue) {
this.mapValue = mapValue;
}
public Set getSetValue() {
return setValue;
}
public void setSetValue(Set setValue) {
this.setValue = setValue;
}
public String getStrValue() {
return strValue;
}
public void setStrValue(String strValue) {
this.strValue = strValue;
}
}
<?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.0.xsd"> <bean id="bean1" class="com.yx.spring.bean.Bean1"> <property name="strValue" value="str123"></property> <property name="intValue"> <value>123</value> </property> <property name="listValue"> <list> <value>list1</value> <value>list2</value> </list> </property> <property name="setValue"> <set> <value>set1</value> <value>set2</value> </set> </property> <property name="arrayValue"> <list> <value>array1</value> <value>array2</value> </list> </property> <property name="mapValue"> <map> <entry key="k1" value="v1" /> <entry key="k2" value="v2" /> </map> </property> </bean> </beans>
package com.yx.spring.test;
import java.util.Arrays;
import junit.framework.TestCase;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yx.spring.bean.Bean1;
public class SpringTest extends TestCase {
private BeanFactory factory;
protected void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext("applicationContext.xml");
}
public void testInjection1() {
Bean1 bean1 = (Bean1)factory.getBean("bean1");
System.out.println("bean1.strValue=" + bean1.getStrValue());
System.out.println("bean1.intValue=" + bean1.getIntValue());
System.out.println("bean1.listValue=" + bean1.getListValue());
System.out.println("bean1.setValue=" + bean1.getSetValue());
System.out.println("bean1.arrayValue=" + Arrays.toString(bean1.getArrayValue()));
System.out.println("bean1.mapValue=" + bean1.getMapValue());
}
}
上一篇: validate 动态返回错误信息
下一篇: 二、Spring属性注入