spring中的各种通知
来源:互联网 发布:免费linux云主机 编辑:程序博客网 时间:2024/05/20 05:25
package cn.itcast.spring.aop.xml;public class Person {private String pname;private Integer pid;public String getPname() {return pname;}public void setPname(String pname) {this.pname = pname;}public Integer getPid() {return pid;}public void setPid(Integer pid) {this.pid = pid;}}
package cn.itcast.spring.aop.xml;import java.util.List;public interface PersonDao {public void savePerson();public void deletePeron();public void updatePerson();public List<Person> findPerson();}
package cn.itcast.spring.aop.xml;import java.util.ArrayList;import java.util.List;public class PersonDaoImpl implements PersonDao {@Overridepublic void deletePeron() {System.out.println("delete Person");}@Overridepublic void savePerson() {System.out.println("save Person");}@Overridepublic void updatePerson() {System.out.println("update Person");}@Overridepublic List<Person> findPerson() {Person person1=new Person();person1.setPid(1);person1.setPname("person1");Person person2=new Person();person2.setPid(2);person2.setPname("person2");List<Person> personlist=new ArrayList<Person>();personlist.add(person1);personlist.add(person2);return personlist;}}
package cn.itcast.spring.aop.xml;import java.util.ArrayList;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;public class Transaction {public void beginTransaction(){System.out.println("begintransaction");}/** * @param joinPoint * 通过joinPoint可以得到目标类和目标方法的一些信息 * @param val * 目标方法的返回值 * 和<aop:after-returning returning="val"/>中returning的值保质一致 */public void commit(JoinPoint joinpoint,Object val){//后置通知String methodName=joinpoint.getSignature().getName();System.out.println(methodName);int i=1/0;System.out.println(joinpoint.getTarget().getClass().getName());System.out.println("commit");List<Person> list=(ArrayList<Person>)val;for(Person p:list){System.out.println(p.getPname());}}public void finallyMethod(){System.out.println("finallyMethod");}public void aroundMethod(ProceedingJoinPoint joinpoint){System.out.println("aroundMethod");try {joinpoint.proceed();//调用目标类的目标方法/* * 环绕通知和前置通知与后置通知的区别 * 环绕通知需要手动的调用目标类的目标方法 手动调用时 相当于前置和后置通知 * 前置和后置通知 是自动调用目标类的目标方法的 */} catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void throwingMethod(Throwable except){System.out.println(except.getMessage());}}
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 1.目标类 2.切面 3.进行aop的配置 --> <bean id="personDaoImpl" class="cn.itcast.spring.aop.xml.PersonDaoImpl"></bean> <!-- 切面的声明 --> <bean id="transaction" class="cn.itcast.spring.aop.xml.Transaction"></bean> <!-- aop的配置 --> <aop:config> <!-- 配置切入点: id是切入点的标识 expression:为切入点的表达式 execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?) modifiers-pattern? 修饰符 一次或一次也没有 可选 public private protected ret-type-pattern 返回类型 必选 * 代表任意类型 declaring-type-pattern ? 一次或一次也没有 方法的声明类型 name-patterm 方法名称类型 set* 以set开头的所有的方法名称 update* 以update开头的所有的方法名称 param-pattern 参数匹配 (..) 任意多个参数,每个参数任意多个类型 (*,String) 两个参数 第一个是任意类型,第二个是String (String,*,Integer) 三个参数,第一个是String类型,第二个是任意类型,第三个是Integer类型 throws-pattern ? 异常的匹配模式 例子: execution(* cn.itcast.spring.aop.xml.AService.*(..)); cn.itcast.spring.aop.xml.AService下的所有的方法 execution(public * cn.itcast.oa..*.*(..)) 返回值为任意类型,修饰符为public,在cn.itcast.oa包及子包下的所有的类的所有的方法 exectuion(* cn.itcast.oa..*.update*(*,String)) 返回值是任意类型,在cn.itcast.oa包及子包下所有的以update开头的参数为两个,第一个为任意类型 第二个为String类型的所有类的所有的方法 --> <aop:pointcut expression="execution(* cn.itcast.spring.aop.xml.PersonDaoImpl.*(..))" id="perform"/> <!-- 配置切面 ref:指向声明切面的类 --> <aop:aspect ref="transaction"> <!-- 前置通知 pointcut-ref 引用一个切入点--> <aop:before method="beginTransaction" pointcut-ref="perform"/> <!-- 配置后置通知:returning 目标方法的返回值 如果目标方法中有可能存在异常 异常确实反生了 这个时候 后置通知将不在执行 --> <aop:after-returning method="commit" pointcut-ref="perform" returning="val"/> <!-- 最终通知 不能得到目标方法的返回值 无论目标方法时候有异常 最终通知都执行 资源关闭 连接的释放写在追踪通知里 --> <aop:after method="finallyMethod" pointcut-ref="perform" /> <!-- 环绕通知 --><!-- <aop:around method="aroundMethod" pointcut-ref="perform"/>--> <!-- 异常通知 --> <aop:after-throwing method="throwingMethod" pointcut-ref="perform" throwing="except"/> <!-- 原理: 当启动spring容器的时候 会把纳入spring容器的bean实例化 spring容器会解析配置文件中的 <aop:config> ,从而解析aop:config下的切入点表达式 解析出来以后 就会在spring容器的bean中 查找 是否有类 符合切入点表达式 如果符合 则为这个对象创建代理对象 客户端根据bean的id需找一个bean 如果该bena有代理对象 则返回代理对象 如果没有代理对象 这返回bean的对象 --> </aop:aspect> </aop:config></beans>
package cn.itcast.spring.aop.xml;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAop {@Testpublic void test(){ApplicationContext context=new ClassPathXmlApplicationContext("cn/itcast/spring/aop/xml/applicationContext.xml");PersonDao personDao=(PersonDao) context.getBean("personDaoImpl");personDao.updatePerson();}}