Spring入门(二)
Spring IOC&DI
控制反转(inversion of control):控制什么?什么反转?
我们都知道,传统的程序中,如果A类需要使用B类对象,会在程序中直接创建B类对象实例,此时创建对象的控制权在A类手上;但现在出现了一个IOC/DI容器,由IOC/DI容器代替A类执行实例化B类的权利,所谓控制权由A类转向IOC容器,即控制反转。
依赖注入(Dependency Injection):什么是依赖,谁依赖谁,什么是注入,谁注入,注入哪里?
依赖就是依靠依附(鱼依赖水),对象依赖IOC/DI容器,注入:IOC/DI容器将某个对象的资源注入到所需要该对象资源的对象中;从IOC/DI容器的角度来看,Spring容器负责将被依赖对象赋值给调用者的成员变量,相当于为调用者注入他所依赖的实例。
总结:控制反转是原则,依赖注入是实现,控制反转的从程序角度出发描述概念,依赖注入从Spring容器角度出发描述概念。
Spring IOC实现
BeanFactory:提供完整的Ioc服务支持,是一个管理Bean的工厂,负责初始化各种bean,有多个实现类,例如:XmlBeanFactory(该类根据XML配置文件中的定义来装配Bean)
ApplicationContext:是BeanFactory的子接口,除了支持BeanFactory的功能外,还支持国际化,资源访问等内容
创建ApplicationContext接口实例的方法有三种
- ClassPathXmlApplicationContext(相对路径)
- FileSystemXmlApplicationContext(绝对路径)
- 通过Web服务器实例化ApplicationContext(web.xml文件中) 此种方式自己还未理解什么意思
依赖注入类型
使用构造方法注入
Dao&DaoImpl
public interface TestDIDao {
void sayhello();
}
import com.dao.TestDIDao;
public class TestDIDaoImpl implements TestDIDao{
@Override
public void sayhello() {
System.out.println("Hello World!");
}
}
Service&ServiceImpl
public interface TestDIService {
public void sayHello();
}
public class TestDIServiceImpl implements TestDIService {
public TestDIDao testDIDao;
//构造方法,用于实现依赖注入接口对象testDIDao
public TestDIServiceImpl(TestDIDao testDIDao) {
super();
this.testDIDao = testDIDao;
}
@Override
public void sayHello() {
//调用testDIDao中的sayHello方法
testDIDao.sayhello();
System.out.println("TestDIService依赖注入调用TestDIDao中的方法....");
}
}
Xml
<!-- 将指定的类TestDaoImpl配置给Spring,让Spring创建该实例 -->
<bean id="testDIDao" class="com.dao.impl.TestDIDaoImpl" />
<!-- 使用构造方法注入 -->
<bean id="testDIService" class="com.service.impl.TestDIServiceImpl">
<!--将myTestDADao注入到TestDIServiceImpl类的属性testDIDao上-->
<constructor-arg index="0" ref="testDIDao"></constructor-arg>
</bean>
Test
//测试用构造器方式注入
//初始化spring容器ApplicationContext,加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取testDIService实例,测试构造方法注入
TestDIService testDIService = (TestDIService) context.getBean("testDIService");
testDIService.sayHello();
使用属性注入
Dao&DaoImpl
public interface TestDIDao {
void sayhello();
}
import com.dao.TestDIDao;
public class TestDIDaoImpl implements TestDIDao{
@Override
public void sayhello() {
System.out.println("Hello World!");
}
}
Service&ServiceImpl
public interface TestDIService {
public void sayHello();
}
public class TestDIServiceImpl implements TestDIService {
public TestDIDao testDIDao;
//使用setter方法实现注入
public void setTestDIDao(TestDIDao testDIDao) {
this.testDIDao = testDIDao;
}
@Override
public void sayHello() {
//调用testDIDao中的sayHello方法
testDIDao.sayhello();
System.out.println("TestDIService依赖注入调用TestDIDao中的方法....");
}
}
XML
<!-- 将指定的类TestDaoImpl配置给Spring,让Spring创建该实例 -->
<bean id="testDIDao" class="com.dao.impl.TestDIDaoImpl" />
<!-- 使用属性方法注入 -->
<bean id="testDIService" class="com.service.impl.TestDIServiceImpl">
<!-- 调用TestDIServiceImpl类的setter方法,将myTestDADao注入到TestDIServiceImpl类的属性testDIDao上 -->
<property name="testDIDao" ref="testDIDao"></property>
</bean>
Test
//测试用属性方式注入
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
TestDIService testDIService = (TestDIService)context.getBean("testDIService");
testDIService.sayHello();
参考博文:
https://blog.csdn.net/doris_crazy/article/details/18353197
https://www.cnblogs.com/zanpen2000/p/7810884.html
https://www.cnblogs.com/leoo2sk/archive/2009/06/17/di-and-ioc.html
同样在自学Spring的小伙伴,如果看到我的博客,希望留下你的联系方式,我们一起加油!!!
Spring入门(二)的更多相关文章
- Spring入门(二):自动化装配bean
Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...
- Spring入门二:整合mybatis
一.SM思路分析 1.引入核心依赖及相关依赖: spring(略).mybatis.mysql.mybatis-spring(减少自己实现FactoryBean接口).druid <depen ...
- spring入门(二) 使用注解代替xml配置
1.导包(略) 2.applicationContext.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...
- Spring入门详细教程(二)
前言 本篇紧接着spring入门详细教程(一),建议阅读本篇前,先阅读第一篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/1016553 ...
- Spring Boot入门(二):获取配置文件值
本篇博客主要讲解下在Spring Boot中如何获取配置文件的值. 1. 使用yaml配置文件 Spring Boot默认生成的配置文件为application.properties,不过它也支持ya ...
- Spring(二)--Spring入门案例
Spring入门案例 1.需要的实体类 2.需要的接口和实现类 3.需要的service和实现类 /** * service层的作用 * 在不改变dao层代码的前提下,增加业务逻辑操作 */ publ ...
- spring 入门篇
spring 入门篇 相对于Hibernate(冬眠),Spring(春天),具有更多的诗意与希望的感觉,是为了解决传统J2EE开发效率过低.开发商之间不统一.没有真正实现“写一次到处 ...
- spring入门详细教程(五)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- Spring入门详细教程(四)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- Spring入门详细教程(三)
前言 本篇紧接着spring入门详细教程(二),建议阅读本篇前,先阅读第一篇和第二篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/101 ...
随机推荐
- JUnit中Assert简单介绍
junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...
- Vue.js文档学习
Vue细碎小点 生命周期钩子:created().mounted().updated().destroyed() 不要在选项属性或回调上使用箭头函数,比如 created: () => cons ...
- vue Inline JavaScript is not enabled. Is it set in your options?
Vue 全家桶 vue全面介绍--全家桶.项目实例 https://www.cnblogs.com/nogodie/p/9853660.html vue项目 Inline JavaScript is ...
- 4、Python 基础类型 -- Tuple 元祖类型
Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: 实例(P ...
- 正规式α向有限自动机M的转换
[注:这一节是在学习东南大学廖力老师的公开课时,所记录的一些知识点截屏,谢谢廖力老师的辛劳付出] 引入3条正规式分裂规则来分裂α,所得到的是NFA M(因为包含ε弧,之后进行确定化就是所需要求得DF ...
- 前端避免XSS(跨站脚本攻击)
尽量或禁止使用危险的脚本. 示例1: 如:eval() eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码.
- OpenSearch最新功能介绍
摘要:阿里云开放搜索(OpenSearch)是一款结构化数据搜索托管服务,其能够提供简单.高效.稳定.低成本和可扩展的搜索解决方案.OpenSearch以平台服务化的形式,将专业搜索技术简单化.低门槛 ...
- Python 生成json文件
1.数据准备 数据下载 2.python代码 import datetime import os import mssqlhelper ms = mssqlhelper.MSSQL(host=&quo ...
- noip2007 tg day1t1 统计数字
题目描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*10^9).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出 ...
- 第一个脚本 "Hello World!"
打开记事本就可以编辑脚本,REM就相当于注释,和脚本语言一样 REM Hello World GUI r DELAY STRING notepad ENTER DELAY STRING Hello W ...