Spring的依赖注入和管理Bean
1.实例化spring容器 和 从容器获取Bean对象
实例化Spring容器常用的两种方式:
方法一:
在类路径下寻找配置文件来实例化容器 [推荐使用]
ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:/META-INF/spring/applicationContext-memcached.xml");
MemCachedManager cache = appContext.getBean(MemCachedManager.class);
方法二:
在文件系统路径下寻找配置文件来实例化容器 [这种方式可以在开发阶段使用]
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“d:\\beans.xml“});
Spring的配置文件可以指定多个,可以通过String数组传入。
当spring容器启动后,因为spring容器可以管理bean对象的创建,销毁等生命周期,
所以我们只需从容器直接获取Bean对象就行,而不用编写一句代码来创建bean对象。
从容器获取bean对象的代码如下:
ApplicationContext ctx = new ClassPathXmlApplicationContext(“beans.xml”);
OrderService service = (OrderService)ctx.getBean("personService");
2.Spring实例化Bean的三种方式
以下是三种方式的例子:
1.使用类构造器实例化 [默认的类构造器]<bean id=“orderService" class="cn.itcast.OrderServiceBean"/>
2.使用静态工厂方法实例化<bean id="personService" class="cn.itcast.service.OrderFactory" factory-method="createOrder"/>
public class OrderFactory {
public static OrderServiceBean createOrder(){ // 注意这里的这个方法是 static 的!
return new OrderServiceBean();
}
}
3.使用实例工厂方法实例化:<bean id="personServiceFactory" class="cn.itcast.service.OrderFactory"/>
<bean id="personService" factory-bean="personServiceFactory" factory-method="createOrder"/>public class OrderFactory { public OrderServiceBean createOrder(){ return new OrderServiceBean();
}
}
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
3.Bean的生命周期 (Bean的作用域)
bean的scope 属性
The scope of this bean: typically "singleton" (one shared instance, which will be returned by all calls
to getBean with the given id), or "prototype" (independent instance resulting from each call to
getBean). Default is "singleton". Singletons are most commonly used, and are ideal for multi-
threaded service objects. Further scopes, such as "request" or "session", might be supported by
extended bean factories (e.g. in a web environment). Note: This attribute will not be inherited by
child bean definitions. Hence, it needs to be specified per concrete bean definition. Inner bean
definitions inherit the singleton status of their containing bean definition, unless explicitly specified:
The inner bean will be a singleton if the containing bean is a singleton, and a prototype if the
containing bean has any other scope.

