AOP 概念:http://blog.csdn.net/moreevan/article/details/11977115

AOP 所使用到的jar 包:

aspectjrt.jar

common-annotations.jar

aspectjweaver.jar

cglib-nodep.jar

commons-logging.jar

spring.jar

1.在beans.xml中配置AOP的命名空间和Context的命名空间,同时打开AOP注解开关:

<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-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <aop:aspectj-autoproxy />
</beans>

2.新建PersonIService和PersonServiceBean :

public interface PersonIService {
public void save();
public void update();
public String getObject(String name);
}
public class PersonServiceImpl implements PersonIService {

	@Override
public void save() {
System.out.println("这是一个Save方法!");
} @Override
public void update() {
System.out.println("这是一个Update方法!");
} @Override
public String getObject(String name) {
System.out.println("get方法获取对象= "+name+" 的对象");
return "返回了"+name;
}
}

3.新建AOP拦截类:

// @Aspect 标注这个类是一个切面
@Aspect
public class MyInterceptor {
@Pointcut("execution (* cn.raffaello.service..*.*(..))")
private void anyMethod(){} // 声名一个切入点,声名方式比较奇特 @Before("anyMethod()") // 定义前置通知
public void doAccessCheck(){
System.out.println("前置通知");
}
}

4.在beans.xml中分别注入PersonService和MyInterceptor:

	<bean id="myInterceptor" class="cn.raffaello.aop.MyInterceptor" />
<bean id="personIService" class="cn.raffaello.service.impl.PersonServiceImpl"/>

5.测试代码:

public class springTest {
private PersonIService personIService=null;
@Before
public void init(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
personIService=(PersonIService)context.getBean("personIService");
}
@Test
public void test() {
personIService.save();
}
}

Spring、Hello AOP的更多相关文章

  1. Spring、AOP详解

    如何配置AOP查看:Spring.Hello AOP 1.对于拦截规则@Pointcut的介绍: @Pointcut("execution (* cn.raffaello.service.. ...

  2. JAVAEE——spring02:使用注解配置spring、sts插件、junit整合测试和aop演示

    一.使用注解配置spring 1.步骤 1.1 导包4+2+spring-aop 1.2 为主配置文件引入新的命名空间(约束) 1.3 开启使用注解代替配置文件 1.4 在类中使用注解完成配置 2.将 ...

  3. 170511、Spring IOC和AOP 原理彻底搞懂

    Spring提供了很多轻量级应用开发实践的工具集合,这些工具集以接口.抽象类.或工具类的形式存在于Spring中.通过使用这些工具集,可以实现应用程序与各种开源技术及框架间的友好整合.比如有关jdbc ...

  4. 16监听器、Filter、Nginx、Spring、AOP

    16监听器.Filter.Nginx.Spring.AOP-2018/07/30 1.监听器 监听web对象创建与销毁的监听器 ServletContextListener HttpSessionLi ...

  5. Spring 3.0 AOP (一)AOP 术语

    关于AOP.之前我已写过一个系列的随笔: <自己实现简单的AOP>,它的关注点在于实现.实现语言是C#,实现方式为 自定义实现 RealProxy 抽象类.重写Invoke方法,以便进行方 ...

  6. Spring系列之AOP实现的两种方式

    AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...

  7. Spring系列之AOP

    一.什么是AOPAOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引 ...

  8. SSM ( Spring 、 SpringMVC 和 Mybatis )配置详解

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...

  9. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

随机推荐

  1. linux case 语句

    #!/bin/bash #$ 表示脚本名 #$n 表示第n个参数(n>) in ") echo '--=> A' ;; ") echo '--=> B' ;; * ...

  2. android 手势滑动

    1.概述, 两次都是画曲线统计图用到手势滑动.左滑动,右滑动曲线图翻页 2.直接上代码 3.注: 第一次使用的时候是implement了 OnTouchListener 接口,是在画图布局上layou ...

  3. 利用COM组件IPicture读取jpg、gif、bmp图片文件数据和显示图片

    1.读取图片数据 函数原型:bool LoadImage(const char *pName, unsigned char *pBitData); 函数功能,读取pName指向的图片文件的位图数据 b ...

  4. 【LeetCode练习题】Minimum Window Substring

    找出包含子串的最小窗口 Given a string S and a string T, find the minimum window in S which will contain all the ...

  5. Windows查看电脑上安装的.Net Framework版本的五种方法(转)

    1.查看安装文件判断Framwork版本号 打开资源管理器,比如我的电脑,再地址栏输入%systemroot%\Microsoft.NET\Framework后单击“转到”或者按回车. 在新文件夹中查 ...

  6. C#打印条码BarTender SDK打印之路和离开之路(web平凡之路)

    从来没想过自己会写一篇博客,鉴于这次从未知的探索到一个个难点的攻破再到顺利打印,很想记录这些点滴,让后人少走弯路. 下面走进正题. 需求:取数据库里的相应的字段数据,并生成条形码,可以批量.单条打印. ...

  7. Android面试题目及其答案

    转自:http://blog.csdn.net/wwj_748/article/details/8868640 Android面试题目及其答案 1.Android dvm的进程和Linux的进程, 应 ...

  8. hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)

    Problem Description There was no donkey ,) , the down-right cell ,N-) and the cell below the up-left ...

  9. ping网络故障

    网络的应用已渐渐深入我们的工作和生活,它带给了我们各方面的便利.因此,这种种的便利致使很多人对网络产生依赖性.那么,当电脑不能上网时,我们如何才能准确地判断电脑问题出在哪里?又如何能快捷地解决这故障? ...

  10. Android单元測试之JUnit

    随着近期几年測试方面的工作慢慢火热起来.常常看见有招聘測试project师的招聘信息.在Java中有单元測试这么一个JUnit 方式,Android眼下主要编写的语言是Java,所以在Android开 ...