现在的位置: 首页 > 综合 > 正文

spring面向切面编程详解

2019年03月07日 ⁄ 综合 ⁄ 共 2764字 ⁄ 字号 评论关闭
spring面向切面编程详解spring面向切面编程详解spring面向切面编程详解spring面向切面编程详解切面,听起来似乎有些陌生吧,但其在实际项目中经常用到:比如说:Filter(过滤器),Interceptor(拦截器)以及代理...
一、aop术语:
1、切面:所有切入点的集合
2、切入点:一组符合某种规则的连接点
3、连接点:狭义上通俗的讲指的是某个方法切面,听起来似乎有些陌生吧,但其在实际项目中经常用到:比如说:Filter(过滤器),Interceptor(拦截器)以及代理...
一、aop术语:
1、切面:所有切入点的集合
2、切入点:一组符合某种规则的连接点
3、连接点:狭义上通俗的讲指的是某个方法
4、通知:在某个连接点上的某种操作,该操作并非连接点中的操作,而是外来的操作。
5、引入(Introduction):引入(在AspectJ中被称为inter-type声明)使得一个切面可以定义被通知对象实现给定的接口,
并且可以为那些对象提供具体的实现

二、用示例来理解:

2.1:添加声明以及支持:为下面红色字体
<?xml version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 配置Spring上下文的注解
扫描包的注解 -->
<context:component-scan
base-package="com.lovesmile.oa" />
<aop:aspectj-autoproxy/>//声明切面的自动代理

2.2:切面的定义类:

@Aspect//声明为切面
@Component//向Spring提交
public class MyInterceptor
{
@Pointcut("execution(*
com.lovesmile.oa.biz.impl.StuffBizImpl.*add(..))")
private void anymethod(){
//@Pointcut("execution(*
com.lovesmile.oa.biz.impl.StuffBizImpl.*(..))
&& execution(*
find*(..))")
System.out.println("ddddd");
};

@Pointcut("execution(*
com.lovesmile.oa.biz.impl.StuffBizImpl.find*(..))")
private void manymethod(){
//@Pointcut("execution(*
com.lovesmile.oa.biz.impl.StuffBizImpl.*(..))
&& execution(*
find*(..))")
System.out.println("ddddd");
};
@Before("anymethod()
&&
args(name)")//也可注册多个切入点,比如:@Before("anymethod()
&&
 manymethod()")
public void Before(String
name){
System.out.println("前置通知:短信发送Before"); 
}
@AfterReturning(pointcut="anymethod()",returning="returnval")
public void AfterReturning(String
returnval){
System.out.println("后置通知:发送成功:@AfterReturning"+returnval); 
}
@AfterThrowing(pointcut="anymethod()",throwing="e")
public void AfterThrowing(Exception
e){
System.out.println("后置通知:发送异常:@AfterReturning"); 
}
@After("anymethod()")
public void After(){
System.out.println("最终通知:发送成功:@AfterReturning"); 
}
@Around("anymethod()")//环绕通知
public Object
doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable
{
Object object=null;
System.out.println("短信:事务开始");
object=pjp.proceed();//继续调用业务方法
System.out.println("短信:事务关闭");
return object;
}
}

下面来详解一下:
@Pointcut("execution(*
com.lovesmile.oa.biz.impl.StuffBizImpl.find*(..))")
第一个*表示返回任何类型,
也可指定-->java.lang.String
 
com.lovesmile.oa.biz.impl.StuffBizImpl表示要过滤的类
.StuffBizImpl.find*(..)表示类的任意方法以及任意参数

注意:
com.lovesmile.oa.biz.impl..*.*(..)
表示包中的任意类或者子类的任意方法任意参数


2.3:哈哈,jar包你是否有了,这几个你必须得有:红色箭头的
jar文件
spring面向切面编程详解

是不是理解了,不会的可以私信哦...

抱歉!评论已关闭.