博客
关于我
Spring框架的复习IoC部分
阅读量:435 次
发布时间:2019-03-05

本文共 773 字,大约阅读时间需要 2 分钟。

Spring框架的复习

IoC容器(控制反转,依赖注入)

主要问题

对象由谁创建?对象属性如何设置?控制?反转?优点?

解答

对象由Spring容器创建

对象属性通过Spring容器设置

传统的应用程序对象的创建时由程序本身控制的,使用Spring后,由Spring创建

反转的内容:正转时之程序来创建对象,而反转指程序本身不回去创建对象,而变味了被动接收对象。在配置文件中声明对象,你需要的时候给你

Ioc是一种编程思想,由主动编程变为被动接收,Ioc

优点

对象由原来的程序主动创建变为了程序接收对象,程序员可以集中于业务的实现

实现了各层之间的解耦,实现分离,告别直接依赖

代码示例

Hello.java

public class Hello { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void show() { // TODO Auto-generated method stub System.out.println("Hello:"+name); }}

bean.xml

Test.java

public class Test { public static void main(String[] args) { //解析bean.xml文件,生成管理相应的bean对象 ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml"); Hello h=(Hello) ctx.getBean("hello"); h.show(); }}

最终结果

转载地址:http://yutwz.baihongyu.com/

你可能感兴趣的文章
SpringBoot之ElasticsearchRestTemplate常用示例
查看>>
ping 全网段CMD命令
查看>>
ping 命令的七种用法,看完瞬间成大神
查看>>
Pinia入门(快速上手)
查看>>
Pinia:$patch的使用场景
查看>>
Pinia:$subscribe()的使用场景
查看>>
Pinpoint对Kubernetes关键业务模块进行全链路监控
查看>>
Pinterest 大规模缓存集群的架构剖析
查看>>
pintos project (2) Project 1 Thread -Mission 1 Code
查看>>
PinYin4j库的使用
查看>>
PIP
查看>>
pip install goose-extractor // SyntaxError: Missing parentheses in call to 'print'
查看>>
pip install mysqlclient报错
查看>>
pip install 出现报asciii码错误的解决
查看>>
pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding‘ 在尝试安装新软件包时
查看>>
pip 下载慢
查看>>
pip 升级报错AttributeError: ‘NoneType’ object has no attribute ‘bytes’
查看>>
pip 安装opencv-python卡死
查看>>
pip 安装出现异常
查看>>
Pip 安装失败:需要 SSL
查看>>