国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php框架 > 框架设计 > 【SSH三大框架】Spring基础第一篇:搭建Spring环境、实例化Bean、管理Bean的作用域以及Bean的生命周期

【SSH三大框架】Spring基础第一篇:搭建Spring环境、实例化Bean、管理Bean的作用域以及Bean的生命周期

来源:程序员人生   发布时间:2015-01-07 08:11:20 阅读次数:3499次

1、搭建Spring环境:

在lib目录下引入jar包,然后add to path,这就不过量说了。

2、实例化Bean的3种方式:

首先,我们先写两个java类:

接口类:

public interface PersonService { public abstract void save(); }

实现类:

public class PersonServiceBean implements PersonService { @Override public void save(){ System.out.println("我是save()方法"); } }

再写1个测试方法:

public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void instanceSpring() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)ctx.getBean("personService"); personService.save(); } }

1、使用类构造器实例化Bean

如果我们使用类构造器实例化Bean,则我们需要配置1下beans.xml文件:

<?xml version="1.0" encoding="UTF⑻"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑵.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context⑵.5.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean> </beans>
id是这个bean的唯1标识,class是这个bean指向的路径。

我们运行测试类中的instanceSpring方法,可以在控制台打印出:

我是save()方法

2、使用静态工厂实例化Bean

首先,我们建立1个工厂类,其中有个方法是静态的:

public class PersonServiceBeanFactory { public static PersonServiceBean createPersonServiceBean(){ return new PersonServiceBean(); } }
然后,我们需要配置1下beans.xml文件:

<?xml version="1.0" encoding="UTF⑻"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑵.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context⑵.5.xsd"> <bean id="personServiceFactory" class="cn.itcast.service.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBean"></bean> </beans>
id是这个bean的唯1标识,class表明指向的类,factory-method表明使用工厂方法中的某个方法。

我们运行测试类中的instanceSpring方法,可以在控制台打印出:

我是save()方法

3、使用实例工厂的方法实例化Bean

使用实例工厂需要配置两个bean:

我们配置1下beans.xml文件:

<?xml version="1.0" encoding="UTF⑻"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑵.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context⑵.5.xsd"> <bean id="personServiceFactory" class="cn.itcast.service.impl.PersonServiceBeanFactory"></bean> <bean id="personService" factory-bean="personServiceFactory" factory-method="createPersonServiceBean"></bean> </beans>
可以看到,我们配置了两个bean,第2个bean通过factory-bean援用了第1个bean,并调用factory-method

我们看1下PersonServiceBeanFactory.java:

public class PersonServiceBeanFactory { public PersonServiceBean createPersonServiceBean(){ //注意,这里相对静态工厂少了个static return new PersonServiceBean(); } }
有1点需要注意的是,这里被调用的bean所指向的类中要调用的方法不能是静态的,由于这不是静态工厂实例化。

我们运行测试类中的instanceSpring方法,可以在控制台打印出:

我是save()方法

3、管理Bean的作用域

在Spring的Bean中有1个属性scope是用来配置Spring Bean的作用域,它标识Bean的作用域

在Spring 2.0以后默许有5种类型的Bean:

singleton、prototype、request、session、global session(application)

其中,后3种是用于WEB中的Bean对象,这里只介绍前两种。

PS:需要注意的1点就是,如果Bean没有设置作用域的时候,默许是singleton类型

1、 .singleton

当1个Bean的作用域设置为.singleton,Srping IOC容器中仅仅会存在1个同享的bean实例。

在所有对bean的要求中,只要要求的id与bean的id相同,则他们返回的实例都是同1个。

即当设置为singleton的时候,容器只会创建唯1的1个实例。这个单1实例会被存储到单例缓存(singleton cache)中,并且所有针对该bean的后续要求和援用都将返回被缓存的对象实例。

2、.prototype

当bean的作用域设置为prototype的时候,在每次要求bean的时候,Spring IOC容器都会创建1个新的Bean实例。

4、Bean的作用域

1、当Bean的作用域是单例(scope="singleton"或没有scope属性)的时候

当容器实例化的时候,Bean也同时实例化了。

另外,如果当bean的lazy-init属性设置为true的时候,会延迟初始化,即在调用getBean()方法的时候初始化Bean

2、当Bean的作用域是(scope="prototype")的时候

当调用getBean()方法的时候,Bean才会被实例化。


对所有的Bean实例有几个属性,init-method方法和destroy-method方法。

init-method方法是在实例化Bean以后,调用此方法(这是通过IOC容器反射技术实现的)

destroy-method方法是在容器关闭以后,调用此方法











生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生