本文主要講解以下幾個(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>新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注