您的位置:首页 > 编程语言 > Java开发

Spring IoC – 属性注入 – set方法和构造方法

2012-10-14 23:56 441 查看
一、建立项目

二、添加Spring的配置文件

三、建立Bean类

a) User

代码:

public class User {

private int intValue;

private char charValue;

private String strValue;

private double doubleValue;

private boolean booleanValue;

private Set<String> setValue;

private List<String> listValue;

private Properties props;

private Map<String, String> mapValue;

private Date dateValue;

private Student student;

private String[] strArr;

public User() {

super();

// TODO Auto-generated constructor stub

}

public User(String strValue) {

super();

this.strValue = strValue;

}

public int getIntValue() {

return intValue;

}

public void setIntValue(int intValue) {

this.intValue = intValue;

}

public char getCharValue() {

return charValue;

}

public void setCharValue(char charValue) {

this.charValue = charValue;

}

public String getStrValue() {

return strValue;

}

public void setStrValue(String strValue) {

this.strValue = strValue;

}

public double getDoubleValue() {

return doubleValue;

}

public void setDoubleValue(double doubleValue) {

this.doubleValue = doubleValue;

}

public boolean isBooleanValue() {

return booleanValue;

}

public void setBooleanValue(boolean booleanValue) {

this.booleanValue = booleanValue;

}

public Set<String> getSetValue() {

return setValue;

}

public void setSetValue(Set<String> setValue) {

this.setValue = setValue;

}

public List<String> getListValue() {

return listValue;

}

public void setListValue(List<String> listValue) {

this.listValue = listValue;

}

public Properties getProps() {

return props;

}

public void setProps(Properties props) {

this.props = props;

}

public Map<String, String> getMapValue() {

return mapValue;

}

public void setMapValue(Map<String, String> mapValue) {

this.mapValue = mapValue;

}

public Date getDateValue() {

return dateValue;

}

public void setDateValue(Date dateValue) {

this.dateValue = dateValue;

}

public Student getStudent() {

return student;

}

public void setStudent(Student student) {

this.student = student;

}

public String[] getStrArr() {

return strArr;

}

public void setStrArr(String[] strArr) {

this.strArr = strArr;

}

}

四、配置到Spring的配置文件applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="date" class="java.util.Date"></bean>

<bean id="student" class="com.www.ioc.Student">

<!-- 构造方法的注入 -->

<constructor-arg index="0" value="userName"></constructor-arg>

<constructor-arg index="1" value="123"></constructor-arg>

</bean>

<bean id="user" class="com.www.ioc.User">

<!-- 构造方法注入 -->

<constructor-arg type="java.lang.String">

<value>strValue</value>

</constructor-arg>

<!-- int类型的注入 -->

<property name="intValue" value="123"></property>

<!-- boolean类型的注入 -->

<property name="booleanValue" value="true"></property>

<!-- char类型的注入 -->

<property name="charValue" value=""></property>

<!-- double类型的注入 -->

<property name="doubleValue" value="1.2"></property>

<!-- date类型的注入 -->

<property name="dateValue" ref="date"></property>

<!-- list集合的注入 -->

<property name="listValue">

<list>

<value>list1</value>

<value>list2</value>

<value>list3</value>

</list>

</property>

<!-- properties属性文件的注入 -->

<property name="props">

<props>

<prop key="key">propsValue</prop>

</props>

</property>

<!-- map的注入 -->

<property name="mapValue">

<map>

<entry key="key1" value="key1Value"></entry>

<entry key="key2" value="ley2Value"></entry>

</map>

</property>

<!-- set集合的注入 -->

<property name="setValue">

<set>

<value>set1</value>

<value>set2</value>

</set>

</property>

<!-- Bean的注入 -->

<property name="student">

<ref bean="student" />

</property>

<!-- 数组注入 -->

<property name="strArr">

<list>

<value>strArr1</value>

<value>strArr2</value>

<value>strArr3</value>

</list>

</property>

</bean>

</beans>

五、编写测试类:

代码:

public class UserTest {

public static void main(String[] args) {

BeanFactory factory = new ClassPathXmlApplicationContext(

"applicationContext.xml");

User user = (User) factory.getBean("user");

System.out.println("user.getCharValue():" + user.getCharValue());

System.out.println("user.getDoubleValue():" + user.getDoubleValue());

System.out.println("user.getIntValue():" + user.getIntValue());

System.out.println("user.getStrValue():" + user.getStrValue());

System.out.println("user.getDateValue():" + user.getDateValue());

System.out.println("user.getListValue().get(1):"

+ user.getListValue().get(1));

System.out.println("user.getMapValue().get('key1'):"

+ user.getMapValue().get("key1"));

System.out.println("user.getProps():" + user.getProps());

System.out.println("user.getSetValue():" + user.getSetValue());

System.out.println("user.getStudent():" + user.getStudent());

System.out.println("user.getStrArr():" + user.getStrArr()[1]);

}

}

附件:http://down.51cto.com/data/2361586
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring
相关文章推荐