spring三种实例化bean的方式
1构造函数实例化
2静态工厂方法实例化
3实例工厂方法实例化
service接口:
package service;
public interface PersonService {
public void save();
}
PersonServiceBean:
package service.impl;
import service.PersonService;
public class PersonServiceBean implements PersonService {
public void save(){
System.out.println("我是save()方法");
}
}
PersonServiceBeanFactory:
package service.impl;
public class PersonServiceBeanFactory {
/*
* 静态工厂方法
*/
public static PersonServiceBean createPersonServiceBean(){
return new PersonServiceBean();
}
/*
* 实例工厂方法
*/
public PersonServiceBean createPersonServiceBean2(){
return new PersonServiceBean();
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 默认构造方法 -->
<bean id = "personService" class = "service.impl.PersonServiceBean"></bean> <!-- 静态工厂方式 -->
<bean id = "personService2" class = "service.impl.PersonServiceBeanFactory"
factory-method = "createPersonServiceBean"></bean> <!-- 实例工厂方式 -->
<bean id = "personServiceFactory" class = "service.impl.PersonServiceBeanFactory"></bean>
<bean id = "personService3" factory-bean = "personServiceFactory"
factory-method = "createPersonServiceBean2"></bean> </beans>
测试类:
package test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import service.PersonService;
import service.impl.PersonServiceBean; /**
* @ClassName: FirstTest
* @Description: 测试实spring三种实例化方式
* @author 无名
* @date 2016-02-10
* @version 1.0
*/
public class FirstTest
{
@Test
public void testCreateBean(){
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf); //利用构造器来实例化
PersonService ps = ac.getBean("personService",PersonServiceBean.class);
ps.save(); //利用静态工厂方法
PersonService ps02 = (PersonService)ac.getBean("personService2");
ps02.save(); //实例工厂方法
PersonService ps03 = (PersonService)ac.getBean("personService3");
ps03.save(); }
}
spring三种实例化bean的方式的更多相关文章
- (转)Spring的三种实例化Bean的方式
http://blog.csdn.net/yerenyuan_pku/article/details/52832793 Spring提供了三种实例化Bean的方式. 使用类构造器实例化. <be ...
- 三种实例化bean的方式
在spring中有三中实例化bean的方式: 一.使用构造器实例化:(90%通常使用的一个方法) 二.使用静态工厂方法实例化: 三.使用实例化工厂方法实例化. 每种实例化所采用的配置是不一样的: 一. ...
- Spring三种实例化Bean的方法
1.实例化bean的三种方法:(1) 构造器<!-- 体验1 --><bean id="personService" class="com.persia ...
- Spring第四弹—–Spring的三种实例化bean的方式
1.使用类构造器实例化 1 <bean id=“orderService" class="cn.itcast.OrderServiceBean"/> 2. ...
- Spring中四种实例化bean的方式
本文主要介绍四种实例化bean的方式(注入方式) 或者叫依赖对象实例化的四种方式.上面的程序,创建bean 对象,用的是什么方法 ,用的是构造函数的方式 (Spring 可以在构造函数私有化的情况下把 ...
- Spring中三种配置Bean的方式
Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...
- spring IOC容器实例化Bean的方式与RequestContextListener应用
spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...
- Spring —— 三种配置数据源的方式:spring内置、c3p0、dbcp
01.Spring内置数据源配置Class:DriverManagerDataSource全限定名:org.springframework.jdbc.datasource.DriverManagerD ...
- Spring入门学习(二)三种实例化bean的方法
前面的哪一种就是通过构造函数来实例化对象 下面我们可能用到工厂方法来视力话对象,这样我们的配置文件又该怎么配置呢 <bean name="service2" class=&q ...
随机推荐
- sql语句备份
1.新采购需求查询 SELECT p.sku, g.GoodsName, w.WarehouseID, w.WarehouseName, s.FullNameFROM PurchaseRequires ...
- html制作,点击文字超链接显示文本框,再点击文字超链接隐藏文本框
</head><body> <script> window.onload=function(){ document.getElementById('click'). ...
- 基于阿里云服务器的git服务器搭建
使用阿里云Ubuntu 12.0.4 64位操作系统做git服务器. 首先git服务器有两种访问方式可以选择:http方式和ssh的方式,http方式更容易使用. 1.http方式的git服务器搭建以 ...
- mac--有用的命令和快捷键
有用的命令: 将man命令打开为pdf文件预览 man -t grep | open -f -a Preview 定位某文件的位置 locate htop 隐藏和显示桌面文件 chflags hidd ...
- jQuery中的事件和动画——《锋利的jQuery》(第2版)读书笔记2
第4章 jQuery中的事件和动画 jQuery中的事件 加载DOM $(document).ready(function(){ // 编写代码... }); 可以简写成: $(function( ...
- 连通图模板(HDU 1269)
http://acm.hdu.edu.cn/showproblem.php?pid=1269 题目大意:给定一个图,判断该图是否是强连通图.(强连通图为从任意一点出发,可到达其他所有点).深搜的Tar ...
- 解决 HttpClient 模拟 http 的get 请求后 ,出现 403 错误
解决方法: URI uri = builder.build(); // 创建http GET请求 HttpGet httpGet = new HttpGet(uri); httpGet.setHead ...
- MVC5+EF6 简易版CMS(非接口) 第一章:新建项目
目录 简易版CMS后台管理系统开发流程 MVC5+EF6 简易版CMS(非接口) 第一章:新建项目 MVC5+EF6 简易版CMS(非接口) 第二章:建数据模型 MVC5+EF6 简易版CMS(非接口 ...
- iOS.ReactNative-2-bridge-and-react-native-app-execution
Bridge and React Native App Execution 基于0.18.1 Async batched bridge used to communicate with the Jav ...
- ns3模拟无线Ad hoc 网络通信
Ad hoc网络 Ad hoc网是一种多跳的.无中心的.自组织无线网络,又称为多跳网(Multi-hop Network).无基础设施网(Infrastructureless Network)或自组织 ...