1:Aop(aspect object programming)面向切面编程,名词解释:
    1.1:功能:让关注点代码与业务逻辑代码分离
    1.2:关注点
        重复代码就叫做关注点
    1.3:切面
        关注点形成的类,就叫做切面(类)
        面向切面编程,就是指对很多功能都有的重复代码抽取,再在运行的时候往业务方法上动态植入"切面类代码";
    1.4:切入点
        执行目标对象方法,动态植入切面代码
        可以通过切入点表达式,指定拦截那些类的那些方法,给指定的类在运行的时候植入切面类代码;
2:注解方式实现aop编程
    2.1:开发步骤
        (1):先引入aop相关的jar文件
            spring-aop-3.2.5.RELEASE.jar【去spring3.2源码里面找】
            aopalliance.jar【去spring2.5源码/lib/aopalliance文件里面找】
            aspectjweaver.jar【去spring2.5源码/lib/aspectj文件里面找】或者【aspectj-1.8.2/lib/aspectjweaver.jar】
            aspectjrt.jar【去spring2.5源码/lib/aspectj文件里面找】或者【aspectj-1.8.2/lib/aspectjrt.jar】

        《注意:用到的spring2.5版本的jar本舰,如果用jd1.7版本可能会出现问题,
              需要升级以下aspectj组件,即使用aspectj-1.8.2版本中提供的jar文件aspectjweaver.jar和aspectjrt.jar》   
        (2)bean.xml中引入aop名称空间
      技巧:找到文件spring-framework-3.2.5.RELEASE/docs/spring-framework-reference/htmlsingle
         打开index.html搜索xmlns:aop然后找到下面红色三句话,分别拷贝到bean.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:aop="http://www.springframework.org/schema/aop"

          xsi:schemaLocation="http://www.springframework.org/schema/beans

            http://www.springframework.org/schema/beans/spring-beans.xsd

          http://www.springframework.org/schema/aop

          http://www.springframework.org/schema/aop/spring-aop.xsd">

      拷贝之后的bean.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
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"> </beans>

    (3):bean.xml中开启aop注解扫描,如下配置所示:

 <?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
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"> <!-- 开启注解扫描 -->
<context:component-scan base-package="com.bie.aop"></context:component-scan> <!-- 开启aop注解方式,默认为false -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>

    (4):开始写一个切面类,源码如下所示:

 package com.bie.aop;

 import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* @author BieHongLi
* @version 创建时间:2017年3月28日 下午9:10:43
* @Aspect:指定当前类为切面类
*/
@Component //加入到IoC容器
@Aspect //指定当前类为切面类
public class Aop { //指定切入点表达式,拦截那些方法,即为那些类生成代理对象
//@Pointcut("execution(* com.bie.aop.UserDao.save(..))") ..代表所有参数
//@Pointcut("execution(* com.bie.aop.UserDao.*())") 指定所有的方法
//@Pointcut("execution(* com.bie.aop.UserDao.save())") 指定save方法 @Pointcut("execution(* com.bie.aop.UserDao.*(..))")
public void pointCut(){ } @Before("pointCut()")
public void begin(){
System.out.println("开启事务");
} @After("pointCut()")
public void close(){
System.out.println("关闭事务");
} }

    (5):写好切面类就可以写执行目标对象方法,接口和实现类如下所示:

 package com.bie.aop;

 /**
* @author BieHongLi
* @version 创建时间:2017年3月28日 下午9:09:29
*
*/ public interface IUserDao { public void save();
}
 package com.bie.aop;

 import org.springframework.stereotype.Component;

 /**
* @author BieHongLi
* @version 创建时间:2017年3月28日 下午9:09:53
* 目标对象
*/
@Component
public class UserDao implements IUserDao{ @Override
public void save() {
System.out.println("..核心业务--核心业务..");
} }

    (6):最后就可以进行进行测试了,源码如下所示:

 package com.bie.aop;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author BieHongLi
* @version 创建时间:2017年3月28日 下午9:13:18
*
*/
public class AopTest { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //目标对象有实现接口,spring会自动选择"jdk代理【动态代理】"
//动态代理的标识:class com.sun.proxy.$Proxy10
@Test
public void test01(){
IUserDao dao = (IUserDao) ac.getBean("userDao");
System.out.println(dao.getClass());
dao.save();
} //class com.bie.aop.OrderDao$$EnhancerByCGLIB$$4952a60a
//目标对象没有实现接口,spring会用"cglib代理哦"
@Test
public void testCglib(){
OrderDao dao = (OrderDao) ac.getBean("orderDao");
System.out.println(dao.getClass());
dao.save();
}
}

  3:心得体会和报错解决:

    3.1:虽然案例很简单很简单,但是我花了三四个小时,为什么呢!我用junit测试spring写的注解实现aop(面向切面编程)。

    3.2:编辑环境:eclipse+tomcat8.0+jdk1.8,为什么说编辑环境呢,因为jdk1.8和spring好像有仇似的,开始我安装的jdk是1.8版本的,总之包很多很多错,主要的caused by:java.lang.IllegalArgumentException【看错误主要看caused by】

    3.3:这个错是UserDao dao =  (UserDao) ac.getBean("userDao");---》

        IUserDao dao =  (IUserDao) ac.getBean("userDao");

        意思就是说必须使用接口来接受从IoC容器获取的,不能使用实现类接受,不然报的错够你喝一壶了

