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. JVM 看不到某些异常的stacktrace问题(转)

    在java 1.5的release notes里面可以看到这样一句话: The compiler in the server VM now provides correct stack backtra ...

  2. [转]linux下IPTABLES配置详解

    如果你的IPTABLES基础知识还不了解,建议先去看看.开始配置我们来配置一个filter表的防火墙.(1)查看本机关于IPTABLES的设置情况[root@tp ~]# iptables -L -n ...

  3. HDU 4122 Alice's mooncake shop

    单调队列,裸的!!坑死了,说好的“All the orders are sorted by the time in increasing order. 呢,我就当成严格上升的序列了,于是就各种错.测试 ...

  4. linux(边压缩边传输边解压)

    比如我要转移旧VPS /home/wwwroot 下的web目录到新VPS(123.123.123.123)的/home/wwwroot 目录下   1.进入目录 cd /home/wwwroot  ...

  5. linux下如何产生core,调试core

    linux下如何产生core,调试core 摘自:http://blog.163.com/redhumor@126/blog/static/19554784201131791239753/ 在程序不寻 ...

  6. open函数

    open函数用来打开文件 其语法为:open(name[, mode[, buffering]]) open函数使用一个文件名作为唯一的强制参数,然后返回一个文件对象.模式(mode)和缓冲(buff ...

  7. Jam's math problem(思维)

    Jam's math problem Submit Status Practice HDU 5615   Description Jam has a math problem. He just lea ...

  8. Seek the Name, Seek the Fame(Kmp)

    Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (J ...

  9. WIN7 以下创建cocos2d-x3.0+lua项目

    用命令行生成和执行项目 无需打开VS 配置完环境 CMD执行 cocos new  helloWold   -p com.test -l lua -d E:\cocos2dx 来创建项目 cocos ...

  10. UVA - 10574 Counting Rectangles

    Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...