Spring 装配基本属性和集合

Spring 装配基本属性的原理
	String 类型
	<property name="name" value="wtljiayou"></property>
	Integer类型
	<property name="id" value="88"></property>

Spring装配各种集合类型的属性
<?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" >
	<!-- String 类型-->
	<property name="name" value="wtljiayou"></property>
	<!--Integer类型-->
	<property name="id" value="88"></property>
		<!-- 注入的集合 -->
	application.xml
		<property name="sets">
			<set>
				<value>第一个</value>
				<value>第二个</value>
				<value>第三个</value>
				<value>第四个</value>
			</set>
		</property>
		<!-- 注入list集合 -->
		<property name="list">
			<list>
				<value>第一个</value>
				<value>第三个</value>
				<value>第二个</value>
				<value>第一个</value>
			</list>
		</property>
		
		<!-- 注入properties -->
		<property name="properties">
			<props>
				<prop key="key1">可以1</prop>
				<prop key="key2">可以2</prop>
				<prop key="key3">可以3</prop>
			</props>
		</property>
		<!-- 注入Map -->
		<property name="maps">
			<map>
				<entry key="key-1" value="value-1"></entry>
				<entry key="key-2" value="value-2"></entry>
				<entry key="key-3" value="value-3"></entry>
			</map>
		</property>
</beans>

	//这里本身只需要set方法就行,但是为了测试时的调用,就写了get方法
	PersonServiceBean.java
	package com.service.impl;
	public class PersonServiceBean implements PersonService {
		private  PersonDao personDao;
		private  String    name;
		//注入集合
		private Set<String> sets =new HashSet<String>();
		//注入List集合
		private List<String> list = new ArrayList<String>();
		//注入properties
		private Properties properties =new Properties();
		//注入Map<String,String>
		private Map<String ,String>  maps=new HashMap<String, String>();
		
		
		public void setName(String name) {
			this.name = name;
		}
		public void setPersonDao(PersonDao personDao) {
			this.personDao = personDao;
		}
		public Map<String, String> getMaps() {
			return maps;
		}
		public void setMaps(Map<String, String> maps) {
			this.maps = maps;
		}
		public Properties getProperties() {
			return properties;
		}
		public void setProperties(Properties properties) {
			this.properties = properties;
		}
		public List<String> getList() {
			return list;
		}
		public void setList(List<String> list) {
			this.list = list;
		}
		public Set<String> getSets() {
			return sets;
		}
		public void setSets(Set<String> sets) {
			this.sets = sets;
		}
		public void save(){
			personDao.add();
			System.out.println(name);
		}
	}

	PersonService.java
		package com.service;

		import java.util.List;
		import java.util.Map;
		import java.util.Properties;
		import java.util.Set;
		public interface PersonService {
			public abstract void save();
			public abstract Set<String> getSets();
			public abstract List<String> getList();
			public abstract Properties getProperties();
			public abstract Map<String, String> getMaps();
		}

		PersonDao.java
		package com.dao;
		public interface PersonDao {
			public abstract void add();
		}

	PersonDaoBean.java
		package com.dao.impl;
		import com.dao.PersonDao;
		public class PersonDaoBean implements PersonDao {
			public void add(){
				System.out.println("z执行PersonDaoBean中的方法");
			}
		}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值