今天我給大家詳細解釋一下Spring的applicationContext.xml檔案。Ok,還是通過代碼加註釋的方式為大家示範;
以下是詳解Spring的applicationContext.xml檔案代碼:
1.<!-- 標頭檔,主要注意一下編碼 -->
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> </beans>
2.<!--載入jdbc設定檔,建立資料來源--!>
<!-- 載入jdbc.properties設定檔 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- c3p0資料來源配置方式--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"p:driverClass="${jdbc.driverClassName}"p:jdbcUrl="${jdbc.url}"p:user="${jdbc.username}"p:password="${jdbc.password}"p:maxPoolSize="${jdbc.maxPoolSize}"p:minPoolSize="${jdbc.minPoolSize}"p:initialPoolSize="${jdbc.initialPoolSize}"p:maxIdleTime="${jdbc.maxIdleTime}"p:acquireIncrement="${jdbc.acquireIncrement}"p:maxStatements="${jdbc.maxStatements}"p:idleConnectionTestPeriod="${jdbc.idleConnectionTestPeriod}" />
3.<!-- 定義Hibernate會話工廠 ,並注入資料來源執行個體dataSource--!>
<bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" ><property name="dataSource" ref="dataSource"></property><property name="hibernateProperties"> <props> <!--hibernate.dialect 只是Hibernate使用的資料庫方言,就是要用Hibernate串連那種類型的資料庫伺服器。--> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!--是否在後台顯示Hibernate用到的SQL語句,開發時設定為true。項目部署後可以設定為false,提高運行效率--> <prop key="hibernate.show_sql">true</prop> <!--jdbc.batch_size是指Hibernate批量插入,刪除和更新時每次操作的記錄數。BatchSize越大,大量操作的向資料庫發送Sql的次數越少, 速度就越快,同樣耗用記憶體就越大--> <prop key="hibernate.jdbc.batch_size">20</prop> <!--jdbc.fetch_size是指Hibernate每次從資料庫中取出並放到JDBC的Statement中的記錄條數。 FetchSize設的越大,讀資料庫的次數越少,速度越快,Fetch Size越小,讀資料庫的次數越多,速度越慢--><prop key="hibernate.jdbc.fetch_size">50</prop><!-- 如果改為thread將不能開啟事務 --><prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> <!-- 是否自動建立資料庫表 他主要有一下幾個值: validate:當sessionFactory建立時,自動驗證或者schema定義匯入資料庫。 create:每次啟動都drop掉原來的schema,建立新的。 create-drop:當sessionFactory明確關閉時,drop掉schema。 update(常用):如果沒有schema就建立,有就更新。 --> <prop key="hibernate.hbm2ddl.auto">update</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.temp.use_jdbc_metadata_defaults ">false</prop></props><!-- 包級掃描 hibernate實體類 --> </property><property name="packagesToScan"><list> <value>com.b505.bean</value></list> </property> </bean>
4.<!--start spring集中式聲明(事務的配置)--!>
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="saveUser*" /> <tx:method name="get*"/> <tx:method name="find*"/> <tx:method name="update*"/> <tx:method name="findUsers*" /> <tx:method name="delete*"/> <!--hibernate4必須配置為開啟事務 否則 getCurrentSession()擷取不到session --> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="productServiceMethods" expression="execution(* com.b505.service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> </aop:config>
上面這段代碼的意思是:
意思是這個事物advice的管理者為transactionManager,你從設定檔中應該能夠找到一個ID為transactionManager的bean,而這個建議中規定了save方法的傳輸方式為required,也就是說沒有sessionfactory的時候會自動建立,有的時候就不會建立了。當然了這隻是規定了一個advice,你還需要指定一個aop:pointcut去引用他,例如<aop:config> <aop:pointcut id="productServiceMethods" expression="execution(public * x.y..*.*(..))" /> <aop:advisor pointcut-ref="productServiceMethods" advice-ref="txAdvice" /> </aop:config>這樣這個advice就被聯絡到了productServiceMethods這個pointcut上了
5.<!-- 交易管理,與hibernate相關聯 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
6.<!-- 配置掃描註解,不掃描@Controller註解 -->
<context:component-scan base-package="com.b505"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
詳解:<context:component-scan base-package="com.b505"> </context:component-scan>
Spring 容器初始化的時候,會掃描com.b505下 標有 (@Component,@Service,@Repository) 註解的 類 納入spring容器管理
然而 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>的作用卻是不掃描標有@Controller註解 ,標有@Controller註解的類將在spring mvc的設定檔中進行掃描;
在類上 ,使用以下註解,實現bean 的聲明
@Component 泛指組件,當組件不好歸類的時候,我們可以使用這個註解進行標註。
@Service 用於標註業務層組件
@Controller 用於標註控制層組件(如srping mvc的controller,struts中的action)
@Repository 用於標註資料訪問組件,即DAO組件
樣本:
@Controller
@RequestMapping(value = "/test")
public class TestController {
}
=====================================================================================================
在類的成員變數上,使用以下註解,實現屬性的自動裝配
@Autowired : 按類 的 類型進行裝配
@Resource (推薦) : 1 如果同時指定了name和type,則從spring上下文中找到唯一匹配的bean進行裝配,找不到則拋出異常
2. 如果指定了name,則從上下文中尋找名稱(id)匹配的bean進行裝配,找不到則拋出異常
3.如果指定了type,則從上下文中找到類型匹配的唯一bean進行裝配,找不到或者找到多個,都會拋出異常
4.如果既沒有指定name,又沒有指定type,則自動按照byName方式進行裝配;如果沒有匹配,則回退為一個原始類型進行匹配,如果匹配則自動裝配;
@Resource註解在欄位上,這樣就不用寫setter方法了,並且這個註解是屬於J2EE的,減少了與spring的耦合。
樣本:
@Resource
private TestServiceImpl testServiceImpl;