欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

applicationContext配置

程序员文章站 2022-03-03 13:12:30
...
   <!-- 1.启动spring注解 -->
    <context:annotation-config></context:annotation-config>
    
    <!-- 2.扫描spring注解 -->
    <context:component-scan base-package="com.boolib">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
    
    <!-- 3.扫描log4j配置 -->
    <context:property-placeholder location="classpath:log4j.properties"/>
    
    <!-- 4.加载数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${uname}"></property>
        <property name="password" value="${upass}"></property>
    </bean>
    
    <!-- 5.创建SqlSessionFactory[配置数据源/读取核心配置文件/读取映射文件]-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:com/boolib/**/model/*Mapper.xml"></property>
    </bean>
    
    <!-- 6.接口代理模式:根据指定接口的位置去自动实例化对象 -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.boolib.**.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
    </bean>
    
    <!-- 7.启动事务注解 -->
    <tx:annotation-driven proxy-target-class="true"/>
    
    <!-- 8.配置事务bean数据源 -->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 9.配置事务管理 -->
    <tx:advice id="serviceAdvice" transaction-manager="transactionManager">
        <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="load*" read-only="true"/>
                <tx:method name="query*" read-only="true"/>
                <tx:method name="list*" read-only="true"/>
                <tx:method name="find*" read-only="true"/>
                <tx:method name="sel*" read-only="true"/>
                <tx:method name="login*" read-only="true"/>
                <tx:method name="check*" read-only="true"/>
                <tx:method name="valid*" read-only="true"/>
                <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    
    
    
    <!-- 10.启动AOP注解 -->
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
    
    <!-- 11.配置AOP监测表达式-->
    <aop:config proxy-target-class="true">
        <aop:pointcut expression="execution(* com.boolib..service.*Service.*(..))" id="aopPointCut"/>
        <aop:advisor advice-ref="serviceAdvice" pointcut-ref="aopPointCut"/>
    </aop:config>