java.lang.ClassCastException: com.sun.proxy.$Proxy10 cannot be cast to com.bie.aop.UserDao
    at com.bie.aop.AopTest.test01(AopTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

     3.4:然后呢,我就思考就要换成jdk1.7,我去oracle官网搜了,已经没了jdk1.7,我又去我的百度云翻出1.7,然后安装,之后呢,我就验证呗,其实安装之前我也思考过,能不能安装1.8之后再安装1.7,安装肯定没毛病啊,但是总要配置环境变量吧,我就配置jdk1.7之后发现验证的时候还是显示是jdk1.8,我就赶紧思考啊,思考过后我考虑应该把jdk1.7,jdk1.8都卸载了,然后我都卸载了,然后重新安装jdK1.7,之后配置之后,其实主要给Java_home环境变量改一下就行,然后验证就显示jdk1.7版本了,之后呢又去测试程序,然而呢,程序之前是jdk1.8,全部报错了,我又把jdk1.8换成1.7;详细更换jdk1.8--->jdk1.7的过程如下所示;


首先,第一步:点击window-->Preferences-->Java-->Installed JREs-->ADD

第二步:点击add之后显示如下所示

第三步:点击next之后点击如下图所示的Directory即可;

第四步:点击项目右击

第五步:如下所示

第六步:如下所示

第七步:如下所示

第八步:如下所示

第九步:如下所示

然后呢,jdk总算是由jdk1.8换成了1.7,这个过程虽然比较麻烦,但是解决问题,我也很开心啊,加油吧,骚年~~~

Spring之注解实现aop(面向切面编程)的更多相关文章

  1. 不依赖Spring使用AspectJ达到AOP面向切面编程

    网上大多数介绍AspectJ的文章都是和Spring容器混用的,但有时我们想自己写框架就需要抛开Spring造轮子,类似使用原生AspectJ达到面向切面编程.步骤很简单,只需要两步. 1.导入依赖 ...

  2. spring框架学习(三)——AOP( 面向切面编程)

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  3. Spring注解式AOP面向切面编程.

    1.AOP指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式.aop底层是动态代理. package com.bie.config; import org.aspectj.lan ...

  4. Spring:AOP面向切面编程

    AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...

  5. Spring Boot2(六):使用Spring Boot整合AOP面向切面编程

    一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop ​ aop全称Aspec ...

  6. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  7. 详细解读 Spring AOP 面向切面编程(二)

    本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...

  8. Spring 08: AOP面向切面编程 + 手写AOP框架

    核心解读 AOP:Aspect Oriented Programming,面向切面编程 核心1:将公共的,通用的,重复的代码单独开发,在需要时反织回去 核心2:面向接口编程,即设置接口类型的变量,传入 ...

  9. 谈一谈AOP面向切面编程

    AOP是什么 : AOP面向切面编程他是一种编程思想,是指在程序运行期间,将某段代码动态的切入到指定方法的指定位置,将这种编程方式称为面向切面编程 AOP使用场景 : 日志 事务 使用AOP的好处是: ...

随机推荐

  1. [2017-05-31]Abp介绍和经验分享-目录

    很久没动博客了,人比较懒. 最近想写点啥,主要就介绍下ABP框架和我这两年的使用经验. 文档翻译之类的工作就算了,需要的请参考: 官方文档 PS:官方文档末尾有中文文档的链接,这里就不贴了 先列个提纲 ...

  2. Cookie常用操作以及属性

    概述 最近项目要用到cookie存储部分用户信息;研究了一下做一下分享 Cookie 是服务器保存在浏览器的一小段文本信息,每个 Cookie 的大小一般不能超过4KB.浏览器每次向服务器发出请求,就 ...

  3. 通过编译lambda表达式来创建实例(可在反射时候用,效率比反射高一些)

    原文地址:https://rogerjohansson.blog/2008/02/28/linq-expressions-creating-objects/ 据说编译lambda创建实例是比反射快.实 ...

  4. C++IO类&文件输入输出

    C++IO类&文件输入输出 istream(输入流)类型,提供输入操作. ostream(输出流)类型,提供输出操作. cin,一个istream对象,从标准输入读取数据. cout,一个os ...

  5. [自制操作系统] 连续页分配释放&kmalloc/kfree

    本文将在JOS上实现连续内存.释放,提供内核的kmalloc与kfree,并在分配frambuffer的时候进行测试. Github : https://github.com/He11oLiu/JOS ...

  6. jenkins+gitlab+sonar+testng构建持续集成测试环境(配置干货篇)

    几个工具的安装部分就不在此介绍了! jenkins配置: 1.插件安装 2.root私钥配置 3.构建job配置 4.部署job配置 5.测试job配置 7.jenkins全局工具配置 8.jenki ...

  7. CCNA基础知识摘录

    cisco设备的启动要点: 1.检测硬件(保存在rom) 2.载入软件(IOS)(保存在Flash) 3.调入配置文件(密码,IP地址,路由协议都保存在此)(此文件保存在NVRAM) 0x2102:正 ...

  8. PTA中提交Java程序的一些套路

    201708新版改版说明 PTA与2017年8月已升级成新版,域名改为https://pintia.cn/,官方建议使用Firefox与Chrome浏览器. 旧版 PTA 用户首次在新版系统登录时,请 ...

  9. Python IDLE快捷键一览

    编辑状态时:Ctrl + [ .Ctrl + ] 缩进代码Alt+3 Alt+4 注释.取消注释代码行Alt+5 Alt+6 切换缩进方式 空格<=>TabAlt+/ 单词完成,只要文中出 ...

  10. 201521123060 《Java程序设计》第9周学习总结

    1.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2.书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1截图你的提交结果(出现学号) 1.2自己以前编写 ...