之前在看spring,看IOC实在是云里雾里,包括看AOP也是云里雾里的,后来重新学习Java Web,做了一个简单的web项目,再之后看了崔希凡老师的视频,Day27和Day28两天的内容,真的很有必要,很重要!

这里先说一下我对IOC控制反转的理解:

之前我们创建对象,都是通过new一个对象来生成的,比如:

Student st = new Student();

就调用构造器来进行对象的创建,但是有更好的方法,就是通过工厂方式来创建对象,具体的内容如下:

1.创建beans.xml文件并添加如下的配置:

<beans>
<bean id="stu1" className="cn.seu.domain.Student">
<property name="number" value="1001"/>
<property name="name" value="zhangSan"/>
<property name="age" value="29"/>
<property name="sex" value="male"/>
</bean>
</beans>

这就是一个Student类,其中所有的成员变量通过<property></property>标签来声明。

通过bean工厂创建对象:

BeanFactory bf = new BeanFactory(“beans.xml”);
Student s1= (Student)bf.getBean(“stu1”);

在beans.xml中添加<bean>标签中的scope属性,其中如果scope的值为singleton,就是单例模式,如果scope的值为prototype,就是多例模式。

2.如果希望实现两个对象有关联关系,在Property标签下可以添加ref标签来指示属性的值:

<bean id="stu1" className=" cn.seu.domain.Student ">
<property name="number" value="1001"/>
<property name="name" value="zhangSan"/>
<property name="age" value="29"/>
<property name="sex" value="male"/>
<property name="teacher" ref="t1"/><!-- ref的值必须是另一个been的id -->
</bean> <bean id="t1" className=" cn.seu.domain.Teacher">
<property name="tid" value="TEACHER_2001" />
<property name="name" value="liSi" />
<property name="salary" value="1234.56" />
</bean>

即在工厂中得到的对象已经完成了装配的功能.

3.但是上述这种domain下面的类往往不会通过这种方式来进行配置,这样的方式往往应用在Dao,Service和Action中。通过面向接口编程实现解耦。

在Dao包下创建Dao的接口,并创建该接口的两个实现类:

public interface StudentDao {
void add(Student stu);
void update(Student stu);
} public class StudentImpl implements StudentDao {
@Override
public void add(Student stu) {
System.out.println("StudentImpl.add()");
}
@Override
public void update(Student stu) {
System.out.println("StudentImpl.update()");
}
} public class StudentImpl2 implements StudentDao {
@Override
public void add(Student stu) {
System.out.println("StudentImp2.add()");
}
@Override
public void update(Student stu) {
System.out.println("StudentImp2.update()");
}
}

在beans.xml文件中进行配置:

<bean id="stuDao" className="cn.seu.dao.impl.StudentImpl1">
</bean>

如果想使用该接口的第二种实现方式只需要在配置文件中改变className的值即可:

<bean id="stuDao" className="cn.seu.dao.impl.StudentImpl2">
</bean>

在使用的时候只需要将得到的bean转换成该接口类型就可以实现对这两个对象的使用:

BeanFactory bf = new BeanFactory("beans.xml");
StudentDao stuDao = (StudentDao)bf.getBean("stuDao");
stuDao.add(null);
stuDao.update(null);

保证了换实现类的时候并不需要改变代码。直接调用接口中的方法。

4.同时在Service包下创建Service接口及其实现类:

原来我们在Service中首先要创建Dao对象,才能使用Dao中的方法,如:

Private StudentDao studentDao = new StudentDao();

但是通过BeanFactory可以不需要在其中创建,而是谁调用service方法,谁就需要先调用创建Dao的方法:

public interface StudentService {
void login();
} public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = null;
// 谁调用service方法,谁就需要先调用本方法,提供dao
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public void login() {
studentDao.add(null);
studentDao.update(null);
}
}

在配置文件中需要进行相关配置:

<bean id="stuDao" className="cn.itcast.dao.impl.StudentImpl2">
</bean> <bean id="stuService" className="cn.itcast.service.impl.StudentServiceImpl">
<property name="studentDao" ref="stuDao"/>
</bean>

即可实现装配。

上述就是我个人对于IOC所谓的将对象的创建和装配功能转交给容器来完成。

