O(∩_∩)O哈哈~过新年了,我的Hibernate学习也至此告一段落了,Hibernate是冬眠,也刚好过完冬天了,过春节了,我该学习Spring了。Spring I'm coming...
首先要有一个实体。
package cn.ineeke.spring;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MyBean {
private String strValue;
private int intValue;
private List listValue;
private Set setValue;
private String[] arrayValue;
private Map mapValue;
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 getListValue() {
return listValue;
}
public void setListValue(List listValue) {
this.listValue = listValue;
}
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 getMapValue() {
return mapValue;
}
public void setMapValue(Map mapValue) {
this.mapValue = mapValue;
}
}
有了实体就可以开始配置Spring了。在crc下创建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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="myBean" class="cn.ineeke.spring.MyBean">
<property name="strValue" value="Hello" />
<property name="intValue">
<value>21</value>
</property>
<property name="listValue">
<list>
<value>neeke1</value>
<value>neeke2</value>
</list>
</property>
<property name="setValue">
<set>
<value>ineeke1</value>
<value>ineeke2</value>
</set>
</property>
<property name="arrayValue">
<list>
<value>array1</value>
<value>array2</value>
</list>
</property>
<property name="mapValue">
<map>
<entry key="ek1" value="ek1"/>
<entry key="ek2" value="ek2"/>
</map>
</property>
</bean>
</beans>
现在只仅仅是配置好了,下面看看如何new出这个实体。
package cn.ineeke.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean bean = (MyBean)factory.getBean("myBean");
System.out.println(bean.getIntValue());
}
}
这个主要是为了了解如何使用Spring对实体的属性进行注入,其重点又在于不同数据类型的属性如何注入以及如何使用Spring获得需要的实体。