您的位置:首页 > 编程语言 > Java开发

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

2013-03-22 15:06 435 查看
/article/8597693.html

[java] view
plaincopy

<!-- 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配置文件结束 -->

对于

[java] view
plaincopy

org.springframework.web.context.ContextLoaderListener

可以参见
http://blog.csdn.net/seng3018/article/details/6758860 中的说明

一般spring上下文中xml默认叫applicationContext.xml

[java] view
plaincopy

<!-- 扫描注解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"/>

[java] view
plaincopy

<import resource="classpath*:com/avicit/resource/spring/spring-dao.xml"/>

这里导入了另一个配置文件,就是配置数据源的

[java] view
plaincopy

<?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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: