spring bean 實例化的方式
1.默認情況下是調用bean對象的默認構造函數來示例話bean
2.調用工廠的靜態方法來實例化bean
<bean id="bean實例name" class="工廠類" factory-method="工廠的靜態方法"></bean>
3.調用工廠方法來實例化bean
<bean id="工廠實例name" class="工廠類" class="工廠類名">
<bean id="bean實例name" factory-name="工廠實例name",factory-method="工廠的非靜態方法">
spring bean 的作用域 (可以使用scope="singleton/prototype/request/session/global session"來配置)
1.默認作用域(singleton)情況下:
bean的實例化是在容器實例化(被主動申請加載)時就實例化
將lazy-init="true" (延遲初始化) 不會再容器實例化實例化bean (不建議使用)
用init-method="methodName" 可以指定bean被實例化時調用的方法
用destory-method="methodName" 可以制定容器調用close方法后bean執行的方法
2.配置prototype(原型)作用域:
bean的實例化是在調用getBean方法時被實例化的
每次調用一個getBean方法都回返回一個新的對象
Spring Dependency Injection(Spring的依賴注入)
1.使用映射的方式注入
<bean id="personDao" class="接口實現類路徑"></bean>
<bean id="persionService" class="接口實現的類路徑">
<property name="personDao" ref="personDao"></property> <!--反射賦值-->
<property name="name" value="tom test!!!!!!!!"></property> <!--為基本類型賦值-->
<property name="name" value="100"></property> <!--為基本類型賦值-->
</bean>
2.使用內部bean 的方式注入
<bean id="persionService" class="實現的類路徑">
<property name="persionDao">
<bean class="dao實現路徑"/>
</property>
</bean>
3.集合類型的注入
<bean id="personService" class="實現類路徑">
<property name="sets">
<set> <!--此處可以為list-->
<value>一個value</value>
<value>兩個value</value>
</set>
<map>
<entry key="key-1" value="value-1"/>
<entry key="key-2" value="value-2"/>
<entry key="key-3" value="value-3"/>
</map>
</property>
</bean>
4.使用構造器參數注入(調用特定的構造函數)
如果PersonServiceBean中的有構造函數PersonServiceBean(PersonDao personDao,String name){}
<bean id="personDao11111111" class="類實現路徑"><bean>
<bean id="personService" class="類實現路徑">
<constructor-arg index="0" type="自定義type類的路徑" ref="personDao111111">
<constructor-arg index="1" type="基本類型可以省去......" value="111111111">
</bean>
5.使用Field注入(用于注解的方式)
屬性的值注入
@Resource private PersonDao personDao; /* 先找到 容器中的 bean的對應名稱進行匹配,若無,找類型匹配 */
屬性的set方法注入
@Resource
public void setXXX(Object object){}
Spring 自動掃描和管理(掃描的方式去自動管理組建)
引入類配置<context:component-scan base-package>
@Service @ Resposity
通過class 類名首字母小寫 @Service("對象名")來獲得,也可以 @scope("prototype") 來設置初始化類的屬性
可以在方法名上面@PostConstruct 可以制定初始化方法
可以在方法名上面@PreDestory 可以在bean實例被摧毀之前執行的方法。
Spring和Struts2的整合
1.找到Struts2-Spring-plugin-.XX.jar包和Spring.jar包 加入到web項目的lib包下
2.配置WEB-INF/web.xml文件,加入與struts整合的監聽器
<!--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
-->
<!-- 若applicationContext.xml文件不在WEB-INF 下則需要將將路徑放入上面參數中-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3.在struts.xml文件中加入spring常量(structs中對象的創建叫給spring)
<constant name="struts.objectFactory" value="spring"/>
<action class="loginAction" name="loginAction_*" method="{1}">
<result name="success" type="redirect">index.jsp</result>
</action>
4.創建applicationContext.xml文件,文件可以放在web-inf目錄下,這樣可以不用在1中加入context的配置
<?xml version="1.0" encoding="UTF-8"?>
<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="loginAction" class="xxxxxxxxxxxxxxxxxxxxxxx" scope="prototype"/>
注意:(1)struts2中需要加入prototype(原型)的配置,這樣才能滿足struts2為每次請求創建一個對象的服務方式
(2)此處的id 名需要與3中的class同名