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();
	}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值