Spring aop xml配置案例

该博客展示了Spring框架中AOP(面向切面编程)的配置和应用,包括配置数据源、定义Bean、设置通知(前置、后置、异常、最终、环绕)以及切入点表达式。通过Logger类实现方法执行前后的日志打印,体现了AOP在实际业务中的应用。
摘要由CSDN通过智能技术生成
	<?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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">


    <context:property-placeholder location="classpath:jdbcConfig.properties"/>


    <!-- 需要被代理的对象  :Target(目标对象) -->
    <bean id="IAccountService" class="com.geor.helloSpringxml.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <bean id="accountDao" class="com.geor.helloSpringxml.dao.impl.AccountDaoImpl">
        <property name="runner" ref="runner"></property>
        <property name="connectionUtils" ref="connectionUtils"></property>
    </bean>
    <bean id="connectionUtils" class="com.geor.helloSpringxml.utils.ConnectionUtils">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner">
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>

    </bean>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--连接数据库的必备信息-->
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>


    <!-- 通知  ,这里是前置通知 -->
    <bean id="logId" class="com.geor.helloSpringxml.utils.Logger"></bean>

    <aop:config >
        <!--
           ref: 是指定通知类bean的Id。
        -->
        <aop:aspect id="logAspectId" ref="logId">
            <!--
                method : advice的增强方法
                pointcut : Target(目标对象) 的切入点配置
            -->
            <!--<aop:before method="printLog"-->
            <!--pointcut="execution(* * com.geor.helloSpringxml.service.impl.AccountServiceImpl.findAccountById(..)  )"></aop:before>-->

            <!--<aop:before method="printLog" pointcut=""></aop:before>-->

            <aop:pointcut id="pt1" expression="execution(* com.geor.helloSpringxml.service.impl.*.*(..) )"/>
             <!--配置前置通知:在切入点方法执行之前执行-->
        <aop:before method="beforePrintLog" pointcut-ref="pt1" ></aop:before>

             <!--配置后置通知:在切入点方法正常执行之后值。它和异常通知永远只能执行一个-->
            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>

             <!--配置异常通知:在切入点方法执行产生异常之后执行。它和后置通知永远只能执行一个-->
            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>

            <!-- 配置最终通知:无论切入点方法是否正常执行它都会在其后面执行
            <aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>-->

            <!-- 配置环绕通知 详细的注释请看Logger类中-->
            <aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around>
        </aop:aspect>


    </aop:config>


</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值