BeanFactory和IOC控制反转的更多相关文章

  1. Spring Boot笔记十:IOC控制反转

    目录 IOC控制反转和DI依赖注入 IOC实现Hello World Spring IOC容器怎么知道哪些是管理的对象? IOC容器getBean方法的三种签名 xml配置文件的import导入 @A ...

  2. Spring详解篇之IoC控制反转

    ###一.Spring概况 spring是一个开源框架 是一个轻量的控制反转和面向切面的容器框架 大小和开销都是轻量的. 通过控制反转技术可以达到松耦合的目的 切面编程,允许通过分离应用的业务逻辑. ...

  3. Spring源码——IOC控制反转

    1.基础知识 Spring有两个核心功能,分别是ioc和aop,其中ioc是控制反转,aop是切面编程. 在ioc中,还有一个名次叫DI,也就是依赖注入.嗯,好像IOC和DI是指同一个,好像又感觉他俩 ...

  4. Python实现IOC控制反转

    思路: 用一个字典存储beanName和资源 初始化时先将beanName和资源注册到字典中 然后用一个Dscriptor类根据beanName动态请求资源,从而实现控制反转 # -*- coding ...

  5. Spring专题2: DI,IOC 控制反转和依赖注入

    合集目录 Spring专题2: DI,IOC 控制反转和依赖注入 https://docs.spring.io/spring/docs/2.5.x/reference/aop.html https:/ ...

  6. 回顾Spirng ioc 控制反转

    Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的.结合网上对Spring Ioc的理解,回顾一下自 ...

  7. 谈谈php里的IOC控制反转,DI依赖注入

    理论 发现问题 在深入细节之前,需要确保我们理解"IOC控制反转"和"DI依赖注入"是什么,能够解决什么问题,这些在维基百科中有非常清晰的说明. 控制反转(In ...

  8. DI依赖注入/IOC控制反转

    DI依赖注入# 啥都不说,直接上代码 <?php class UserController { private $user; function __construct(UserModel $us ...

  9. IoC实践--用Autofac实现MVC5.0的IoC控制反转方法

    Autofac是一个.net平台下发性能还不错的IoC框架,利用它可以实现依赖注入和控制反转,使自己的软件模块之间的耦合性大大降低,让软件扩展.维护更加容易.控制反转(Inversion of Con ...

随机推荐

  1. Deloyment Descriptor Web.xml Analysis

    https://javaee.github.io/tutorial/webapp003.html Web.xml详解分析: 该web.xml文件包含Facelets应用程序所需的几个元素.使用NetB ...

  2. jQuery设置全选和全反选

    HTML 代码: <input type="checkbox" id="allChecked" onclick="setAllChecked(t ...

  3. Android TextView之空格占位法

    在Android布局中进行使用到空格,为了实现文字的对齐.具体要怎么使用了? •请忽视文中‘& #160’中&和#之间的空格 空格: & #160; 窄空格: & #8 ...

  4. 【Node.js】一个愚蠢的Try Catch过错

    前段时间学习<深入浅出Nodejs>时,在第四章 - 异步编程中作者朴灵曾提到,异步编程的难点之一是异常处理,书中描述"尝试对异步方法进行try/catch操作只能捕获当次事件循 ...

  5. python3 爬虫笔记(一)beautiful_soup

    很多人学习python,爬虫入门,在python爬虫中,有很多库供开发使用. 用于请求的urllib(python3)和request基本库,xpath,beautiful soup,pyquery这 ...

  6. Help for enable SSL 3.0 and disable TLS 1.0..

    https://support.mozilla.org/en-US/questions/967266 i cant find tab Encryption for enable SSL 3.0 and ...

  7. Linux远程桌面(一)

    在机房折磨很久弄好的自己 Mark 一下.(测试环境rhel5.5) vnc 之独立服务配置 步骤一: (1)查看系统是否安装vnc服务(也可以在 系统-管理员-服务 里查看并勾选开机自启) # rp ...

  8. OpenGL纹理高级

    矩形纹理 对于二维纹理来说,除了GL_TEXTURE_2D之外,使用GL_TEXTURE_RECTANGLE就可以使用矩形纹理. 矩形纹理几大特点: 不能Mip,只能加载glTexImage2D的le ...

  9. CopyTranslator-复制即翻译的外文辅助阅读翻译解决方案

    英语/English 复制即翻译的外文辅助阅读翻译解决方案 请尽快更新到,这是你没有体验过的全新版本,只需3分钟,你就会跟我一样,爱上这个软件. 如果您觉得软件对您有所帮助,不用follow,不用fo ...

  10. python剑指offer数组中出现次数超过一半的数字

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...