国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

9、spring的bean基礎(chǔ)(1)

2019-11-08 02:28:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

9、sPRing的bean基礎(chǔ)(1)

本文主要講解以下幾個(gè)知識(shí)點(diǎn)

1、在spring中引用bean的例子2、注入值到bean屬性3、加載多個(gè)配置文件4、spring 內(nèi)部bean的示例5、spring bean的作用域

1、在spring中引用bean的例子

引用同一個(gè)配置文件下的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"> <bean id="HelloWorld" class="..."> <property name="outputGenerator" > <ref local="Custom"/> </property> </bean> <bean id="Custom" class="..." /></beans>

引用其他配置文件的bean

<!-- first.xml --> <bean id="HelloWorld" class="..."> <property name="outputGenerator" > <ref bean="Custom"/> </property> </bean> <!-- second.xml --> <bean id="Custom" class="..."/>

2、注入值到bean屬性

假設(shè)現(xiàn)在有一個(gè)bean類如下所示:

package com.main;public class HelloWorld{ private String name; private int state; //setter and getter methods //toString methods}

在Spring框架中有三種方式可以注入值到bean屬性 第一種:normal方式

<bean id="HelloWorld" class="com.main"> <property name="name"> <value>jack</value> </property> <property name="state"> <value>1</value> </property> </bean>

第二種:快捷方式

<bean id="HelloWorld" class="com.main"> <property name="name" value="jack"/> <property name="state" value="1"/> </bean>

第三種:P標(biāo)簽方式

注意:此時(shí)如果要使用p標(biāo)簽,必須要在spring xml配置文件中聲明 xmlns:p=”http://www.springframework.org/schema/p”

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="HelloWorld" class="com.main" p:name="jack" p:type="1" /></beans>

3、spring加載多個(gè)配置文件

背景說(shuō)明:在實(shí)際開發(fā)的項(xiàng)目中,bean配置文件非常多,因此需要一個(gè)配置文件倉(cāng)庫(kù)來(lái)統(tǒng)一管理和使用某個(gè)bean配置文件,將會(huì)減少很多查找工作。

假設(shè)項(xiàng)目中存在以下兩個(gè)bean配置文件 first.xml(路徑為classpath:/com/main/first/first.xml)

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="FirstBean" class="com.main.first" /></beans>

second.xml(路徑為classpath:/com/main/second/second.xml)

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="SecondBean" class="com.main.first" /></beans>

把上述兩個(gè)bean配置文件組織在一個(gè)xml文件中,暫且命名為Spring-Module.xml

那么Spring-Module.xml的配置如下:

<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"> <import resource="com/main/first/first.xml"/> <import resource="com/main/second/second.xml"/></beans>

至此就完成了加載多個(gè)配置文件的工作; 從Spring-Module.xml中獲取bean的方法如下

applicationContext context = new ClassPathXmlApplicationContext(Spring-Module.xml);Object object = (Object)context.getBean("value");

4、內(nèi)部bean實(shí)例

說(shuō)明:在一些場(chǎng)景中,我們或許需要為某一個(gè)bean的屬性設(shè)定一個(gè)內(nèi)部bean(即只允許自己使用),通常有以下兩種方式可以實(shí)現(xiàn);

第一種:通過(guò)setter方法構(gòu)造內(nèi)部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"> <bean id="Customer" class="..."> <property name="person"> <!--聲明內(nèi)部bean --> <bean class="..."> <!-- 內(nèi)部bean的屬性 --> <property name="name" value="yiibai" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </property> </bean></beans>

第二種:通過(guò)構(gòu)造方法構(gòu)造內(nèi)部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"> <bean id="Customer" class="..."> <constructor-arg> <!--聲明內(nèi)部bean --> <bean class="..."> <!-- 內(nèi)部bean的屬性 --> <property name="name" value="yiibai" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </constructor-arg> </bean></beans>

5、spring bean的作用域

spring bean的作用域的作用就在于:告訴spring容器應(yīng)該選擇哪一種類型的bean實(shí)例返回給調(diào)用者。支持的作用域有以下五種;

單例-每次都返回同一個(gè)bean實(shí)例原型-每一次都返回一個(gè)新的實(shí)例請(qǐng)求-返回每個(gè)HTTP請(qǐng)求的一個(gè)Bean實(shí)例會(huì)話-返回每個(gè)HTTP會(huì)話的一個(gè)bean實(shí)例全局會(huì)話-返回全局HTTP會(huì)話的一個(gè)bean實(shí)例

單例(在bean中不設(shè)置scope屬性)

<bean id="normalBean" class="..."/>

原型

<bean id="normalBean" class="..." scope="prototype" />

使用注解來(lái)使用作用域

package com.main;@Service@Scope("prototype")public class{ //property //methods}

要使用注解形式的作用域,必須要啟用組件掃描

<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.main" /></beans>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 祁门县| 莱州市| 衡阳县| 德清县| 香河县| 竹山县| 理塘县| 徐汇区| 同江市| 安泽县| 闻喜县| 天气| 建阳市| 万安县| 磴口县| 霍邱县| 晋城| 旺苍县| 五河县| 扶风县| 汝州市| 耿马| 湘乡市| 海原县| 衢州市| 河南省| 黎川县| 靖江市| 红河县| 佛教| 永泰县| 平远县| 雷山县| 图木舒克市| 清河县| 北京市| 周口市| 内丘县| 星子县| 汉沽区| 南川市|