读书人

spring2.5学习札记(三):属性赋值

发布时间: 2012-10-26 10:30:59 作者: rapoo

spring2.5学习笔记(三):属性赋值
beans.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"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd           "><!-- IOC控制反转的原理演示 --><bean id="persondao" ><!-- 这里运用java的反射机制,将生成的bean组件(persondao)中的方法,映射给ipersondao,也就是 在PersonServiceBean中只要建一个persondao相关接口的引用就能调用到实现了这个接口的类中的方法--><!-- 依赖属性值的注入 --><property name="ipersondao" ref="persondao"></property><!-- 普通类型值的注入 --><property name="name" value="hhp"></property><property name="id" value="88"></property><!-- 四种集合类型变量属性值的注入 --><property name="sets"><set><value>第一个set值</value><value>第二个set值</value><value>第三个set值</value></set></property><property name="lists"><list><value>第一个list值</value><value>第二个list值</value><value>第三个list值</value></list></property><property name="properties"><props><prop key="key1">第一个properties的值</prop><prop key="key2">第二个properties的值</prop><prop key="key3">第三个properties的值</prop></props></property><property name="maps"><map><entry key="key-1" value="第一个map值"></entry><entry key="key-2" value="第二个map值"></entry><entry key="key-3" value="第三个map值"></entry></map></property>                                         <!-- 以下是通过构造器给属性赋值 --><constructor-arg index="0" type="cn.itcast.dao.impl.IPersonDao" ref="persondao"></constructor-arg><constructor-arg index="1" value="这是通过构造器注入的形式,为属性赋值"></constructor-arg></bean><!-- 以下使用java的@Resource注解注入的形式给 属性赋值 --><context:annotation-config></context:annotation-config><bean id="iPersonDao" ></bean></beans>


cn.itcast.dao.impl包下的java文件
package cn.itcast.dao.impl;public interface IPersonDao {public abstract void add();}

package cn.itcast.dao.impl;public class PersonDaoBean implements IPersonDao {public void add(){System.out.println("执行PersonDaoBean类中的add方法");}}


cn.itcast.service包下的java文件
package cn.itcast.service;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public interface IPersonService {public Set<String> getSets();public List<String> getLists();public Properties getProperties();public Map getMaps();public abstract void save();}


cn.itcast.service.impl包下的java文件
package cn.itcast.service.impl;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;import cn.itcast.dao.impl.IPersonDao;import cn.itcast.dao.impl.PersonDaoBean;import cn.itcast.service.IPersonService;public class PersonServiceBean implements IPersonService {/* (non-Javadoc) * @see cn.itcast.service.impl.IPersonService#save() *///此方法在配置文件bean标签中用init-method属性调用private IPersonDao ipersondao ;private String name;private Integer id;private Set<String> sets = new HashSet<String>();private List<String> lists = new ArrayList<String>();private Properties properties = new Properties();private Map maps = new HashMap();private IPersonDao personDao;private String str;public PersonServiceBean(IPersonDao personDao, String str) {super();this.personDao = personDao;this.str = str;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public Set<String> getSets() {return sets;}public void setSets(Set<String> sets) {this.sets = sets;}public List<String> getLists() {return lists;}public void setLists(List<String> lists) {this.lists = lists;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}public Map getMaps() {return maps;}public void setMaps(Map maps) {this.maps = maps;}public IPersonDao getIpersondao() {return ipersondao;}public void setIpersondao(IPersonDao ipersondao) {this.ipersondao = ipersondao;}public void init(){System.out.println("初始化");}public PersonServiceBean(){System.out.println("我被实例化了");}public void save(){System.out.println("这是PersonServiceBean类中的save方法");System.out.println("id="+id+"    name="+name);System.out.println("=========以下是通过构造器注入的方式向属性赋值");personDao.add();System.out.println("这是通过构造器注入的属性值"+str);System.out.println("=========以上是通过构造器注入的方式向属性赋值");//ipersondao.add();}public void destroy(){System.out.println("关闭打开的资源");}}

package cn.itcast.service.impl;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;import org.springframework.beans.factory.annotation.Autowired;import cn.itcast.dao.impl.IPersonDao;import cn.itcast.service.IPersonService;public class PersonServiceBean2 implements IPersonService {//@ItcastResource //@Resource @Autowired private IPersonDao iPersonDao;public List<String> getLists() {// TODO Auto-generated method stubreturn null;}public Map getMaps() {// TODO Auto-generated method stubreturn null;}public Properties getProperties() {// TODO Auto-generated method stubreturn null;}public Set<String> getSets() {// TODO Auto-generated method stubreturn null;}public IPersonDao getIPersonDao() {return iPersonDao;} public void setIPersonDao(IPersonDao personDao) {iPersonDao = personDao;}public void save() {iPersonDao.add();}}


测试文件
package junit.test;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.itcast.service.IPersonService;public class SpringTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Test public void instanceSpring(){ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");IPersonService personservice5 = (IPersonService)ctx.getBean("personservice5");System.out.println("============set==============");for(String value : personservice5.getSets()){System.out.println("注入的set值:   "+value);}System.out.println("============list==============");for(String value : personservice5.getLists()){System.out.println("注入的list值:   "+value);}System.out.println("============properties==============");for(Object key : personservice5.getProperties().keySet()){System.out.println("key:   "+personservice5.getProperties().getProperty((String)key));}System.out.println("============map==============");for(Object key : personservice5.getMaps().keySet()){System.out.println("map:   "+personservice5.getMaps().get(key));}personservice5.save();}}

读书人网 >软件架构设计

热点推荐