spring2.5学习笔记(二):IOC控制反转简单演示

本文通过一个具体的例子展示了Spring框架中IOC(控制反转)的实现方式。主要介绍了如何使用beans.xml配置文件来定义bean,并通过依赖注入的方式让PersonServiceBean能够调用PersonDaoBean中的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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">

<!-- 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>
</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 {
/* (non-Javadoc)
* @see cn.itcast.dao.impl.IPersonDao#add()
*/
public void add(){
System.out.println("执行PersonDaoBean类中的add方法");
}
}


cn.itcast.service包下的java文件

package cn.itcast.service;
public interface IPersonService {
public abstract void save();
}


cn.itcast.service.impl包下的java文件

package cn.itcast.service.impl;
import cn.itcast.dao.impl.IPersonDao;
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 ;
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方法");
ipersondao.add();
}

public void destroy(){
System.out.println("关闭打开的资源");
}
}



最后,单元测试用例:

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 ctx1 = new ClassPathXmlApplicationContext("beans.xml");

IPersonService personservice5 = (IPersonService)ctx1.getBean("personservice5");
personservice5.save();

}

}



学习感想:

打个比方:接口IPersonDao是父亲,类PersonDaoBean是接口的儿子。

父亲年龄大了,儿子要尽孝道。现在情况是,父亲有张银行卡add(),但是没钱。儿子有张银行卡add(),有很多钱。
正常情况下是父亲没钱用,儿子主动打钱。好,现在出问题了,儿子不给老子钱了,那怎么办?

找法院判决吧,判决的结果是:儿子的卡和父亲的卡绑定,父亲要用钱直接用自己的卡取,不管(而且自己的卡是没钱的,只是儿子的有钱),儿子由于父亲不怎么会取钱的手续,一切手续由代办机构代办。父亲只管问要钱,想啥时候要就啥时候要,其他一切不必操心。

程序实现以上比喻是这样的:

1:父亲有卡,IPersonDao接口中add方法(方法没实现,比喻为没钱),儿子有卡,PersonDaoBean类中的方法(方法实现,比喻为有钱)

2:儿子的卡与父亲的卡绑定,就是配置文件的依赖注入
<property name="ipersondao" ref="persondao"></property>

3:代理机构帮父亲办理手续取钱,就是spring帮忙管理对象的创建和销毁。

4:父亲取到的钱
测试类中personservice5.save();,能输出儿子(PersonDaoBean)中的add方法中的语句
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值