Spring中List Set Map 和Date注入

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对象。这种方式使用简单点,还有其他方式看自己需要。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值