go4it

just do it

Spring视频学习(三)注入bean的属性

注入基本类型,集合,类属性:

<!-- ref方式注入属性 -->
<bean id="personDao" class="com.persia.PersonDaoBean"></bean>
<bean id="personService4" class="com.persia.PersonServiceBean">
  <property name="personDao" ref="personDao"></property>
</bean>

<!-- 内部bean方式注入 -->
<bean id="personService5" class="com.persia.PersonServiceBean">
  <property name="personDao">
     <bean class="com.persia.PersonDaoBean"></bean>
  </property>
  <property name="name" value="persia"></property>
  <property name="age" value="21"></property>
  
  <property name="sets">
    <!-- Set集合的注入 -->
     <set>
       <value>第一个</value>
       <value>第二个</value>
       <value>第三个</value>
     </set>
  </property>
  
  <property name="lists">
    <!-- List集合的注入 -->
    <list>
        <value>第一个l</value>
       <value>第二个l</value>
       <value>第三个l</value>
    </list>
    
  </property>
  
  <property name="properties">
    <props>
      <prop key="key1">value1</prop>
      <prop key="key2">value2</prop>
      <prop key="key3">value3</prop>
    </props>
  </property>
  
  <property name="map">
   <!-- Map集合的注入 --> 
   <map>
      <entry key="key1" value="value-1"></entry>
      <entry key="key2" value="value-2"></entry>
      <entry key="key3" value="value-3"></entry>
   </map>
  </property>
</bean>
注意提供setter方法:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PersonServiceBean implements IService {

        private IDaoBean personDao;
	private String name;
	private int age;
	private Set<String> sets=new HashSet<String>();
	private List<String> lists=new ArrayList<String>();
	private Properties properties=new Properties();
	private Map<String,String> map=new HashMap<String,String>();
	
	
	
	public PersonServiceBean(String name, IDaoBean personDao) {
		
		this.name = name;
		this.personDao = personDao;
	}

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public List<String> getLists() {
		return lists;
	}

	public void setLists(List<String> lists) {
		this.lists = lists;
	}

	public Set<String> getSets() {
		return sets;
	}

	public void setSets(Set<String> sets) {
		this.sets = sets;
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public IDaoBean getPersonDao() {
		return personDao;
	}

	public void setPersonDao(IDaoBean personDao) {
		this.personDao = personDao;
	}

	public PersonServiceBean(){
		System.out.println("bean被实例化");
	}
	
	public void init(){
		System.out.println("资源操作");
	}
	public void save(){
		System.out.println("hello");
		personDao.add();
		System.out.println("被注入的name的值:"+this.name);
	}
	
	public void destory(){
		System.out.println("资源关闭操作");
	}
}

posted on 2009-07-20 13:06  cxccbv  阅读(336)  评论(0编辑  收藏  举报

导航