论坛首页 Java企业应用论坛

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

浏览 7213 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (16) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-06-04   最后修改:2010-06-04

一、基于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();
	}
 

 

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

Spring 事务管理
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics