Spring的spring.xml文檔的配置
最近在寫Spring的配置文件時,發現Spring文檔的配置其實沒必要那么繁瑣記憶,網上的很多文章都寫得很繁瑣,如果所有的東西按照路徑去查找,可以很快的幫我們完成文檔的配置,根本不用我們去記憶,同時配置項也不用那么繁瑣,博主按照自己的思路整理出自己的一套文檔的配置順序,算是自己的小心得,僅供參考。
首先我們需要導入如下jar包:(至於導入的路徑,會在下面為大家介紹)
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
這些jar的路徑可以在Spring的官網進行配置,路徑如下:
Spring官網——PROJECTS——SPRING FRAMEWORK(第三個圖案)——Spring Framework中的4.3.8版本 reference
然后就是官方的詳細介紹了
首先打開搜索,快捷鍵位Ctrl+F,然后通過“<?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.xsd">
</beans>
然后按照如下步驟進行配置:
注:這其中我們分別會用到<tx></tx> <aop></aop> <context></context>三個標簽,所以將上面的步驟補充如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
<!-- 下面的tx aop context三者可以在Spring的官網中看到,但是也可以結合上面的來寫,只是部分名字不同,分別用相應的名字代替即可 -->
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
然后在按照如下步驟進行
<!-- 支持Spring的注解方式 -->
<context:annotation-config></context:annotation-config>
<!-- 如果有context:component-scan,則不需要context:annotation-config -->
<context:component-scan base-package="com.dfys.spring.hibernate.dao.impl(這個是自己的包名)"></context:component-scan>
<!-- 1、配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean(可在Maven Dependencies目錄下spring-orm\4.3.8.RELEASE.jar中進行查看)">
<!-- 注意:!!!!!一下兩個屬性的名字一定要通過上面 sessionFactory的class類點進去查看,要保證和里面的名字相同,否則會造成其它錯誤-->
<!-- 配置連接連接池 -->
<property name="dataSource" ref="datasource"></property>
<property name="mappingResources">
<!-- 因為mappingResources是一個數組,使用list標簽 -->
<list>
<value>com/dfys/spring/bean/TbUser.hbm.xml</value>
<value>com/dfys/spring/bean/TbAccount.hbm.xml</value>
<value>com/dfys/spring/bean/TbBaojian.hbm.xml</value>
<value>com/dfys/spring/bean/TbImage.hbm.xml</value>
<value>com/dfys/spring/bean/TbTaocan.hbm.xml</value>
<value>com/dfys/spring/bean/TbHotel.hbm.xml</value>
<value>com/dfys/spring/bean/TbOrder.hbm.xml</value>
<value>com/dfys/spring/bean/TbShoppingCart.hbm.xml</value>
</list>
</property>
<!-- 此處多增加一個屬性,便於我們在測試的時候可以通過sql語句來查看是否執行,以及狀態是否為懶加載,在事務發生錯誤的時候,需要回滾時,可以查看是否執行語句回滾,都可以有一個直觀的感受 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<!-- 如果Session交給Spring進行管理,則不需要此句 -->
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->(這句話寫出來時注釋掉的,不能加上去,因為Spring已經幫我們完成了配置,我們不需要多此一舉)
</props>
</property>
</bean>
<!-- 2、配置連接池 -->
<bean id="datasource" class="org.apache.commons.dbcp2.BasicDataSource"(可在Maven Dependencies目錄下commons-dbcp2\2.1.1.jar中進行查看)>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mydb?useSSL=true"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 3、sessionFactory 創建session對象,用來連接數據庫,所以增設一個property屬性,然后再來連接我們所獲取的表格對應的bean對象,所以在增加一個property屬性-->
<!-- 然后回到1中進行補充完成 -->
<!-- 4、事務管理器 -->
<!-- 配置Spring的事務管理的類 -->
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"(可在Maven Dependencies目錄下spring-orm\4.3.8.RELEASE.jar中進行查看)>
<!-- 事務是通過Session進行開啟和提交,Session需要使用Session Factory創建,所以需要sessionFactory對象 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5、事務管理的具體方法的說明和配置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 對所有以get開頭方法進行事務優化為只讀 -->
<!-- <tx:method name="get*" read-only="true"/> -->
<!-- 對所有的方法進行事務的開啟和提交 -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 6、使用AOP將事務的管理HIbernateTransactionManager植入到dao層 -->
<aop:config>
<aop:pointcut id="dao_point_cut" expression="execution(* com.dfys.spring.hibernate.dao.*.*(..))"/>(這里的*都代表這泛指,括號中的“..”也泛指參數,有或者沒有都可以)
<!-- 配置事務的開始、提交、回滾的一個建議,,, 類似aop:before aop:after -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="dao_point_cut"/>
</aop:config>
</beans>
配置完成。
后記:關於配置,主要是按照自己的思路配置即可,方便自己的記憶優先,整理出自己的思路就行了。
現在Spring的封裝已經越來越偏向注解的形式,因為注解比較清晰明了,而且注解的功能已經越來越強大,博主會在后續就這個注解的方式給大家進行詳解,使用后你會覺得注解真的很方便,而且有關注解的方式網上也很少,希望后續對大家有所幫助。