BeanFactory和IOC控制反转
之前在看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控制反转的更多相关文章
- Spring Boot笔记十:IOC控制反转
目录 IOC控制反转和DI依赖注入 IOC实现Hello World Spring IOC容器怎么知道哪些是管理的对象? IOC容器getBean方法的三种签名 xml配置文件的import导入 @A ...
- Spring详解篇之IoC控制反转
###一.Spring概况 spring是一个开源框架 是一个轻量的控制反转和面向切面的容器框架 大小和开销都是轻量的. 通过控制反转技术可以达到松耦合的目的 切面编程,允许通过分离应用的业务逻辑. ...
- Spring源码——IOC控制反转
1.基础知识 Spring有两个核心功能,分别是ioc和aop,其中ioc是控制反转,aop是切面编程. 在ioc中,还有一个名次叫DI,也就是依赖注入.嗯,好像IOC和DI是指同一个,好像又感觉他俩 ...
- Python实现IOC控制反转
思路: 用一个字典存储beanName和资源 初始化时先将beanName和资源注册到字典中 然后用一个Dscriptor类根据beanName动态请求资源,从而实现控制反转 # -*- coding ...
- Spring专题2: DI,IOC 控制反转和依赖注入
合集目录 Spring专题2: DI,IOC 控制反转和依赖注入 https://docs.spring.io/spring/docs/2.5.x/reference/aop.html https:/ ...
- 回顾Spirng ioc 控制反转
Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的.结合网上对Spring Ioc的理解,回顾一下自 ...
- 谈谈php里的IOC控制反转,DI依赖注入
理论 发现问题 在深入细节之前,需要确保我们理解"IOC控制反转"和"DI依赖注入"是什么,能够解决什么问题,这些在维基百科中有非常清晰的说明. 控制反转(In ...
- DI依赖注入/IOC控制反转
DI依赖注入# 啥都不说,直接上代码 <?php class UserController { private $user; function __construct(UserModel $us ...
- IoC实践--用Autofac实现MVC5.0的IoC控制反转方法
Autofac是一个.net平台下发性能还不错的IoC框架,利用它可以实现依赖注入和控制反转,使自己的软件模块之间的耦合性大大降低,让软件扩展.维护更加容易.控制反转(Inversion of Con ...
随机推荐
- 将webservice嵌套到以完成的web项目中
一.先把webservice服务端写入项目(基于spring) 1.在pom.xml中引入WebService相关的jar依赖 <!--webservice开始 --> <!--ht ...
- mysql常用的优化措施
http://www.cnblogs.com/ggjucheng/archive/2012/11/07/2758058.html
- nopCommerce如何支持MySQL
此方法支持nopCommerce2.4以上版本(缺少的代码,可参照nopCommerce2.6源码) nopCommerce 4.1 如何支持Mysql 请看 Url: http://www.nop ...
- python3绘图示例6-1(基于matplotlib,绘图流程介绍及设置等)
#!/usr/bin/env python# -*- coding:utf-8 -*- import os import pylab as pyimport numpy as npfrom matpl ...
- LeetCode Path Sum 判断树的路径之和
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; ...
- Ubuntu中在QT中配置OpenGL
之前搞实验室项目,博客有些天没有更新.现在学习需要,开始搞OpenGL+Ubuntu+QT. 搞了整整一天,由于是首次使用ubuntu,所以这ubuntu下配置qt和Opengl环境时走了很多的弯路, ...
- 认识http客户端
最简单的http客户端就是我们的浏览器,浏览器地址输入baidu.com,就会返回响应内容,打开network,都是http请求,第一个就是www.baidu.com的请求,旁边第一个General就 ...
- nodejs的一些概念
上一节我们几乎是扫通http请求和响应的整个闭环,包括请求时候的头信息和服务器返回时候的头信息和状态码等等,这些在node的http中都能获取到,并且有相应都接口组装这些信息和返回它们,同时这些htt ...
- 使用shc加密bash脚本程序
摘要以前写看到别人写的脚本用shc加密的,我也有就了解了下. SHC代表shell script compiler,即shell脚本编译器.通过SHC编译过的脚本程序对普通用户而言是不读的,因此如果你 ...
- 利用API设置桌面背景
实现效果: 知识运用: API函数SystemParametersInfo 实现代码: [DllImport("user32.dll", EntryPoint = "Sy ...