<三>Ioc控制反转——bean.xml注入对象、值和集合(附源码)

在<一>的基础上修改,后面会给出完整代码:

一、对象的注入

1、新建dao层的接口和实现类:

接口类:

package cn.chen.dao;

public interface PersonDao {
	public void add();
}
实现类:

package cn.chen.dao.impl;

import cn.chen.dao.PersonDao;

public class PersonDaoImpl implements PersonDao{

	@Override
	public void add() {
		// TODO Auto-generated method stub
		System.out.println("我是dao层的add()方法");
	}

}

2、在service层调用该dao层的方法,PersonServiceImpl代码如下:

package cn.chen.service.impl;

import cn.chen.dao.PersonDao;
import cn.chen.service.PersonService;

public class PersonServiceImpl implements PersonService{

	private PersonDao personDao;//设置属性。要生成setter和getter方法
	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("我是save()方法");
		personDao.add();
	}

//	public void init(){
//		System.out.println("我是初始化方法");
//	}

	public PersonDao getPersonDao() {
		return personDao;
	}

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


3、在bean.xml中注册该类,使得该类在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">
           <bean id="chen" class="cn.chen.dao.impl.PersonDaoImpl"></bean>
            <!--class表示类的路径,id表示由spring容器来创建该类的一个对象-->
          <bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" >
          	<property name="personDao" ref="chen"></property>
          </bean>
		 	
</beans>
其中,
<property name="personDao" ref="chen"></property>
这一行中,ref的值是和 <bean id="chen" class="cn.chen.dao.impl.PersonDaoImpl"></bean>的id一致,name的值要和<bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" >中的PersonServiceImpl类的参数的名称一样(即private PersonDao personDao;)

4、测试,SpringTest类

package cn.chen.test;

import org.junit.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.chen.service.PersonService;

public class SpringTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception{
		
	}
	@Test public void instanceSpring(){
		//ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//读取spring配置文件
		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService) ctx.getBean("personService");//根据id值获得该类PersonService的对象,注意是获得,而不是创建,创建是由spring容器来完成的
		personService.save();
//		PersonService personService0 = (PersonService)ctx.getBean("personService");
//		PersonService personService1 = (PersonService)ctx.getBean("personService");
//		System.out.println(personService0==personService1);
		ctx.close();
	}
}
控制台出现:

我是save()方法
我是dao层的add()方法


二、值的注入

1、PersonServiceImpl类中,添加name属性,并生成setter和getter方法:

package cn.chen.service.impl;

import cn.chen.dao.PersonDao;
import cn.chen.service.PersonService;

public class PersonServiceImpl implements PersonService{

	private PersonDao personDao;
	private String name;
	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("我是save()方法");
		System.out.println("注入的值为:"+name);
		personDao.add();
	}

//	public void init(){
//		System.out.println("我是初始化方法");
//	}

	public PersonDao getPersonDao() {
		return personDao;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
2、配置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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
           <bean id="chen" class="cn.chen.dao.impl.PersonDaoImpl"></bean>
            <!--class表示类的路径,id表示由spring容器来创建该类的一个对象-->
          <bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" >
          	<property name="personDao" ref="chen"></property>
          	<property name="name" value="chenning"></property>
          </bean>
		 	
</beans>
3、测试,在控制台会显示

  我是save()方法
注入的值为:chenning
我是dao层的add()方法


三、集合的注入

1、PersonServiceImpl类中添加一下四个属性并生成setter和getter方法

	private Set<String> sets=new HashSet<String>();
	private List<String> lists=new ArrayList<String>();
	private Properties properties=new Properties();
	private Map<String,String> maps=new HashMap<String,String>();

2、添加接口方法:

package cn.chen.service;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public interface PersonService {
	public Set<String> getSets();
	public List<String> getLists();
	public Properties getProperties();
	public Map<String, String> getMaps();
	public void save();
}


3、配置beans.xml:

<bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" >
          	<property name="personDao" ref="chen"></property>
          	<property name="name" value="chenning"></property>
          	<!--  集合类的注入-->
          	<property name="sets">
          		<set>
          			<value>set-one</value>
          			<value>set-two</value>
          			<value>set-three</value>
          		</set> 		
          	</property>
          	<property name="lists">
          		<list>
          			<value>list-one</value>
          			<value>list-two</value>
          			<value>list-three</value>
          		</list>
          	</property>
          	<property name="properties">
          		<props>
          			<prop key="prop-key1">prop-value1</prop>
          			<prop key="prop-key2">prop-value2</prop>
          			<prop key="prop-key3">prop-value3</prop>  			
          		</props>
          	</property>
          	<property name="maps">
          		<map>
          			<entry key="map-key1" value="map-value1"/>
          			<entry key="map-key2" value="map-value2"/>
          			<entry key="map-key3" value="map-value3"/>
          		</map>
          	</property>
          	
          </bean>

4、测试,SpringTest类:

package cn.chen.test;

import org.junit.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.chen.service.PersonService;

public class SpringTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception{
		
	}
	@Test public void instanceSpring(){
		//ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//读取spring配置文件
		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService) ctx.getBean("personService");//根据id值获得该类PersonService的对象,注意是获得,而不是创建,创建是由spring容器来完成的
		personService.save();
//		PersonService personService0 = (PersonService)ctx.getBean("personService");
//		PersonService personService1 = (PersonService)ctx.getBean("personService");
//		System.out.println(personService0==personService1);
		System.out.println("======set======");
		for(String value : personService.getSets()){
			System.out.println(value);
		}
		System.out.println("======list======");
		for(String value : personService.getLists()){
			System.out.println(value);
		}
		System.out.println("======property======");
		for(Object key : personService.getProperties().keySet()){
			System.out.println(key+"="+personService.getProperties().getProperty((String) key));
		}
		System.out.println("======map======");
		for(String key : personService.getMaps().keySet()){
			System.out.println(key+"="+personService.getMaps().get(key));
		}
		ctx.close();
	}
}

控制台显示:

我是save()方法
注入的值为:chenning
我是dao层的add()方法
======set======
set-one
set-two
set-three
======list======
list-one
list-two
list-three
======property======
prop-key3=prop-value3
prop-key2=prop-value2
prop-key1=prop-value1
======map======
map-key1=map-value1
map-key2=map-value2
map-key3=map-value3


完成。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

源码下载地址:

点击打开链接



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值