`

Spring基础入门一:基于XML配置完成Spring对Bean的实例化

阅读更多

一、基于XML配置的Spring对Bean的实例化:有三种方式:类的构造器,静态工厂,实例工厂

<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-2.5.xsd">

	<!-- Spring三种实例化Bean之一:使用类构造器实例化 -->
	<bean id="personService"
		class="base.service.imple.PersonServiceImple">
	</bean>

	<!-- Spring三种实例化Bean之二:使用静态工厂方法实例化 -->
	<bean id="personService2"
		class="base.factory.StaticBuildBeanFactroy"
		factory-method="createPersonService">
	</bean>

	<!-- Spring三种实例化Bean之三:使用实例工厂方法实例化 -->
	<bean id="buildBeanFactory" class="base.factory.BuildBeanFactory"></bean>
	<bean id="personService3" factory-bean="buildBeanFactory"
		factory-method="createPersonService">
	</bean>
</beans>

 Bean的接口:

package base.service;

public interface PersonService {

	public void save();

}

 Bean的实现:

public class PersonServiceImple implements PersonService {

	// 一个普通的方法,可以在beans.xml设置成它为在构造方法执行完后就执行的方法
	public void init() {
		System.out.println("初始化方法");
	}

	// 一个普通的方法,可以在beans.xml设置成它为在Spring容器关闭后执行的方法
	// (若该Bean的scope为prototype时则不会执行)
	public void destroy() {
		System.out.println("销毁方法");
	}

	// 构造方法(用来显示实例始化了)
	public PersonServiceImple() {
		System.out.println("实例化了...");
	}

	public void save() {
		System.out.println("PersonServiceImple的save()方法");
	}
}

 两个Bean工厂如下:

/**
 * 实例工厂构造Bean
 * 
 * @author 张明学
 */
public class BuildBeanFactory {

	public PersonService createPersonService() {
		return new PersonServiceImple();
	}
}
 
/**
 * 静态工厂构造Bean
 * 
 * @author 张明学
 */
public class StaticBuildBeanFactroy {
	public static PersonService createPersonService() {
		return new PersonServiceImple();
	}
}

 测试:

public static void main(String[] args) {
		// Spring 容器初始化
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		// 多个Spring配置文件可以作为一个数组
		// context = new ClassPathXmlApplicationContext(new String[] {
		// "beans.xml" });
		PersonService personService = null;
		//personService = (PersonService) context.getBean("personService");
		//personService = (PersonService) context.getBean("personService2");
		personService = (PersonService) context.getBean("personService3");
		personService.save();
	}

 Spring2.5为Bean提供了几中模式:singleton(默认配置),prototype,request,session,global session

还可以设置Bean实例的时机,可以在容器启动时构造实例(默认配置)也可在第一次使用时构造实例。

还有一些其配置如下:

<!-- 
		scope默认为:singleton 单例模式
			 设置为:prototype 每次得到一个新的实例对象
			   还有:request,session,global session
		lazy-init: true	 表示:该Bean延迟初始化,不是Spring容器初始化时就初始化
				   false(默认值) 表示:不以该Bean延迟初始化,即Spring容器初始化时就初始化
				   在beans设置default-lazy-init可以为该beans里所有的bean设置一个默认的lazy-init的值
		设置了scope为“prototype”时:lazy-init无论是true还是false都是延迟初始化
		
		init-method 设置为:该bean中的某个方法在构造方法执行完就执行的方法
		destroy-method 设置为:在Spring空器关闭之后执行的方法(若将该bean的scope设置为prototype则不会执行)
	-->
	<bean id="personService4"
		class="base.service.imple.PersonServiceImple" scope="prototype"
		lazy-init="true" init-method="init" destroy-method="destroy">
	</bean>
   

测试如下:

public static void main(String[] args) {
		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		PersonService p1 = (PersonService) ctx.getBean("personService4");
		PersonService p2 = (PersonService) ctx.getBean("personService4");
		/**
		 * 在默认情况下:scope为singleton单例模式 prototype每次会得到一个新的实例
		 */
		System.out.println(p1 == p2);
		// 关闭Spring的容器
		ctx.close();
	}
 

 

分享到:
评论
10 楼 yadan 2010-06-20  
我觉得挺好啊
9 楼 zzpxingfu 2010-06-08  
leijing 写道
希望LZ能够继续写好。

Spring 事务管理
8 楼 xiaojing3517 2010-06-07  
马上就杯具了。。。。
7 楼 qiyueguxing 2010-06-07  
这种贴也能在主页上!Google一搜一大把!
类似的没有技术含量的东西直接写在自己博客里好了。
6 楼 xiaobojava 2010-06-07  
我会坚持的!最近很忙。
5 楼 freeza 2010-06-07  
同求楼主头像!
4 楼 leijing 2010-06-07  
希望LZ能够继续写好。
3 楼 leepengyu 2010-06-06  
LZ的头像 有没有大的? 用来做Eclipse的启动画面   不错  有的话发一份
2 楼 leepengyu 2010-06-06  
.....其实我衷心的希望不要再发XXX入门一...XXX学习笔记一...  然后就没有下文了  此类帖子一是多, 二是鱼龙混杂, 如要坚持发, 希望能够保证高质量和持续性。
1 楼 joehe 2010-06-06  
配数据库连接,密码怎么己加密的方式配置?

相关推荐

    Spring — 基于Java类的配置 代码实例

    基于Java类的配置方法和基于XML或基于注解的配置方式相比,前者通过代码的方式更加灵活地实现Bean的实例化及Bean之间的装配,但后面两者都是通过配置声明的方式,在灵活性上要稍逊一些,但是配置上要更简单一些。

    基于java的企业级应用开发:Bean的实例化.ppt

    * * * * * * Bean的实例化 Bean的实例化有哪些方式? Bean的实例化 在面向对象的程序中,想要使用某个对象,就需要先实例化这个对象。同样,在Spring中,要想使用容器中的Bean,也需要实例化Bean。实例化Bean有三种...

    Spring入门.docx

    实例化完成的Bean对象存放在singletonObjects当中。 FileSystemXmlApplicationContext是加载磁盘里具体位置的xml文件。不同环境下会出现不同的容器,比如导入spring-web包时,会有web环境下的容器。 (4)基于xml

    Spring bean初始化及销毁你必须要掌握的回调方法.docx

    3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1、通过java提供的@PreDestroy注释; 2、通过实现spring提供的DisposableBean接口,并...

    spring001 基于xml的DI bean实例调用的是无参构造,如果需要bean对象的属性进行初始化

    spring核心:iOC Aop IOC:inverson of control(控制反转): 就是对对象控制权的转移,从程序代码本身反转到 外部容器中,通过外部容器实现对象的创建,属性 ...创建spring的配置文件,编写bean 4.在测试类中测试

    JAVA spring 系列案例50个和学习资料

    Spring系列第4篇:xml中bean定义详解(-)Spring系列第5篇:创建bean实例这些方式你们都知道?Spring系列第6篇:玩转bean scope,避免跳坑里!Spring系列第7篇:依赖注入之手动注入Spring系列第8篇:自动注入...

    SpringBoot开发实战(实战案例)

    案例05 Bean的实例化 案例06 Bean的作用域 案例07 OOP方式实现日志记录 案例08 AOP方式实现日志记录 案例09 基于JdbcTemplate的学生信息维护 案例10 Maven入门案例 案例11 基于Maven构建实现学生新增 案例12 Spring ...

    模拟实现Spring的IOC

    1、Spring主要两个作用:实例化Bean,动态装配Bean。并将所有的bean放到spring容器中,调用时从容器中取。Spring容器就是一个bean的Map:private Map, Object&gt; beans = new HashMap, Object&gt;(); 2、本工程,模拟...

    spring技术入门相关源码

    spring技术入门系列源码 public class SpringTest { public static void main(String[] args) { //创建spring的ApplicationContext ApplicationContext ctx = new ClassPathXmlApplicationContext(...

    spring.doc

    3.3.1使用类构造器实例化(默认无参数) 14 3.3.2使用静态工厂方法实例化(简单工厂模式) 14 3.3.3初始化(创建)bean时机 15 Lazy-init初始化bean的时机拓展: 15 3.4 Bean的作用域 16 Scope单例多例作用域拓展: 16 ...

    Spring+3.x企业应用开发实战光盘源码(全)

     第7章:对如何使用基于AspectJ配置AOP的知识进行了深入的分析,这包括使用XML Schema配置文件、使用注解进行配置等内容。  第8章:介绍了Spring所提供的DAO封装层,这包括Spring DAO的异常体系、数据访问模板等...

    spring框架技术+第2天+xmind思维导图

    bean实例化三种方式:用构造器来实例化 、使用 静态工厂方法实例化 、使用实例工厂方法实例化 。总结,我们会选择第一种方式,因为spring的存在就是要消除工厂模式,因为工厂本身就会在每次调用时new出对象,只是把...

    Spring.3.x企业应用开发实战(完整版).part2

    10.4.1 Spring通过单实例化Bean简化多线程问题 10.4.2 启动独立线程调用事务方法 10.5 联合军种作战的混乱 10.5.1 Spring事务管理器的应对 10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼...

    spring3.0jar包

    ◆容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一...

    Spring-Reference_zh_CN(Spring中文参考手册)

    实例化bean 3.2.4. 使用容器 3.3. 依赖 3.3.1. 注入依赖 3.3.1.1. Setter注入 3.3.1.2. 构造器注入 3.3.1.3. 一些例子 3.3.2. 构造器参数的解析 3.3.2.1. 构造器参数类型匹配 3.3.2.2. 构造器参数的索引 3.3.3. ...

    Spring3.x企业应用开发实战(完整版) part1

    10.4.1 Spring通过单实例化Bean简化多线程问题 10.4.2 启动独立线程调用事务方法 10.5 联合军种作战的混乱 10.5.1 Spring事务管理器的应对 10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼...

    Spring框架(详细 一).md

    本篇博文适合零基础的同学:主要包括:spring介绍;...装配bean基于xml---实例化方式; bean种类; bean作用域; 生命周期; 属性注入--setter方法 p命名空间; sqel; 集合注入; 装配bean基于注解;

    Spring 中文API&开发文档.rar

    ◆容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一...

    spring-framework-3.1.0.RELEASE.zip

    容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一个...

    Spring.html

    ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则创建容器时就会创建里面bean的实例,如果是多例在获取使用时才会创建bean实例 ...

Global site tag (gtag.js) - Google Analytics