学习笔记(七)中,总结了AOP中的基本概念,并用注解的方式实现了AOP。
下面用配置的方式实现,以同注解方式实现比较,从而深入掌握AOP编程。
代码一:接口和实现类
package cn.itcast.server;
public interface IPersonService {
public void save(String name);
public String getPersonName(Integer id);
public void update(String name,Integer id);
}
package cn.itcast.server.impl;
import cn.itcast.server.IPersonService;
public class PersonServiceBean2 implements IPersonService {
public String getPersonName(Integer id) {
System.out.println("这是getPersonName方法");
return "xxx";
}
public void save(String name) {
// throw new RuntimeException("我是例外");
System.out.println("这是save方法");
}
public void update(String name, Integer id) {
System.out.println("这是update方法");
}
}
代码二:拦截器的定义
package cn.itcast.server;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyInteceptor2 {
public void doAccessCheck(){
System.out.println("这里输出的是前置通知 ");
}
public void doAfterReturning(){
System.out.println("这里输出的是后置通知 " );
}
public void doAfterReturn(){
System.out.println("这里输出的是最终通知");
}
public void doAfterThrowing(){
System.out.println("这里输出的是例外通知 ");
}
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
//判断用户是否具有权限
System.out.println("进入方法");
Object result = pjp.proceed();
System.out.println("退出方法");
return result;
}
}
代码三:配置文件
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<aop:aspectj-autoproxy/>
<!--AOP开发(基于xml配置进行AOP开发,基于注解方式进行AOP开发) -->
<bean id="myInteceptor2" class="cn.itcast.server.MyInteceptor2"></bean>
<bean id="personService2" class="cn.itcast.server.impl.PersonServiceBean2"></bean>
<aop:config>
<aop:aspect id="asp" ref="myInteceptor2">
<!-- expression说明要处理的切入点(方法),下面那个意思是过滤任意返回类型的,cn.itcast.server.impl.PersonServiceBean2类中的任意参数的切入点 -->
<!-- <aop:pointcut id="mycut" expression="execution(* cn.itcast.server.impl.PersonServiceBean2.*(..) )"/> -->
<aop:pointcut id="mycut" expression="execution(* cn.itcast.server.impl.PersonServiceBean2.*(java.lang.String,..) )"/>
<!-- <aop:pointcut id="mycut" expression="execution(!void cn.itcast.server.impl.PersonServiceBean2.*(..) )"/> -->
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
<aop:after pointcut-ref="mycut" method="doAfterReturn"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
</beans>
测试类:
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.server.IPersonService;
public class SpringAOPTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void interceptorTest(){
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
IPersonService ipersonService = (IPersonService) ac.getBean("personService2");
ipersonService.update("xxx",2);
ipersonService.save("xx");
ipersonService.getPersonName(2);
}
}
分享到:
相关推荐
最近做的Spring 2.5 AOP 例子,保留下以后复习说不定用得上. 1.使用类扫描功能这个有点意思.使用时方便了许多. 2.XML配置方式
第八课:Spring AOP配置选项 21 一、 AOP配置annotation方式 21 (一) 搭建annotation开发环境 21 (二) aspectJ类库 22 (三) AOP的annotation实例 22 (四) AspectJ的专业术语 23 (五) 织入点语法 23 (六) Advice 24 ...
基于 Spring2.5.6 包含Spring2.5中的信赖注入、AOP、事务管理等较全面的内部。
spring2.5所有jar包:spring-aop.jar,spring-jms.jar等
Spring2.5开发 如果我们用到了AOP的话 一定需要aspectjrt.jar + aspectjweaver.jar 两个jar文件 这里面提供了 导入这两个就可以进行AOP开发了
Spring2.5和Hibernate3集成 采用声明式事务 1.声明式事务的配置 * 配置sessionFactory * 配置事务管理器 * 配置事务的传播特性 * 配置哪些类哪些方法使用事务 2.编写业务逻辑方法 * 继承...
使用Spring配置文件实现AOP
spring2.5学习PPT 传智博客 01_全面阐释Spring及其各项功能 02_搭建与测试Spring的开发环境 03_编码剖析Spring管理Bean的原理 04_Spring的三种实例化Bean的方式 05_配置Spring管理的bean的作用域 06_Spring...
spring的Ioc和aop的总结,以及spring的事务管理(Jdbc和Hibernate)
使用Spring的注解方式实现AOP的细节
赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....
Spring2.5-Reference_zh_CN.chm Spring2.5-中文参考手册.chm spring——AOP,IOC.doc Spring框架快速入门之简介.doc spring配置全书.doc Spring中的IOC与AOP详解.ppt
Spring源码最难问题:当Spring AOP遇上循环依赖.docx
主要包括了spring2.5中主要包的框架图,有AOP,beans,web,orm等,是不可多得的资源。
13) spring-mock.jar需spring-core.jar,spring-beans.jar,spring-dao.jar,spring-context.jar,spring-jdbc.jarspring2.0和spring2.5及以上版本的jar包区别Spring 2.5的Jar打包 在Spring 2.5中, Spring Web MVC...
SpringAOP学习笔记以及四个可运行的Demo,涵盖经典代理模式、基于注解、基于xml配置这3方面的Demo
里面包括4个例子:(1)Spring实现AOP方式之一:基于XML配置的Spring AOP (2)Spring实现AOP方式之二:使用注解配置 Spring AOP (3)Spring AOP : AspectJ Pointcut 切点 (4)Spring AOP : Advice 声明 (通知注解)
按照ssh的基本应用逐步添加的jar包,包含AOP切面编程的包。事务的提交建议用AOP去做。
NULL 博文链接:https://microjava.iteye.com/blog/525796