搭建 ssh 或 ssm 項目,spring 核心配置文件中的主要配置選項

(1) spring的核心配置文件中的配置
spring 的核心配置文件的名字叫做 applicationContext.xml,
後期也可以通過配置文件中的配置修改名稱,在 WEB-INF/web.xml 中進行如下配置:
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>

 

(2) 核心配置文件中關於 dao 層的配置
首先準備 jdbc.properties 配置文件,最簡單的配置如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/testDB1?characterEncoding=utf-8
jdbc.username=root
jdbc.password=56789

然後加載在覈心配置文件中加載數據庫文件
<context:property-placeholder location="classpath:resource/jdbc.properties" />

 

(3) 配置數據庫連接池
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
</bean>

後期需要可以再其中添加多個屬性配置。

 

(4) spring 和 hibernate 或 mybatis 的整合,主要是整合 sessionFactory

與 hibernate 的整合:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
         <property name="dataSource">
              <ref bean="dataSource"/>
         </property>

          。。。
</bean>

 

  與 mybatis 的整合:
    <!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 加載mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean>

 

(5) 配置文件中關於事務的配置。
    <!-- 事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 數據源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>


配置通知:
<!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 傳播行爲 -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>

 

aop 切面配置 a-使用 表達式配置事務:
<!-- 切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* com.store.service.*.*(..))" />
    </aop:config>

配置文件中的 service 層的配置。掃描包下面所有的 service 層。
<context:component-scan base-package="com.abcd.service"/>

或 使用 aop 切面配置 b-使用 註解配置事務:

    <!-- 配置聲明式事務:在 Service 實現類或者 public 方法
                 上使用 @Transactional 註解,則此類或方法就會啓用事務機制 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    
    <!-- 開啓 aop 註解功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true" />
    
    <!-- 定義自動掃描 Spring 註解的範圍 -->
    <context:component-scan base-package="com" />

 


開啓注入 springMVC 註解的配置:
<mvc:annotation-driven />

 

(6) 在進行配置的時候所需要引入 beans 標籤的命名空間。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章