1. 程式人生 > >spring上下文在web.xml中的配置

spring上下文在web.xml中的配置

  <!-- Spring配置檔案開始  -->    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:com/avicit/resource/spring/spring-base.xml</param-value>
    </context-param>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- Spring配置檔案結束 -->

對於

org.springframework.web.context.ContextLoaderListener
可以參見

http://blog.csdn.net/seng3018/article/details/6758860 中的說明

一般spring上下文中xml預設叫applicationContext.xml

<!-- 掃描註解Bean -->
    <context:component-scan base-package="com.avicit">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
            	<value>classpath*:com/avicit/resource/jdbc/jdbc.properties</value>
            	<value>classpath*:com/avicit/resource/hibernate/hibernate.properties</value>
            </list>
        </property>
    </bean>

 
    <!-- 國際化的訊息資原始檔 -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <!-- 在web環境中一定要定位到classpath 否則預設到當前web應用下找  -->
                <value>classpath:com/avicit/resource/message/messages</value>
            </list>
        </property>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="cacheSeconds" value="60"/>
    </bean>
    
    <import resource="classpath*:com/avicit/resource/spring/spring-dao.xml"/>
    <import resource="classpath*:com/avicit/resource/spring/spring-dao.xml"/>
這裡匯入了另一個配置檔案,就是配置資料來源的
<?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: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"


	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


	<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
		<property name="alias" value="proxoolDataSource" />
		<property name="driver" value="${jdbc.driver}" />
		<property name="driverUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maximumConnectionCount" value="${jdbc.maximum.connection.count}" />
		<property name="minimumConnectionCount" value="${jdbc.minimum.connection.count}" />
		<property name="statistics" value="${jdbc.statistics}" />
		<property name="simultaneousBuildThrottle" value="${jdbc.simultaneous.build.throttle}" />
	</bean>


	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan" value="com.avicit" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>
				<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
				<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
				<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
				<prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use_reflection_optimizer}</prop>


				<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
				<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
				<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
				<prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop>
				<prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop>
			</props>
		</property>
	</bean>


	<bean id="lookupResolver"
		class="com.avicit.framework.support.matchrule.context.HibernateMatchRuleResolver">
		<property name="packagesToScan" value="com.avicit.fes.*" />
	</bean>


	<!-- 開啟AOP監聽 只對當前配置檔案有效 -->
	<aop:aspectj-autoproxy expose-proxy="true" />


	<!-- 開啟註解事務 只對當前配置檔案有效 -->
	<tx:annotation-driven transaction-manager="txManager" />


	<bean id="txManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>


	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="merge*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="put*" propagation="REQUIRED" />
			<tx:method name="use*" propagation="REQUIRED" />
			<!--hibernate4必須配置為開啟事務 否則 getCurrentSession()獲取不到 -->
			<tx:method name="get*" propagation="REQUIRED" read-only="true" />
			<tx:method name="count*" propagation="REQUIRED" read-only="true" />
			<tx:method name="find*" propagation="REQUIRED" read-only="true" />
			<tx:method name="list*" propagation="REQUIRED" read-only="true" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<aop:config expose-proxy="true">
		<!-- 只對業務邏輯層實施事務 -->
		<aop:pointcut id="txPointcut"
			expression="execution(* com.avicit..service..*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
	</aop:config>
</beans>


(個人微訊號)  (技術公眾號)