Spring实例化bean的几种方式
一,通过constructor实例化bean
Spring可以实例化各种类型的类,不要求必须是JavaBean类型的类。在XML中配置类如下:
<bean id="exampleBean" class="examples.ExampleBean"/> <bean name="anotherExample" class="examples.ExampleBeanTwo"/>
二,通过静态factory method实例化bean
当通过静态factory method实例化bean时,需要用class属性来指定含有静态factory method的类,用factory-method属性来指定对应的factory method。当调用这一方法时,将返回一个类的对象,效果和通过constructor来实例化相同。注意,对应的factory method必须是static方法。
<bean id="clientService"
class="examples.ClientService"
factory-method="createInstance"/>
public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}
public static ClientService createInstance() {
return clientService;
}
}
三,通过实例factory method来实例化bean
与通过静态factory method实例化bean的方法类似,区别是,通过实例factory method来实例化bean需要调用容器中另一个bean的非静态方法。使用这种方式时,class属性不需要填写,在factory-bean属性中制定容器中包含指定factory method的bean,在factory-method属性中设置factory method的名称。
<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
<!-- inject any dependencies required by this locator bean -->
</bean> <!-- the bean to be created via the factory bean -->
<bean id="clientService"
factory-bean="serviceLocator"
factory-method="createClientServiceInstance"/>
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
private DefaultServiceLocator() {}
public ClientService createClientServiceInstance() {
return clientService;
}
}
一个factory类可以包含多个factory method。
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
<!-- inject any dependencies required by this locator bean -->
</bean> <bean id="clientService"
factory-bean="serviceLocator"
factory-method="createClientServiceInstance"/> <bean id="accountService"
factory-bean="serviceLocator"
factory-method="createAccountServiceInstance"/>
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
private static AccountService accountService = new AccountServiceImpl();
private DefaultServiceLocator() {}
public ClientService createClientServiceInstance() {
return clientService;
}
public AccountService createAccountServiceInstance() {
return accountService;
}
}
Spring实例化bean的几种方式的更多相关文章
- Spring实例化Bean的三种方式及Bean的类型
1.使用类构造器实例化 [默认的类构造器] <bean id=“orderService" class="cn.itcast.OrderServiceBean"/ ...
- Spring 实例化bean的三种方式
第一种方法:直接配置Bean <bena id="所需要实例化的一个实例名称" class="包名.类名"/> 例如: 配置文件中的bean.XML ...
- Spring 实例化Bean的3种方式
要使用Spring中的Bean,需要先创建这个Bean的实例. 实例化Bean有3种方式: 构造器方式 静态工厂方式 实例工厂方式 构造器方式 构造器方式是最常用的.在Bean中写构造函数,然后在配置 ...
- Spring 实例化bean的三种方式:
方法一:使用构造器实例化bean java代码: package com.model; public class User { private String username; public User ...
- spring实例化bean的三种方式
公共使用的实体
- Spring学习之实例化bean的三种方式
实例化bean的三种方式 构造器实例化bean Person.java public class Person { private String name; private Integer age; ...
- spring创建bean的三种方式
spring创建bean的三种方式: 1通过构造方法创建bean(最常用) 1.1 spring默认会通过无参构造方法来创建bean,如果xml文件是这样配置,则实体类中必须要有无参构造方法,无参构造 ...
- spring 注入bean的两种方式
我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...
- Spring获取bean的几种方式
工作中需要对一个原本加载属性文件的工具类修改成对数据库的操作当然,ado层已经写好,但是需要从Spring中获取bean,然而,工具类并没有交给Spring来管理,所以需要通过方法获取所需要的bean ...
随机推荐
- TOSCA自动化测试工具安装
1.下载链接 https://www.tricentis.com/software-testing-tools/ 2.免费试用14天, 弹出的页面输入邮箱地址--> 输入一堆信息-->点击 ...
- 【Linux学习】3.Linux常见配置文件
一./etc 配置文件/etc/passwd 用户数据库,其中的域给出了用户名.真实姓名.家目录.加密口令和用户的其他信息 /etc/group 类似/etc/passwd ,但说明的不是用户而是组. ...
- Nginx 日志
原文地址:Nginx深入详解之日志 作者:scq2099yt 一.日志分类 Nginx日志主要分为两种:访问日志和错误日志.日志开关在Nginx配置文件(/etc/nginx/nginx ...
- #ifndef用法
用于避免重复包含头文件 #ifndef _STDIO_H_ #define _STDIO_H_ ...... #endif
- mp4格式的视频,编码方式mpeg4,转化为h264
知识点:在使用vcastr3.swf播放器播放flv视频,(同时在html5页面,使用<video>标签时),发现某些MP4格式的代码不能播放 原因:vcastr3.swf和video,不 ...
- DataStage系列教程 (Slowly Changing Dimension)缓慢变化维
BI中维表的增量更新一般有2种: Type 1:覆盖更改.记录的列值发生变化,直接update成最新记录. Type 2:历史跟踪更改.记录值发生变化,将该记录置为失效,再insert一条新的记录. ...
- java使用poi实现excel表格生成
通过使用poi技术生成Excel,使用反射技术实现自动映射列表的数据. ExportTableUtil.java public class ExportTableUtil { /** * * @Des ...
- maven项目Java resources 上面有个红叉但是代码里面并没有什么报错
maven项目Java resources 上面有个红叉但是代码里面并没有什么报错 解决办法: 1.通过: windows菜单 -> show view/other 菜单 -> o ...
- ResultSet 结果集
转自:http://blog.csdn.net/z93971401/article/details/7469503 这篇文章并没有给出如何使用ResultSet的具体例子,只是从ResultSet的功 ...
- 中国剩余定理——nyoj
中国剩余定理------解法如下:假设存在一个数M M%A=a , M%B=b , M%C=c并且A,B,C必须俩俩互质.满足这一条件下:存在一个R1使得 , K1=A*B*R1 ,K1%C==1.存 ...