Spring通过IOC帮我们做火鸡
前言
什么是IOC,IOC是控制反转,打个比方,本来你自己new一只火鸡,现在让控制权交给spring,它使用依赖注入的技术做给你吃。
一、IOC——setter注入
1、准备dmo
首先准备一只火鸡
public class Turkey {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后准备一个厨房, 属性包含火鸡
public class Cookhouse {
private int id;
private String name;
private Turkey turkey;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Turkey getTurkey() {
return turkey;
}
public void setTurkey(Turkey turkey) {
this.turkey = turkey;
}
@Override
public String toString() {
return "Cookhouse{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
2、applicationContext.xml配置
给bean起个名字,class就是dmo类,property name是属性名,value和ref是属性值,如果属性是对象要使用"ref="
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="t" class="Turkey">
<property name="name" value="usaTurkey"/>
</bean>
<bean name="c" class="Cookhouse">
<property name="name" value="cnCookhouse"/>
<property name="turkey" ref="t"/>
</bean>
</beans>
3、添加spring的相关JAR包
JAR包以及其它知识详见网上一个大神的项目:自学网站
4、spring帮我们在厨房做火鸡
让spring帮我在厨房做火鸡吃,spring根据核心配置文件applicationContext.xml创建了一个中国厨房,然后做了只美国火鸡。
public class TestSpring {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
Cookhouse c = (Cookhouse)applicationContext.getBean("c");
System.out.println(c.getName()+"做了只"+c.getTurkey().getName());
}
}
运行结果:
二、IOC——注解注入
有小伙伴说:火鸡做太慢,等饿了
spring说:那我来用注解注入的方式给你做火鸡
1、@Autowired对属性的注解
首先优化对象的注入方式,applicationContext.xml添加<context:annotation-config/>
,表示告诉Spring要用注解的方式进行注入,<property name="turkey" ref="t"/>
就可以注释掉了,我们只需要在Cookhouse类的属性Turkey上面加上@Autowired就可以注入火鸡了
@Autowired
private Turkey turkey;
2、@Component对Bean的注解
然后优化Bean的注入方式,applicationContext.xml的Bean注入看上去还是比较繁琐,我们再简化下,只新增:
<context:component-scan base-package="com.fantasy.dmo"/>
其作用是告诉Spring,bean都放在这个包下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--bean都放在这个包下-->
<context:component-scan base-package="com.fantasy.dmo"/>
</beans>
因为Bean移到dmo了,所以在dmo分别给Cookhouse和Turkey加上注解@Component,然后给它们的属性name命名。
@Component("c")
public class Cookhouse {
private int id;
private String name="cnCookhouse";
@Autowired
private Turkey turkey;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Turkey getTurkey() {
return turkey;
}
public void setTurkey(Turkey turkey) {
this.turkey = turkey;
}
@Override
public String toString() {
return "com.fantasy.dmo.Cookhouse{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
@Component("t")
public class Turkey {
private int id;
private String name="usaTurkey";
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
运行程序发现结果一样,又省去很多配置,香喷喷的火鸡做的更快了呢~
Spring通过IOC帮我们做火鸡的更多相关文章
- spring boot到底帮我们做了那些事?
一.前言 上一篇介绍了注解,也是为这一篇做铺垫,传统的都是通过配置文件来启动spring,那spring boot到底是做了什么能让我们快速开发昵? 二.启动原理 看下程序启动的入口, ...
- spring是什么,Spring能帮我们做什么
1. spring是什么? Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发.应用程序是由一组相 ...
- [spring入门学习笔记][spring的IoC原理]
什么叫IoC 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency ...
- Spring的IOC
引用:http://www.cnblogs.com/xdp-gacl/p/4249939.html 学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念 ...
- Spring的IOC原理[通俗解释一下]
Spring的IOC原理[通俗解释一下] 1. IoC理论的背景我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图 ...
- Spring对IOC的理解
一.IOC控制反转和DI依赖注入 1.控制反转,字面可以理解为:主动权的转移,原来一个应用程序内的对象是类通过new去主动创建并实例化的,对对像创建的主动权在程序代码中.程序不仅要管理业务逻辑也要管理 ...
- 浅析对spring中IOC的理解
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- Spring中ioc的实现原理
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- Spring的IOC理解(转载)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
随机推荐
- 欧拉路&&欧拉回路
T1是欧拉路板子,但我不会,直接爆炸.. 这玩意就是个dfs,但我以前一直以为欧拉路只能$O(nm)$求 今天才知道可以$O(n+m)$ 欧拉路判定: 无向:起点终点为奇度点,其余偶度 有向:起点终点 ...
- 【转】python之property属性
1. 什么是property属性 一种用起来像是使用的实例属性一样的特殊属性,可以对应于某个方法 # ############### 定义 ############### class Foo: def ...
- 红帽7.4(RHCE7.4)磁盘扩容详细步骤
参照博文VMware虚拟机CentOS 7 磁盘扩容:https://www.linuxidc.com/Linux/2019-04/158346.htm 01.虚拟机扩容磁盘.如下图 02.使用roo ...
- 012.Kubernetes二进制部署worker节点Flannel
一 部署flannel 1.1 安装flannel kubernetes 要求集群内各节点(包括 master 节点)能通过 Pod 网段互联互通.flannel 使用 vxlan 技术为各节点创建一 ...
- ReactJS的4行代码
Angular 2一个显著的变动是,把Angular 1的Promise pattern改成了Observer pattern,并且使用了ReactJS.这里有一篇值得一读的文章 要搞懂ReactJS ...
- Zabbix-(四)邮件、钉钉告警通知
Zabbix-(四)邮件.钉钉告警通知 一.前言 在之前的文章里,通过Zabbix对主机的磁盘.CPU以及内存进行了监控,并在首页Dashboard里创建了监控图形,但是只有当我们登录到Zabbix后 ...
- Laravel上传文件(单文件,多文件)
为了方便,先修改一个配置文件,再laravel框架中config配置中找到 filesystems.php 文件 修改代码如下 'local' => [ 'driver' => 'loc ...
- 自建yum仓库,该仓库为默认仓库
YUM REPO: http://content.example.com/rhel7.0/x86_64/dvd 创建自建yum REPO文件: vim /etc/yum.repos.d/redhat. ...
- centos7 openssh 7.9.1 升级
由于项目构建时间比较长,近期安全检查发现openssh有漏洞.所以要升级openssh到7.9p1版本.由于ssh用于远程连接,所以要谨慎操作. 1. 依赖安装 OpenSSL版本:目前OpenSSH ...
- web自动化测试启示篇
1.首先,对于想学自动化测试的朋友,那么你得懂一种语言,常用的比如Java或者Python.因为没有语言基础,你是写不出自动化脚本的. 我个人选择java 2.有了开发语言的铺垫,那么开始入手Sele ...