.singleton [单例]
eg:<bean id="personService" class="com.yinger.service.impl.PersonServiceBean" scope="singleton"></bean>
在每个Spring IoC容器中一个bean定义只有一个对象实例。
请注意Spring的singleton bean概念与“四人帮”(GoF)模式一书中定义的Singleton模式是完全不同的。
经典的GoF Singleton模式中所谓的对象范围是指在每一个ClassLoader中指定class创建的实例有且仅有一个。
把Spring的singleton作用域描述成一个container对应一个bean实例最为贴切。亦即,假如在单个Spring容器内定义了某个指定class的bean,
那么Spring容器将会创建一个且仅有一个由该bean定义指定的类实例。
默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。
如:<bean id="xxx" class="cn.itcast.OrderServiceBean" lazy-init="true"/>
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下:
<beans default-lazy-init="true“ ...>
.prototype [原型]
每次从容器获取bean都是新的对象。
对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个prototype bean的整个生命周期负责:容器在初始化、配置、装饰或者是
装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法。
但对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,
都是客户端代码的职责。(让Spring容器释放被prototype作用域bean占用资源的一种可行方式是,通过使用bean的后置处理器,该处理器持有要被清除的bean的引用。)
以下的三种scope只是在web应用中才可以使用
.request
.session
.global session
使用这三种配置之前要先初始化Web配置
5.xml配置方法和参数
<context:component-scan base-package="com.ibs.gbplarform.common.memcached" />
<bean id="memCachedPool" class="com.whalin.memcached.SockIOPool"
factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
<property name="servers">
<list>
<value>127.0.0.1:11211</value>
</list>
</property>
<property name="initConn">
<value>20</value>
</property>
<property name="minConn">
<value>10</value>
</property>
<property name="maxConn">
<value>50</value>
</property>
<property name="maintSleep">
<value>3000</value>
</property>
<property name="nagle">
<value>false</value>
</property>
<property name="socketTO">
<value>3000</value>
</property>
</bean>
<bean id="memCachedClient" class="com.whalin.memcached.MemCachedClient">
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
</bean>
Spring的依赖注入和管理Bean的更多相关文章
- spring不依赖注入得到实体bean
如题,我们一般用spring的ioc,通过配置注入接口得到这个实现类,现在通过研究公司平台框架发现还有一种方法得到spring文件配置的bean方法,举个例子(注:这个ApplicationConte ...
- spring的依赖注入是什么意思
最近学习spring框架,对依赖注入有些模糊,遂上网翻阅资料,做了下列总结,原博客为CSDN 南夏的 spring的依赖注入是什么意思,侵删! Spring 能有效地组织J2EE应用各层的对象.不管是 ...
- SpringBoot系列: 理解 Spring 的依赖注入(一)
==============================Spring 的依赖注入==============================对于 Spring 程序, Spring 框架为我们提供 ...
- spring中依赖注入
理解依赖注入:参考https://blog.csdn.net/taijianyu/article/details/2338311 一.依赖注入让bean与bean之间以配置文件组织在一起,而不是以硬编 ...
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...
- Spring的依赖注入(DI)三种方式
Spring依赖注入(DI)的三种方式,分别为: 1. 接口注入 2. Setter方法注入 3. 构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...
- spring的依赖注入的最常见的两种方法
package com.lsz.spring.action; public class User { /** * set注入 */ private String username; public vo ...
- Java Spring各种依赖注入注解的区别
Spring对于Bean的依赖注入,支持多种注解方式: @Resource javax.annotation JSR250 (Common Annotations for Java) @Inject ...
- 一步一步深入spring(3)--spring的依赖注入方式
对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程,sprin ...
随机推荐
- Git的忽略、分支、分支与主线的合并、远程仓库的操作
如果想了解 Git 以及一些基础命令的使用,请看我的另一篇博客: http://www.cnblogs.com/haojun/p/7797508.html 这篇博客会跟大家介绍一下怎么在提交的时候忽略 ...
- Mysql索引优化之索引的分类
Mysql的历史 简单回顾一下Mysql的历史,Mysql 是一个关系型数据库管理系统,由瑞典 Mysql AB 公司开发,目前属于 Oracle 公司.关系型数据库将数据保存在不同的表中,而不是将 ...
- PATB 1018. 锤子剪刀布
时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图 ...
- 使用docker运行GitLab
从docker镜像拉取代码,docker pull gitlab/gitlab-ce:latest. 创建/srv/gitlab目录sudo mkdir /srv/gitlab 启动GitLab CE ...
- node.js + mssql 简易封装操作
时间吧,总是这么凑巧,在我学习[node.js]还没几天,我的 Microsoft SQL Server Management Studio 18 就歇菜了,至于怎么歇菜的吧....它可能的意思就是想 ...
- [乐意黎原创]Centos 7里apache(httpd)自启动
最近,Aerchi在折腾 ECS服务器 (Centos 7),每次重启后都要手动开启apache服务,好繁琐. 仔细研究了下:Apache 的服务第一.启动.终止.重启systemctl start ...
- linux 安装 websocketd
1.下载 wget https://github.com/joewalnes/websocketd/releases/download/v0.3.0/websocketd-0.3.0-linux_am ...
- 【PostgreSQL】 前缀模糊查询级优化
前匹配模糊 使用B-Tree来加速优化前匹配模糊查询 构造数据 新建一张商品表,插入一千万条数据. create table goods(id int, name varchar); insert i ...
- 如何用ModelSim对Xilinx ISE产生的网表进行仿真
图: 在对设计的芯片进行测试时,经常要用到FPGA,可是里面的仿真工具却不如Modelsim那么好用,且在规模比较大时,ISE在仿真时,软件经常会报告内存限制的问题,此时一般会切换到Modelsim软 ...
- [Noi2002]Savage 题解
[Noi2002]Savage 时间限制: 5 Sec 内存限制: 64 MB 题目描述 输入 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci ...