• spring经典配置


    1.annotation方式

    <?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"
    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">
    <context:annotation-config />
    <context:component-scan base-package="com.bjsxt" />

    <!--
    <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">


    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/spring" />
    <property name="username" value="root" />
    <property name="password" value="bjsxt" />
    </bean>
    -->

    <bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <value>classpath:jdbc.properties</value>
    </property>
    </bean>

    <bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
    value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sf"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!--
    <property name="annotatedClasses">
    <list>
    <value>com.bjsxt.model.User</value>
    <value>com.bjsxt.model.Log</value>
    </list>
    </property>
    -->
    <property name="packagesToScan">
    <list>
    <value>com.bjsxt.registration.model</value>

    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sf"></property>
    </bean>

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

    <aop:config>
    <aop:pointcut id="bussinessService"
    expression="execution(public * com.bjsxt.registration.service.*.*(..))" />
    <aop:advisor pointcut-ref="bussinessService"
    advice-ref="txAdvice" />
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="exists" read-only="true" />
    <tx:method name="add*" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice>

    </beans>

    2.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: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.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>classpath:i18n.properties</value>
    <value>classpath:DataConnection.properties</value>
    <value>classpath:cdsp.properties</value>
    <value>classpath:memcache.properties</value>
    <value>classpath:jta.properties</value>
    </list>
    </property>
    </bean>
    <!-- 数据源配置开始 -->
    <!-- 框架默认库 -->
    <bean id="dataSource"
    class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init"
    destroy-method="close">
    <property name="uniqueResourceName">
    <value>sourceA</value>
    </property>
    <property name="xaDataSourceClassName">
    <value>oracle.jdbc.xa.client.OracleXADataSource</value>
    </property>
    <property name="xaProperties">
    <props>
    <prop key="user">${jdbc.username}</prop>
    <prop key="password">${jdbc.password}</prop>
    <prop key="URL">${jdbc.url}</prop>
    </props>
    </property>
    <property name="maxPoolSize">
    <value>60</value>
    </property>
    <property name="testQuery">
    <value>SELECT 1 from dual</value>
    </property>
    </bean>

    <!-- 渠道库 -->
    <bean id="chnDataSource"
    class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init"
    destroy-method="close">
    <property name="uniqueResourceName">
    <value>chnDataSource</value>
    </property>
    <property name="xaDataSourceClassName">
    <value>oracle.jdbc.xa.client.OracleXADataSource</value>
    </property>
    <property name="xaProperties">
    <props>
    <prop key="user">${chn.username}</prop>
    <prop key="password">${chn.password}</prop>
    <prop key="URL">${chn.url}</prop>
    </props>
    </property>
    <property name="maxPoolSize">
    <value>60</value>
    </property>
    <property name="testQuery">
    <value>SELECT 1 from dual</value>
    </property>
    </bean>

    <!-- 多数据源配置结束 -->

    <bean id="default" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingLocations">
    <list>
    <value>classpath:hibernate/*.hbm.xml</value>
    <value>classpath:hibernate/*/*.hbm.xml</value>
    <value>classpath:hibernate/*/*/*.hbm.xml</value>
    <value>classpath:hibernate/*/*/*/*.hbm.xml</value>
    <!-- CDSP hibernate配置文件 -->
    <value>classpath:cdsp/hibernate/*.hbm.xml</value>
    <value>classpath:cdsp/hibernate/*/*.hbm.xml</value>
    <value>classpath:cdsp/hibernate/*/*/*.hbm.xml</value>
    <value>classpath:cdsp/hibernate/*/*/*/*.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">com.wnlja.prm.baselayer.hibernate.wnljaOracle9Dialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.jdbc.fetch_size">50</prop>
    <prop key="hibernate.jdbc.batch_size">25</prop>
    <prop key="hibernate.use_outer_join">true</prop>
    <!-- EhCache集成hibernate配置 -->
    <prop key="hibernate.cache.use_second_level_cache">true</prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
    </props>
    </property>
    </bean>

    <bean id="chnSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="chnDataSource"/>
    <property name="mappingDirectoryLocations">
    <list>
    <value>
    classpath:hibernate/*.xml,
    classpath:hibernate/*/*.xml,
    classpath:hibernate/s98D00/*/*/*.xml,
    </value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">com.wnlja.prm.baselayer.hibernate.wnljaOracle9Dialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.jdbc.fetch_size">50</prop>
    <prop key="hibernate.jdbc.batch_size">25</prop>
    <prop key="hibernate.use_outer_join">true</prop>
    </props>
    </property>
    </bean>
    <!-- bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref local="default" />
    </property>
    </bean-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="get*" read-only="true"/>
    <tx:method name="query*" read-only="true"/>
    <tx:method name="*"/>
    <tx:method name="checkUser" no-rollback-for="com.wnlja.prm.baselayer.exception.BusinessException"/>
    </tx:attributes>
    </tx:advice>
    <aop:config>
    <aop:pointcut id="rightManageServicePointcut" expression="execution(* com.wnlja.prm.baselayer.rightmanage.service.*.*(..))"/>
    <!-- 增加用户 -->
    <aop:pointcut id="addUserPointcut" expression="execution(* com.wnlja.prm.baselayer.rightmanage.service.RightManageService.addUser(..))"/>
    <aop:pointcut id="editUserPointcut" expression="execution(* com.wnlja.prm.baselayer.rightmanage.service.RightManageService.editUser(..))"/>
    <aop:advisor pointcut-ref="rightManageServicePointcut" advice-ref="txAdvice"/>
    <aop:advisor pointcut-ref="rightManageServicePointcut" advice-ref="logAfterAdvice"/>
    </aop:config>
    <!-- -------------------------------------------------------------------------------------------------------- -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
    <ref local="default" />
    </property>
    <!-- EHCache配置 -->
    <property name="cacheQueries">
    <value>true</value>
    </property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    </bean>
    <!-- --------------------------------------------------------------------------------------------------------- -->
    <bean id="atomikosTransactionManager"
    class="com.atomikos.icatch.jta.UserTransactionManager"
    init-method="init" destroy-method="close">
    <property name="forceShutdown">
    <value>true</value>
    </property>
    </bean>

    <bean id="atomikosUserTransaction"
    class="com.atomikos.icatch.jta.UserTransactionImp">
    <property name="transactionTimeout" value="300" />
    </bean>

    <bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager">
    <ref bean="atomikosTransactionManager" />
    </property>
    <property name="userTransaction">
    <ref bean="atomikosUserTransaction" />
    </property>
    </bean>
    <!-- ------------------------------------------------------------------------------------------------------------- -->
    <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
    <property name="transactionManager">
    <!-- <ref bean="transactionManager" /> -->
    <ref bean="transactionManager" />
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>
    <prop key="test*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>
    <!-- ------------------------------------------------------------------------------------------------------------- -->
    <bean id="SpringContextUtil" class="com.wnlja.prm.baselayer.spring.SpringContextUtil" />
    <!-- ------------------------------------------------------------------------------------------------------------- -->
    <!--
    <bean id="imdb" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="imdbDataSource"/>
    <property name="mappingDirectoryLocations">
    <list>
    </list>
    </property>
    <property name="hibernateProperties">
    <value>
    hibernate.dialect=com.wnlja.prm.baselayer.hibernate.wnljaIMDBDialect
    hibernate.show_sql=true
    </value>
    </property>
    </bean>
    -->
    <bean id="rightManageService" class="com.wnlja.prm.baselayer.rightmanage.service.RightManageServiceImpl">
    <property name="dao" ref="rightManageDao" />
    </bean>
    <bean id="rightManageDao" class="com.wnlja.prm.baselayer.rightmanage.dao.${dao.impl}.RightManageDaoImpl"/>

    <bean id="logQueryService" parent="baseTransactionProxy">
    <property name="target">
    <bean class="com.wnlja.prm.baselayer.rightmanage.service.LogQueryServiceImp">
    <!-- 默认查询的sql/hql -->
    <property name="sql">
    <value>
    </value>
    </property>
    <!-- 是否是Hql -->
    <property name="isHql">
    <value>false</value>
    </property>
    <!-- 是否去掉空条件 -->
    <property name="rmEmpty">
    <value>true</value>
    </property>
    <property name="dao" ref="rightManageDao" />
    </bean>
    </property>
    </bean>
    <bean id="DBConnection" parent="baseTransactionProxy">
    <property name="target">
    <bean class="com.wnlja.prm.baselayer.hibernate.DBConnectionImpl">
    <property name="sessionFactory" ref="default" />
    <property name="sessionFactoryMap" ref="sessionFactoryMap" />
    </bean>
    </property>
    </bean>
    <bean id="sessionFactoryMap" class="java.util.HashMap"><!-- 前置的sessionFactory -->
    <constructor-arg>
    <map>
    <entry><key><value>default</value></key><ref bean="default"/></entry>
    <entry><key><value>chn</value></key><ref bean="chnSessionFactory"/></entry>
    </map>
    </constructor-arg>
    </bean>

    <!-- baseDao -->
    <bean id="genericDao" class="com.wnlja.prm.baselayer.hibernate.genericdao.impl.GenericDao">
    </bean>

    <bean id="genericHBDao" class="com.wnlja.prm.baselayer.hibernate.genericdao.impl.GenericDaoHBImpl">
    <property name="sessionFactory">
    <ref bean="default" />
    </property>
    <property name="sessionFactoryMap" ref="sessionFactoryMap" />
    </bean>

    <bean id="genericJDBCDao" class="com.wnlja.prm.baselayer.hibernate.genericdao.impl.GenericJDBCDaoImpl">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="dataSourceMap" ref="dataSourceMap" />
    </bean>
    <bean id="dataSourceMap" class="java.util.HashMap">
    <constructor-arg>
    <map>
    <entry><key><value>default</value></key><ref bean="dataSource"/></entry>
    <entry><key><value>chn</value></key><ref bean="chnDataSource"/></entry>
    </map>
    </constructor-arg>
    </bean>

    <bean id="keyInfo" parent="baseTransactionProxy">
    <property name="target">
    <bean class="com.wnlja.prm.baselayer.keygen.KeyInfo">
    <property name="jdbcTemplate">
    <ref bean="jdbcTemplate"/>
    </property>
    </bean>
    </property>
    </bean>
    <!-- ---------------------------------------------------------------------------------------------------------- -->
    <!-- 添加spring 自动填写事务
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames">
    <value>*Service,*service</value>
    </property>
    <property name="interceptorNames">
    <list>
    <value>logAfterAdvisor</value>
    </list>
    </property>
    </bean> -->
    <!--
    <bean id="privilegeBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
    <ref local="privilegeBeforeAdvice" />
    </property>
    <property name="pattern">
    <value>.*</value>
    </property>
    </bean>
    -->
    <!--
    <bean id="privilegeBeforeAdvice" class="com.wnlja.prm.baselayer.spring.PrivilegeBeforeAdvice" />
    -->

    <!--
    <bean id="logAfterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
    <ref local="logAfterAdvice" />
    </property>
    <property name="pattern">
    <value>.*</value>
    </property>
    </bean>-->

    <bean id="logAfterAdvice" class="com.wnlja.prm.baselayer.spring.LogAfterAdvice" >
    <property name="logWriter">
    <ref local="logWriter" />
    </property>
    </bean>


    <bean id="logWriter" parent="baseTransactionProxy">
    <property name="target">
    <bean class="com.wnlja.prm.baselayer.spring.${dao.impl}.LogWriterImpl">
    <property name="genericDao" ref="genericDao" />
    </bean>
    </property>
    </bean>
    </beans>

  • 相关阅读:
    解决:Cuda安装后找不到安装文件目录
    学习论文:Eyeriss v1
    AOD-NET除雾算法
    学习论文AOD-Net:All-in-One Dehazing Network
    学习ResNeXt
    CentOS-7.6-ARM 离线安装部署FastDFS
    CentOS ARM离线安装部署Mysql5.6.44
    NFS+SnapShot快照式备份迁移ES索引过程
    《无限可能:快速唤醒你的学习脑》
    Oracle用sqlplus无法登陆?
  • 原文地址:https://www.cnblogs.com/wnlja/p/3909273.html
Copyright © 2020-2023  润新知