您的位置:首页 > 编程语言 > Java开发

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

2010-06-04 18:49 519 查看

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

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: