Spring文件AOP编程

来源:互联网 发布:怎么创造软件 编辑:程序博客网 时间:2024/06/06 03:02

Spring文件AOP编程

若目标对象有接口,则使用JDK代理,若目标对象无接口,则选择CGLIB代理

1.导入jar

spring-aop-3.2.0.RELEASE.jar

org.aopalliance-1.0.0.jar - 依赖包下

aspectjrt.jar

aspectjweaver.jar

2.创建切面和目标对象及其接口

3.配置xml文件

1.头信息导入aop

  2.bean配置

3.aop类配置

4.aop 配置

<aop:config>

切入点

<aop:pointcut expression="execution(*com.demo.dao.*.*(..))" id="pt"/>

切面

<aop:aspect ref="aop">

<!-- 前置通知:在目标方法调用前执行-->

<aop:beforemethod="begin" pointcut-ref="pt"/>

<!-- 后置通知: -->

<aop:aftermethod="after" pointcut-ref="pt"/>

<aop:aspect/>

<aop:config/>


AOP是将切面和目标对象结合生成动态代理对象的过程(Spring底层自动生成动态代理)

所有方法都是一个连接点,但并非一定是切入点

 前置通知

 <aop:beforemethod="begin" pointcut-ref="pt"/>  ||  @Before("pointCut_()")

  后置通知

 <aop:aftermethod="after" pointcut-ref="pt"/>   ||  @After("pointCut_()")

  环绕通知

 <aop:aroundmethod="around" pointcut-ref="pt"/>   ||  @Around("pointCut_()")

  返回后通知

 <aop:after-returningmethod="afterReturning" pointcut-ref="pt"/>  || @AfterReturning("pointCut_()")

  异常通知

 <aop:after-throwingmethod="afterThrowing" pointcut-ref="pt"/>  ||@AfterThrowing("pointCut_()")


代码片段

public class Aspect1 {public void begin(){System.out.println("开始事务");}public void end(){System.out.println("事务结束");}public void commit(ProceedingJoinPoint pjp) throws Throwable{System.out.println("执行前");pjp.proceed();System.out.println("执行后");}}public class UserBiz {public void add(){System.out.println("添加用户信息");}public void delete(){System.out.println("删除用户信息");}public void update(){System.out.println("修改用户信息");}public void get(){System.out.println("查询用户信息");}}beans.xml<?xml version="1.0" encoding="UTF-8"?><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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="aspectDemo" class="com.icss.aspect.Aspect1"></bean><bean id="targetDemo" class="com.icss.target.UserBiz"></bean><!-- aop的配置  --><aop:config><!-- 引入切入点 --><!-- <aop:pointcut expression="execution(*com.icss.target.UserBiz.*(..))"   id="pt"/> --><aop:pointcut expression="execution(* com.icss.target.UserBiz.add(..))"   id="pt"/><aop:pointcut expression="execution(* com.icss.target.UserBiz.delete(..))"   id="pt2"/><aop:aspect ref="aspectDemo" ><!-- 加载通知 --><aop:before method="begin"  pointcut-ref="pt"/><aop:after method="end"  pointcut-ref="pt"/><aop:around method="commit"  pointcut-ref="pt"/></aop:aspect></aop:config> </beans>


SpringAOP编程注解方式:

package com.icss.aspect;@Component@Aspectpublic class Aspect2 {@Before("execution(* com.icss.target.StuBiz.*(..))")public void begin(){System.out.println("开始事务");}@After("execution(* com.icss.target.StuBiz.*(..))")public void end(){System.out.println("事务结束");}@Around("execution(* com.icss.target.StuBiz.*(..))")public void commit(ProceedingJoinPoint pjp) throws Throwable{System.out.println("执行前");pjp.proceed();System.out.println("执行后");}}package com.icss.target;@Service("stuBiz")public class StuBiz {public void addStu(){System.out.println("录入学生信息");}public void deleteStu(){System.out.println("删除学生信息");}public void updateStu(){System.out.println("更新学生信息");}public void getStu(){System.out.println("查询学生信息");}}beans.xml<?xml version="1.0" encoding="UTF-8"?><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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置bean--><context:component-scan base-package="com.icss.aspect" /><context:component-scan base-package="com.icss.target" /><!-- 开启AOP注解方式 --><aop:aspectj-autoproxy/></beans>





spring架构图


原创粉丝点击