Spring之注解实现aop(面向切面编程)
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(面向切面编程)的更多相关文章
- 不依赖Spring使用AspectJ达到AOP面向切面编程
网上大多数介绍AspectJ的文章都是和Spring容器混用的,但有时我们想自己写框架就需要抛开Spring造轮子,类似使用原生AspectJ达到面向切面编程.步骤很简单,只需要两步. 1.导入依赖 ...
- spring框架学习(三)——AOP( 面向切面编程)
AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...
- Spring注解式AOP面向切面编程.
1.AOP指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式.aop底层是动态代理. package com.bie.config; import org.aspectj.lan ...
- Spring:AOP面向切面编程
AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...
- Spring Boot2(六):使用Spring Boot整合AOP面向切面编程
一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop aop全称Aspec ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- Spring 08: AOP面向切面编程 + 手写AOP框架
核心解读 AOP:Aspect Oriented Programming,面向切面编程 核心1:将公共的,通用的,重复的代码单独开发,在需要时反织回去 核心2:面向接口编程,即设置接口类型的变量,传入 ...
- 谈一谈AOP面向切面编程
AOP是什么 : AOP面向切面编程他是一种编程思想,是指在程序运行期间,将某段代码动态的切入到指定方法的指定位置,将这种编程方式称为面向切面编程 AOP使用场景 : 日志 事务 使用AOP的好处是: ...
随机推荐
- 两个input在同一行连着不留缝隙
方法1:让两个input 连在一起写 不换行 <div class="inputDiv"> <input type="text" placeh ...
- spring 整合Mybatis 《报错集合,总结更新》
错误:java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldExcepti ...
- 【C++小白成长撸】--(续)单偶数N阶魔方矩阵
1 /*程序的版权和版本声明部分: **Copyright(c) 2016,电子科技大学本科生 **All rights reserved. **文件名:单偶数N阶魔方矩阵 **程序作用:单偶数N阶魔 ...
- react 入门
一:virtual DOM 虚拟DOM树 在React中,render执行的结果得到的并不是真正的DOM节点,结果仅仅是轻量级的JavaScript对象,我们称之为virtual DOM. 虚拟DO ...
- WeTest+微信:小程序云端测试系统上线
日前,微信新增小程序测试系统,可便于开发者检测小程序缺陷,评估小程序产品质量.在小程序发布之前,开发者可将小程序代码提交到测试系统,在不同型号的手机真机上运行,执行完毕后自动生成测试报告.小程序云端测 ...
- Django1.10主题指南—模型
模型是你的数据的唯一的.权威的信息源.它包含你所储存数据的必要字段和操作行为.通常,每个模型都对应着数据库中的唯一一张表. 基础认识: 每个model都是一个继承 django.db.models.M ...
- java伪代码《大道至简》
阅读<大道至简>第一章,深感作者对编程问题的精炼定义,通过对古老寓言故事<愚公移山>的引用,说明了编程的本质,即顺序,分支,循环.其中又将他们扮演的项目组织者,团队经理,编程人 ...
- 转深入Java虚拟机 之四:类加载机制
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17881581 类加载过程 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的整个 ...
- java1.8新特性
转自:http://www.oschina.NET/translate/everything-about-Java-8 建议去看原文,此处转载只是为了记录. 这篇文章是对Java8中即将到来的改进做一 ...
- FFmpeg 常用命令收集
FFmpeg 常用命令 合并视频 ffmpeg -i "KTDS-820A_FHD.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts i ...