AOP面向切面编程:spring

//环境:使用aop除了要到入aop的jar包以外,还需要导入aop联盟的jar包:aopalliance.jar //默认为jak动态代理 //在配置中加上即可变为cglib动态代理或者 String resource = “applicationContext.xml”; ApplicationContext applicationContext=new ClassPathXmlApplicationContext(resource); //手动代理 /* * IStudentService iStudentService=(IStudentService) * applicationContext.getBean(“serviceProxy”); iStudentService.doDestroy(); * String resurt = iStudentService.doFirst(); System.out.println(resurt); try { * //iStudentService.checkUser(“hui”);制造异常 iStudentService.checkUser(“liu”); } * catch (UserException e) { // TODO Auto-generated catch block * e.printStackTrace(); } / //自动代理生成器DefaultAdvisorAutoProxyCreator: / * IStudentService iStudentService=(IStudentService)applicationContext.getBean( * “studentServiceBean”); iStudentService.doDestroy(); * iStudentService.doFirst(); / // Bean名称自动代理生成器BeanNameAutoProxyCreator: IStudentService iStudentService=(IStudentService)applicationContext.getBean(“studentServiceBean”); iStudentService.doDestroy(); iStudentService.doFirst();
ApplicationContext配置:
注册目标对象 注册前置通知 - 注册后置通知 注册环绕通知 注册异常通知 注册顾问 -NameMatchMethodPointcutAdvisor:名称匹配方法 RegexpMethodPointcutAdvisor;正则表达式匹配方法 运算符: “.”:点号,表示出现任意单个字符, “+”:加号,表示前一个字符出现一次或者多次 “
”:星号:表示前一个字符出现0次或者多次 *和.*的区别是,一个是方法名称,一个是全限定性方法名(全限定方法名,包含包的名称) </bean 手动生成代理对象:麻烦 target:目标 通知 顾问 自动代理生成器DefaultAdvisorAutoProxyCreator:方便,但有缺点:不能指定需要代理的方法,每个方法都被迫生成代理 ,而且只能使用顾问 bean class=“org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator”> Bean名称自动代理生成器BeanNameAutoProxyCreator:更方便,不仅能自动生成代理,而且能指定需要代理的方法,不需要代理不需要的代理的方法 , 而且能使用顾问,也能使用通知!

猜你喜欢

转载自blog.csdn.net/weixin_44543131/article/details/113173408
今日推荐