Spring、Hello AOP
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的更多相关文章
- Spring、AOP详解
如何配置AOP查看:Spring.Hello AOP 1.对于拦截规则@Pointcut的介绍: @Pointcut("execution (* cn.raffaello.service.. ...
- JAVAEE——spring02:使用注解配置spring、sts插件、junit整合测试和aop演示
一.使用注解配置spring 1.步骤 1.1 导包4+2+spring-aop 1.2 为主配置文件引入新的命名空间(约束) 1.3 开启使用注解代替配置文件 1.4 在类中使用注解完成配置 2.将 ...
- 170511、Spring IOC和AOP 原理彻底搞懂
Spring提供了很多轻量级应用开发实践的工具集合,这些工具集以接口.抽象类.或工具类的形式存在于Spring中.通过使用这些工具集,可以实现应用程序与各种开源技术及框架间的友好整合.比如有关jdbc ...
- 16监听器、Filter、Nginx、Spring、AOP
16监听器.Filter.Nginx.Spring.AOP-2018/07/30 1.监听器 监听web对象创建与销毁的监听器 ServletContextListener HttpSessionLi ...
- Spring 3.0 AOP (一)AOP 术语
关于AOP.之前我已写过一个系列的随笔: <自己实现简单的AOP>,它的关注点在于实现.实现语言是C#,实现方式为 自定义实现 RealProxy 抽象类.重写Invoke方法,以便进行方 ...
- Spring系列之AOP实现的两种方式
AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...
- Spring系列之AOP
一.什么是AOPAOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引 ...
- SSM ( Spring 、 SpringMVC 和 Mybatis )配置详解
使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...
- Spring学习(二)——Spring中的AOP的初步理解[转]
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
随机推荐
- SQL Server 823,824 错误
第一: 823错误只代表.SQL server 要向操作系统申请一个页面读写的时候遇到Windos读取或写入失败.823错误是读写请求时发生的 和读写的内容没有关系.823与SQL server 本身 ...
- Linux的启动流程以及GRUB详解
一.Linux引导和启动流程 概述,计算机电源接通后通过BISO之后,没有问题,就会去硬盘上找到MBR(Main Boot Record 主引导记录区)位于整个硬盘的0磁道0柱面1扇区, ...
- oracle 创建表空间详细介绍
注意点: 1.如果在PL/SQL 等工具里打开的话,直接修改下面的代码中[斜体加粗部分]执行 2.确保路径存在,比如[D:\oracle\oradata\Oracle9i\]也就是你要保存文件的路径存 ...
- Git 介绍
一,理解 Git 1,分布式版本控制 Git 版本控制系统的设计思想是"去中心化".传统的 CVS .SVN 等工具采用的是 C/S 架构,只有一个中心代码仓库,位于服务器端.而一 ...
- openwrt 新版IPV6问题
我的版本是r45579,表现是路由认证后正确获取IPV6地址并且可以ping通外网,电脑可以获取地址但是连不上外网. 加一条转发规则就好了 先查看/etc/config/network里给电脑分配的地 ...
- hdu 1116 Play on Words(欧拉通路)
Problem Description Some of the secret doors contain a very interesting word puzzle. The team of arc ...
- 安装Discuz!论坛时提示“mysqli_connect() 不支持 advice_mysqli_connect”
安装Discuz!论坛时提示“不支持Mysql数据库,无法安装论坛”的解决方法1,在系统的 system32(C:\windows\system32)目录下缺少libmysql.dll文件,解决方法是 ...
- [Android]APK程序卸载二次确认的实现
严正声明 本人本着技术开放,思想分享的目的,撰写本文.文章仅供参考之用,请勿使之于非法或有害于社会和谐之用. Sodino 2011-01-24 Android上能不能实现卸载时提示呢, ...
- memcached 内存管理 分析(转)
Memcached是一个高效的分布式内存cache,了解memcached的内存管理机制,便于我们理解memcached,让我们可以针对我们数据特点进行调优,让其更好的为我所用.这里简单谈一下我对me ...
- checklistbox的使用
public class CheckListboxHelper { #region 为checklistbox绑定数据源 /// <summary> /// 为checklistbox绑定 ...