1、bean类代码:
package learnBean;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import interfaceSpring.HelloSpring;
public class BeiJingBean implements HelloSpring{
private ArrayList<String> list;
private HashSet<String> set;
private HashMap<String, String> map;
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public HashMap<String, String> getMap() {
return map;
}
public void setMap(HashMap<String, String> map) {
this.map = map;
}
public HashSet<String> getSet() {
return set;
}
public void setSet(HashSet<String> set) {
this.set = set;
}
public ArrayList<String> getList() {
return list;
}
public void setList(ArrayList<String> list) {
this.list = list;
}
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("beijingBean is saying");
}
}
2、测试代码:
public class hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
// HelloSpringService service =
// (HelloSpringService) context.getBean("HelloSpringService");
// BeiJingBean hw= (BeiJingBean) service.getHelloSpring();
BeiJingBean hw =(BeiJingBean) context.getBean("beijingBean");
hw.sayHello();
System.out.println(hw.getList().toString());
System.out.println(hw.getSet().toString());
System.out.println(hw.getMap().toString());
System.out.println(hw.getDate().toString());
}
}
3、xml配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd" />
</bean>
<bean id="beijingBean" class="learnBean.BeiJingBean">
<property name="list">
<util:list list-class="java.util.ArrayList">
<value>one</value>
<value>two</value>
<value>three</value>
</util:list>
</property>
<property name="set" >
<util:set set-class="java.util.HashSet">
<value>1</value>
<value>2</value>
<value>1</value>
</util:set>
</property>
<property name="map">
<util:map map-class="java.util.HashMap">
<entry key="one" value="1"></entry>
<entry key="two" value="2"></entry>
</util:map>
</property>
<property name="date">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2017-5-21" />
</bean>
</property>
</bean>
<bean id="HelloSpringService" class="myService.HelloSpringService">
</bean>
</beans>
4、控制台输出:
beijingBean is saying
[one, two, three]
[1, 2]
{two=2, one=1}
Sun May 21 00:00:00 CST 2017
注:
a、set、list、map:使用 util 模式和<util:list> 来注入,首先要声明 xmlns:util="http://www.springframework.org/schema/util"
b、Date(日期类)注入:声明一个dateFormat bean,在“customer” Bean,引用 “dateFormat” bean作为一个工厂bean。该工厂方法将调用SimpleDateFormat.parse()自动转换成字符串Date对象。这种方式使用简单点,还有其他方式看自己需要。