Spring典型例子 ,让你理解什么是spring

Spring的依赖注入有三种实现方式,常用是setter和construct方式。对于像数组和集合对象注入,注入前必须填充一些对象到集合中,然后再把集合注入到Bean,采用setter方式来完成。
通过一个例子来说明。

   导入Spring所需的包:commons-logging.jar,spring.jar 。(日志包和Spring包)
包下载地址:
http://www.blogjava.net/Files/ducklyl/Spring.rar

(1)创建ObjectSet类
package com.set;

public class ObjectSet {
private String name;

public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}
public String toString()
{
   return name;
}
}

(2)创建SomeBean类
package com.set;

import java.util.List;
import java.util.Map;

public class SomeBean {
private String[] someStrArray;
private ObjectSet[] someObjArray;
private List someList;
private Map someMap;
public SomeBean()
{}

public List getSomeList() {
   return someList;
}

public void setSomeList(List someList) {
   this.someList = someList;
}

public Map getSomeMap() {
   return someMap;
}

public void setSomeMap(Map someMap) {
   this.someMap = someMap;
}

public ObjectSet[] getSomeObjArray() {
   return someObjArray;
}

public void setSomeObjArray(ObjectSet[] someObjArray) {
   this.someObjArray = someObjArray;
}
public String[] getSomeStrArray() {
   return someStrArray;
}
public void setSomeStrArray(String[] someStrArray) {
   this.someStrArray = someStrArray;
}
}

(3)在类路径下,创建set-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="first_object" class="com.set.ObjectSet" >
<property name="name">
     <value>ducklyl</value>
</property>
</bean>
<bean id="second_object" class="com.set.ObjectSet" >
<property name="name">
     <value>wangsp</value>
</property>
</bean>

<bean id="someBean" class="com.set.SomeBean" >
<property name="someStrArray">
     <list>
      <value>Hello</value>
      <value>Welcome</value>
     </list>
</property>
<property name="someObjArray">
   <list>
    <ref bean="first_object"/>
    <ref bean="second_object"/>
   </list>
</property>
<property name="someList">
   <list>
    <value>ListTest</value>
    <ref bean="first_object"/>
    <ref bean="second_object"/>
   </list>
</property>
<property name="someMap">
   <map>
    <entry key="first">
     <value>Hello,ducklyl!</value>
    </entry>
    <entry key="second">
     <ref bean="first_object"/>
    </entry>
   </map>
</property>
</bean>
</beans>

(4)创建测试类SpringTest
package com.set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import java.util.*;

public class SpringTest {
public static void main(String[] args)
{
//读取配置文件
ApplicationContext context=new FileSystemXmlApplicationContext("set-config.xml");
//获取id="setBean"对象
SomeBean someBean=(SomeBean)context.getBean("someBean");

     //获取someStrArray,someObjArray
String[] strs=someBean.getSomeStrArray();
ObjectSet[] some=someBean.getSomeObjArray();

for(int i=0;i<strs.length;i++)
System.out.println(strs[i]+","+some[i]);

     //获取someList
List someList=(List)someBean.getSomeList();
for(int i=0;i<someList.size();i++)
   System.out.println(someList.get(i));

//获取someMap
Map someMap=someBean.getSomeMap();
System.out.println(someMap);

}
}

如果以上配置正确,运行测试程序,结果如下:

Hello,ducklyl
Welcome,wangsp
ListTest
ducklyl
wangsp
{first=Hello,ducklyl!, second=ducklyl}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值