日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php框架 > 框架設計 > 【SSH三大框架】Spring基礎第一篇:搭建Spring環境、實例化Bean、管理Bean的作用域以及Bean的生命周期

【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方法是在容器關閉以后,調用此方法











生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 免费不卡视频 | 日韩av免费看 | 精品久久久精品 | 日本欧美在线 | 九九九久久国产免费 | 美女在线观看www | 日韩精品免费在线视频 | 欧美精品一区视频 | 欧美综合在线观看 | 男女网站在线观看 | 91精品久久久久久久99蜜桃 | 久久久久99精品国产片 | 亚洲综合99 | 成人av久久 | 久久99深爱久久99精品 | 激情五月综合 | 精品国产乱码一区二区三区 | 日韩欧美自拍偷拍 | 久久久在线免费观看 | 精品视频一区二区三区 | 国产精品一区二区不卡 | 午夜成人免费电影 | 日韩免费在线观看视频 | 99久久精品免费看蜜桃的推荐词 | 91免费福利视频 | 日韩a| 精品日产卡一卡二卡麻豆 | 久久亚洲线观看视频 | 精品久久精品久久 | 超碰在线| 91精品久久久久久久久99蜜臂 | 国内精品一区二区 | 91国产视频在线观看 | 国产福利一区二区三区在线播放 | 亚洲综合色一区 | 在线婷婷 | 91久久国产精品 | www免费| 亚洲在线看 | 色就是色欧美亚洲 | 久久成人免费视频 |