spring注入方式之set方式注入

来源:互联网 发布:gta5ol骷髅捏脸数据 编辑:程序博客网 时间:2024/05/17 23:02
利用set方式注入,必须要存在对应的属性的set方法
一.Domain:
1.Person
public class Person {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

2.Student
public class Student {private int stu_id=3;private String stu_name="stu_wangwu";public int getStu_id() {return stu_id;}public void setStu_id(int stu_id) {this.stu_id = stu_id;}public String getStu_name() {return stu_name;}public void setStu_name(String stu_name) {this.stu_name = stu_name;}}




二.Dao
public class PersonDao {private int id;private String name;private Person person;private Student stu;private List list;private Object[] obj;private Set set;private Map map;private Properties pro;private Properties proNull;public Properties getProNull() {return proNull;}public void setProNull(Properties proNull) {this.proNull = proNull;}public Properties getPro() {return pro;}public void setPro(Properties pro) {this.pro = pro;}public Map getMap() {return map;}public void setMap(Map map) {this.map = map;}public Set getSet() {return set;}public void setSet(Set set) {this.set = set;}public Object[] getObj() {return obj;}public void setObj(Object[] obj) {this.obj = obj;}public List getList() {return list;}public void setList(List list) {this.list = list;}public Student getStu() {return stu;}public void setStu(Student stu) {this.stu = stu;}public Person getPerson() {return person;}public void setPerson(Person person) {this.person = person;}public int getId() {return id;}public void setId(int id) {System.out.println("setId");this.id = id;}public String getName() {return name;}public void setName(String name) {System.out.println("setName");this.name = name;}public void add() {System.out.println("id--->" + id);System.out.println("name--->" + name);}}


三.applicationContext.xml
<?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"><!-- 声明Person对象,可以提供给任意类来进行注入 --><bean id="person" class="com.xxc.iocc.setter.domain.Person"><property name="id" value="2"/><property name="name" value="lisi"/></bean><bean id="personDao" class="com.xxc.iocc.setter.dao.PersonDao"><property name="id" value="1"/><property name="name" value="zhangsan"/><!-- 注入引用数据类型 --><property name="person" ref="person"></property><!-- 内部bean注入 ,这种注入方式别的类中是不能用的--><property name="stu"><bean id="stu" class="com.xxc.iocc.setter.domain.Student"/></property><!-- list注入 --><property name="list"><list><value>111</value><ref bean="person"/></list></property><!-- 数组注入,也是用的list --><property name="obj"><list><value>object_1</value><ref bean="person"/></list></property><!-- set注入 --><property name="set"><set><value>set01</value><ref bean="person"/></set></property><!-- map注入 --><property name="map"><map><entry key="mapKey01"><value>mapValue01</value></entry><entry key="mapKey02"><ref bean="person"/></entry></map></property><!-- Properties资源文件注入 --><property name="pro"><props><prop key="prop_key01">prop_value01</prop><prop key="prop_key02">prop_value02</prop></props></property><!-- 装配null --><property name="proNull"><null/></property></bean></beans>

测试类:
public class Test {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/iocc/setter/applicationContext.xml");PersonDao personDao = (PersonDao)ac.getBean("personDao");//personDao.add();//System.out.println(personDao.getPerson().getName());//System.out.println(personDao.getStu().getStu_name());//System.out.println(((Person)personDao.getList().get(1)).getName());//System.out.println(personDao.getObj()[0]);//Iterator it = personDao.getSet().iterator();//while(it.hasNext()){//System.out.println(it.next());//}//System.out.println(personDao.getMap().get("mapKey01"));//System.out.println(personDao.getPro().get("prop_key01"));System.out.println(personDao.getProNull());}}



原创粉丝点击