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配置文件结束 -->
获取方式:ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(
request.getSession.getServletContext() );
ac.getBean(xxx);
一般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>
- <!-- 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.youkuaiyun.com/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>