Spring AOP详解及通过XML配置实现的实现方法
来源:互联网 发布:软件app下载 编辑:程序博客网 时间:2024/06/18 08:32
Spring AOP详解及通过XML配置实现的实现方法
1.Spring AOP详解
讲解一下Spring AOP 的一些常用术语
切面(Aspect):
要增加什么功能?一般一个类就代表着一个功能(每个功能就是一个切面)
常用的切面有:事务管理,日志管理,权限管理,异常处理等等
连接点(Joinpoint):
哪个类的哪个方法上增加功能?
通知(Advice):
切面类的功能加在方法的什么位置?前面?后面?产生异常时?环绕(前面和后面都有)??
切入点(Pointcut):
给连接点(Jooinpoint)命名,便于在多处使用,这点很重要
目标对象(Target Object):
目标对象是被代理对象,自己的功能不够强大,要通过aop加强。
AOP代理(AOP Proxy)
功能已经通过aop加强的那个对象(这个是目标对象加强后的对象)
(一般都是newProxyinstance)
织入(Weaving)
把新功能(通过aop加强的功能)和本身的功能(目标对象的基本功能)组织在一起执行。
下面讲解一下一些语法(也是aop详解部分)
1.joinpoint语法:用一些通配符来定义在哪些类和方法上增加aop功能。
语法:(注意星号后面的空格)
1).execution(public * * (..))
所有public方法
2).execution(* set*(..))
所有以set作为方法名开头的方法
3).exuection(* com.xzy.service.AccountService.* (..))
com.xzy.service.AccountService类中的所有方法
4).execution(* com.xyz.service.*.* (..))
com.xyz.service包中所有类的所有方法
5).execution(* com.xyz.service..*.* (..))
com.xyz.service包及子包下的所有类的所有方法
2.pointcut:给joinpoint取个名字。(这个比较重要)
@pointcut("execution( * com.xzy.*.* (..))")
public void myPoint(){}///必须要写一个空的函数,然后
如这样调用:@Before("myPoint()")
3.advice
@Before
@AfterReturning
@AfterThrowing
@After(finally)
@Around
4.JoinPoint jp
ProceedingJointPoint pjp
这个可以知道当前aop对那个类的那个方法进行功能加强,也很重要,
也就是可以知道aop的切入点
System.out.println(jp.getSignature());///打印切入点的标签
但是aop切面类里面的方法要包含JoinPoint jp,如这样:
public void myend(JoinPoint jp)
{
System.out.println("方法结束了......");
}
下面讲解在spring的xml配置AOP
代码如下:
<aop:config>
<aop:aspect ref="log">
<aop:pointcut
expression="execution(* net.xinqushi.dao.impl.*.*(..))" id="pt"/>
<aop:before method="before" pointcut-ref="pt"/>
<aop:after method="after" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>
注意:在aop类中必须指定@Component(value="log")///说明用spring来初始化这个类
这里面的log和ref="log"必须相同
其他的代码以此类推!
- Spring AOP详解及通过XML配置实现的实现方法
- Spring-通过xml配置实现AOP
- 配置xml方式实现Spring的aop
- Spring AOP XML配置实现
- 采用AOP配置方式的AOP实现及XML解析
- Spring-通过xml配置文件实现切面(AOP)
- Spring AOP xml配置实现(sxt小结)
- Spring配置xml文件实现AOP
- Spring Xml文件配置实现AOP通知
- spring(14)------xml配置AOP实现
- Spring AOP(3)基于XML配置实现的示例
- Spring AOP(3)基于XML配置实现的示例
- 使用Spring的配置xml方式实现AOP
- 【Spring aop】Spring aop的XML和注解的两种配置实现
- spring AOP的实现方法
- spring aop的xml配置详解
- Spring实现AOP方式之一:基于XML配置的Spring AOP
- Spring AOP的实现原理详解及代码实例
- 布局:三栏布局(7种方法)
- App接入支付宝开发流程
- dropwizard微服务实践
- 51nod 1085 背包问题01
- java8 Optional
- Spring AOP详解及通过XML配置实现的实现方法
- 在存储过程中创建临时表来重构数据
- C++异常处理机制
- mysql知识点滴
- addEventListener() 方法,事件监听
- 背包九讲
- 索引压缩
- 存储过程示例:在存储过程中使用临时表
- mysql字符集设置