三大框架之Spring (初级学习 2)

1. AOP

     * AOP:Aspect Oriented Programming,面向切面编程
     *   
     * 在日志和异常处理方面很常用
     * 
     * 新加入了   3 个包:
     *  Spring :aop,aspects,
     *  AspectJ :aspectjweaver */

2. 在不修改原有代码的基础上增加新的功能,通过配置文件,切换不同的功能

    com.zhiyou100.dao.UserDaoOracleImpl 》》》 com.zhiyou100.dao.UserDaoMysqlImpl

2 . 项目 截图:

这里写图片描述

3 . 配置文件: applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- <bean name="mysql" class="com.zhiyou100.dao.UserDaoMySqlImpl"></bean> 
        <bean name="oracle" class="com.zhiyou100.dao.UserDaoOracleImpl"></bean> <bean 
        name="service" class="com.zhiyou100.service.UserServiceImpl"> <property name="dao" 
        ref="mysql"></property> </bean> -->

    <bean name="dao" class="com.zhiyou100.dao.UserDaoOracleImpl"></bean>

    <bean name="service" autowire="byName"
        class="com.zhiyou100.service.UserServiceImpl"></bean>

    <bean name="myAspects" class="com.zhiyou100.aop.MyAspects"></bean>

    <aop:config>
        <aop:aspect ref="myAspects">
            <aop:before method="beforeLog"
                pointcut="execution(public void com.zhiyou100.service.UserServiceImpl.pay())" />
            <aop:after method="afterLog" pointcut="execution(* com..UserServiceImpl.pay())" />

            <!-- 切入点: before:方法执行前,=== 参数校验 after:方法执行后,无论是否产生异常 === 清理资源 after-returing:方法正常执行完毕 
                === 修改返回值 after-throwing:方法抛出异常 === 包装异常信息 around:方法执行前后两个点 -->

            <!-- pointcut:切入点表达式 execution(修饰符 返回值 包名.类名.方法名(参数类型)) 修饰符:可以省略 返回值:不能省略,可以使用 
                * 代替 包名:com 不能省,可以使用 * 代替, 中间的包名也可以使用 * 代替,如果想省略需要写 .. 代替 类名,方法名:不能省略,可以使用 
                * 代替 参数:如果只有一个参数可以使用 * 代替,如果有多个参数可以使用 .. 代替 -->

        </aop:aspect>
    </aop:config>
</beans>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值