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

来源:互联网 发布:树莓派的编程语言 编辑:程序博客网 时间:2024/06/05 05:14

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>
原创粉丝点击