1. 程式人生 > >【Spring】Spring配置檔案簡單解析

【Spring】Spring配置檔案簡單解析

一個標準的Spring配置檔案applicationContext.xml應該包含的基本組成部分如下:

0、框架配置

<?xml version="1.0" encoding="UTF-8"?>
<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"
    xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd"
>
<bean></bean> <bean></bean> <--後面可以有好多bean標籤--> </beans>

其中會有好多配置:

1、啟用配置

<context:annotation-config />
【概述】:啟用spring的bean;
【詳解】:<context:annotation-config/>的作用是向Spring容器註冊以下四個BeanPostProcessor:
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor –>
用於啟用那些已經在spring容器裡註冊過的bean(無論是通過xml的方式還是通過package sanning的方式)上面的註解。每次配置applicationContext.xml檔案的時候,可以直接複製到自己的spring配置檔案裡;

2、 掃描配置

<context:component-scan base-package="com.smf.platform,com.smf.pdms"/>
【概括】:掃面指定目錄,註冊建立javabean;
【詳解】: 除了具有<context:annotation-config>的功能之外,<context:component-scan>還可以在指定的package下掃描以及註冊Javabean ;說白了,就是掃描你指定專案資料夾目錄裡,含有@Controller,@Service,@RequestMapping等註解,並註冊建立JavaBean;@每次配置applicationContext.xml檔案的時候,該標籤可以直接複製到自己的spring配置檔案裡;

3、代理配置

代理

<aop:aspectj-autoproxy/>

【概括】:代理
【詳解】: 自動為spring容器中那些配置@aspectJ切面的bean建立代理,織入切面;在配置自己的applicationContext.xml檔案的時候,可以直接複製;

4、讀取外部配置檔案配置

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
</bean>

【概括】:讀取外部配置檔案;
【詳解】:這裡的jdbc.propertie是和src平級的conf資料夾內的配置檔案;

5、資料來源配置

資料來源就是你資料庫的一些連線資訊,基本配置如下,官網點我

    <bean id="abcDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="150"/>
        <property name="maxIdle" value="10"/>
        <property name="maxWait" value="6000"/>
        <property name="testOnBorrow" value="true"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="validationQuery" value="select 1 from dual"/>
    </bean>

這裡的${} 裡的內容是jdbc.proporities配置檔案裡的鍵值對中的key;
driverClassName:驅動型別名字,例如oracle.jdbc.driver.OracleDriver;
url:資料庫地址;
username:資料庫登入名;
password:資料庫登入密碼;
maxActive:資料庫連線池中最大活躍執行緒數目,0為不限制;
maxIdle:表示最大空閒數目;
maxWait:最大等待時間,單位毫秒;
defaultAutoCommit:是否自動提交;
testOnBorrow:
validationQuery:連線資料庫成功後,用一個最簡單的句子驗證查詢;
這裡寫圖片描述

這裡的資料來源配置還有另一種寫法,那就是:

<bean id="abcDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName">
             <value>${jdbc.driverClassName}</value>
        </property>
        <property name="url">
             <value>${jdbc.url}</value>
        </property>
        <property name="username">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
        <property name="maxActive">
            <value>150</value>
        </property>
        <property name="maxIdle">
            <value>10</value>
        </property>
        <property name="maxWait">
            <value>6000</value>
        </property>
        <property name="testOnBorrow">
            <value>true</value>
        </property>
        <property name="defaultAutoCommit">
            <value>true</value>
        </property>
        <property name="validationQuery">
            <value>select 1 from dual</value>
        </property>
    </bean>

6、Mybatis配置資訊

    <!-- MyBatis配置開始 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="testDataSource" />
        <property name="configLocation" value="classpath:mybatis.xml" />
    </bean>

    <!-- 自動掃描mapper介面 -->
    <bean name="testdao" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.**.dao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

    <bean id="testTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="testDataSource" />
    </bean>
    <!-- MyBatis配置結束 -->

7、JPA配置

JPA是Java Persistence API的簡稱,中文名Java持久層API,是JDK 5.0註解或XML描述物件-關係表的對映關係,並將執行期的實體物件持久化到資料庫中。持久層配置檔案一般為persistence.xml;持久層一般會對各個型別資料庫進行配置,Oracle,SQL Server,MySQL方言(Dialect)的配置

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="abcDataSource" />
        <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
        <property name="persistenceUnitName" value="persistenceUnit" />
        <property name="jpaDialect" ref="jpaDialect"/>
    </bean>

    <bean id="jpaDialect" class="com.smf.platform.framework.dialect.AbcHibernateJpaDialect" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory">
            <ref bean="entityManagerFactory" />
        </property>
        <property name="dataSource" ref="smfDataSource" />
    </bean> 

8、事務配置

<bean name="transactionAttributesSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
        <property name="properties">
            <props>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="import*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="export*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="submit*">PROPAGATION_REQUIRED</prop>
                <prop key="create*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="modify*">PROPAGATION_REQUIRED</prop>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
                <prop key="remove*">PROPAGATION_REQUIRED</prop>
                <prop key="restore*">PROPAGATION_REQUIRED</prop>
                <prop key="do*">PROPAGATION_REQUIRED</prop>
                <prop key="execute*">PROPAGATION_REQUIRED</prop>
                <prop key="debug">PROPAGATION_REQUIRES_NEW</prop>
                <prop key="info">PROPAGATION_REQUIRES_NEW</prop>
                <prop key="error">PROPAGATION_REQUIRES_NEW</prop>
                <prop key="fatal">PROPAGATION_REQUIRES_NEW</prop>
                <prop key="warn">PROPAGATION_REQUIRES_NEW</prop>
            </props>
        </property>
    </bean>

9、事務管理

    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
            <ref bean="transactionManager"/>
        </property>
        <property name="transactionAttributeSource">
            <ref bean="transactionAttributesSource"/>
        </property>
    </bean>

    <bean id="autoTxProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="interceptorNames">
            <list>
                <idref bean="transactionInterceptor"/>
            </list>
        </property>
        <property name="beanNames">  
              <value>*Service,*Provider</value>
        </property>  
    </bean>

    <bean id="simpleJdbcDaoSupport"
        class="org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport">
        <property name="dataSource" ref="abcDataSource" />
    </bean>

    <bean id="dao" class="com.dao.impl.GenericDaoImpl">
    </bean>

     <bean id="template" class="com.dao.impl.GenericTemplateImpl">
        <property name="daoSupport" ref="simpleJdbcDaoSupport"/>
    </bean>

10、pfLogService配置

<bean id="pfLogService" class="com.smf.platform.log.service.impl.PFLogServiceImpl">
        <property name="appender">
            <bean class="com.smf.platform.log.appender.LogPersistAppender">
                <property name="logOwnerProvider">
                    <bean class="com.smf.platform.system.service.impl.LogOwnerProviderImpl"></bean>
                </property>
            </bean>
        </property>
    </bean>

11、其它配置

    <bean id="springConfigTool" class="com.smf.platform.framework.SpringConfigTool"></bean>

    <bean id="systemConfig" class="com.smf.platform.framework.SystemConfig"></bean>