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. Nginx Upload Module 上传模块

    传统站点在处理文件上传请求时,普遍使用后端编程语言处理,如:Java.PHP.Python.Ruby等.今天给大家介绍Nginx的一个模块,Upload Module上传模块,此模块的原理是先把用户上 ...

  2. VC调用javascript的几种方法

    第一种:通过execScript调用.这种方法,虽然操作方便,但不能获取返回值.m_spHtmlDoc->get_parentWindow(&m_pHtmlWindow);VARIANT ...

  3. VC实现图片拖拽及动画

    基础知识 1.PictureBox控件的使用 2.加载位图文件 1.通过文件路径获得位图句柄 //获得位图句柄  void CMovePictureDlg::GetHandleFromPath(CSt ...

  4. https://pta.patest.cn/pta/test/15/exam/3/question/724

    List Reverse( List L ){ if(L==NULL) return; List head=(List)malloc(sizeof(struct Node)); head->Ne ...

  5. opendrive.com提供的免费网盘

    opendrive.com是由以前freehao123给大家介绍的BOXSTr免费网络硬盘演变而来的,现在BOXSTr已经无法使用了.打开BOXSTr网站就会自动跳转到opendrive.com网站. ...

  6. 安装 SQL Server 2008 R2 的硬件和软件要求(转)

    以下各部分列出了安装和运行 SQL Server 2008 R2 的最低硬件和软件要求.有关 SharePoint 集成模式下的 Analysis Services 的要求的详细信息,请参阅硬件和软件 ...

  7. 小黑小波比.coding的使用

    1_Coding的演示 1_html的演示 1_先查看帮助 1.它支持的语言非常多.下面是链接地址 https://coding.net/u/bobo159357456/p/html/paas/hel ...

  8. opengl笔记—— glMultMatrixf() 区别 glLoadMatrixf()

    能找到最好的解释来自:http://www.gamedev.net/topic/489879-glpushmatrixglpopmatrix--glloadmatrixf/ 原理: glPushMat ...

  9. HDU2054_A == B ?【模拟题】【大数】【水的问题】

    A == B ? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  10. 关于HTML css的一些题目

    1.dcutype是什么,他是干嘛用的 doctype是文档的一种声明,告诉浏览器器用什么规则来解析文档. 2.ul ol dl分别适用于什么地方? ul无序列表 ol有序列表 dl是定义列表,会默认 ...