【SSH三大框架】Spring基礎第一篇:搭建Spring環境、實例化Bean、管理Bean的作用域以及Bean的生命周期
來源:程序員人生 發布時間:2015-01-07 08:11:20 閱讀次數:3802次
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方法是在容器關閉以后,調用此方法
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