【SSH三大框架】Spring基礎(chǔ)第一篇:搭建Spring環(huán)境、實(shí)例化Bean、管理Bean的作用域以及Bean的生命周期
來源:程序員人生 發(fā)布時(shí)間:2015-01-07 08:11:20 閱讀次數(shù):3790次
1、搭建Spring環(huán)境:
在lib目錄下引入jar包,然后add to path,這就不過量說了。
2、實(shí)例化Bean的3種方式:
首先,我們先寫兩個(gè)java類:
接口類:
public interface PersonService {
public abstract void save();
}
實(shí)現(xiàn)類:
public class PersonServiceBean implements PersonService {
@Override
public void save(){
System.out.println("我是save()方法");
}
}
再寫1個(gè)測(cè)試方法:
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、使用類構(gòu)造器實(shí)例化Bean
如果我們使用類構(gòu)造器實(shí)例化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是這個(gè)bean的唯1標(biāo)識(shí),class是這個(gè)bean指向的路徑。
我們運(yùn)行測(cè)試類中的instanceSpring方法,可以在控制臺(tái)打印出:
我是save()方法
2、使用靜態(tài)工廠實(shí)例化Bean
首先,我們建立1個(gè)工廠類,其中有個(gè)方法是靜態(tài)的:
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是這個(gè)bean的唯1標(biāo)識(shí),class表明指向的類,factory-method表明使用工廠方法中的某個(gè)方法。
我們運(yùn)行測(cè)試類中的instanceSpring方法,可以在控制臺(tái)打印出:
我是save()方法
3、使用實(shí)例工廠的方法實(shí)例化Bean
使用實(shí)例工廠需要配置兩個(gè)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>
可以看到,我們配置了兩個(gè)bean,第2個(gè)bean通過factory-bean援用了第1個(gè)bean,并調(diào)用factory-method
我們看1下PersonServiceBeanFactory.java:
public class PersonServiceBeanFactory {
public PersonServiceBean createPersonServiceBean(){ //注意,這里相對(duì)靜態(tài)工廠少了個(gè)static
return new PersonServiceBean();
}
}
有1點(diǎn)需要注意的是,這里被調(diào)用的bean所指向的類中要調(diào)用的方法不能是靜態(tài)的,由于這不是靜態(tài)工廠實(shí)例化。
我們運(yùn)行測(cè)試類中的instanceSpring方法,可以在控制臺(tái)打印出:
我是save()方法
3、管理Bean的作用域
在Spring的Bean中有1個(gè)屬性scope是用來配置Spring Bean的作用域,它標(biāo)識(shí)Bean的作用域
在Spring 2.0以后默許有5種類型的Bean:
singleton、prototype、request、session、global session(application)
其中,后3種是用于WEB中的Bean對(duì)象,這里只介紹前兩種。
PS:需要注意的1點(diǎn)就是,如果Bean沒有設(shè)置作用域的時(shí)候,默許是singleton類型
1、 .singleton
當(dāng)1個(gè)Bean的作用域設(shè)置為.singleton,Srping IOC容器中僅僅會(huì)存在1個(gè)同享的bean實(shí)例。
在所有對(duì)bean的要求中,只要要求的id與bean的id相同,則他們返回的實(shí)例都是同1個(gè)。
即當(dāng)設(shè)置為singleton的時(shí)候,容器只會(huì)創(chuàng)建唯1的1個(gè)實(shí)例。這個(gè)單1實(shí)例會(huì)被存儲(chǔ)到單例緩存(singleton cache)中,并且所有針對(duì)該bean的后續(xù)要求和援用都將返回被緩存的對(duì)象實(shí)例。
2、.prototype
當(dāng)bean的作用域設(shè)置為prototype的時(shí)候,在每次要求bean的時(shí)候,Spring IOC容器都會(huì)創(chuàng)建1個(gè)新的Bean實(shí)例。
4、Bean的作用域
1、當(dāng)Bean的作用域是單例(scope="singleton"或沒有scope屬性)的時(shí)候
當(dāng)容器實(shí)例化的時(shí)候,Bean也同時(shí)實(shí)例化了。
另外,如果當(dāng)bean的lazy-init屬性設(shè)置為true的時(shí)候,會(huì)延遲初始化,即在調(diào)用getBean()方法的時(shí)候初始化Bean
2、當(dāng)Bean的作用域是(scope="prototype")的時(shí)候
當(dāng)調(diào)用getBean()方法的時(shí)候,Bean才會(huì)被實(shí)例化。
對(duì)所有的Bean實(shí)例有幾個(gè)屬性,init-method方法和destroy-method方法。
init-method方法是在實(shí)例化Bean以后,調(diào)用此方法(這是通過IOC容器反射技術(shù)實(shí)現(xiàn)的)
destroy-method方法是在容器關(guān)閉以后,調(diào)用此方法
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)