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" class="cn.itcast.dao.impl.PersonDaoBean">
</bean>
<bean id="personservice5" class="cn.itcast.service.impl.PersonServiceBean" >
<!-- 这里运用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" class="cn.itcast.dao.impl.PersonDaoBean">
</bean>
<bean id="personservice6" class="cn.itcast.service.impl.PersonServiceBean2" >
</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 stub
return null;
}

public Map getMaps() {
// TODO Auto-generated method stub
return null;
}

public Properties getProperties() {
// TODO Auto-generated method stub
return null;
}

public Set<String> getSets() {
// TODO Auto-generated method stub
return 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 {

@BeforeClass
public 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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值