AOP的hello world

来源:互联网 发布:类似易企秀的软件 编辑:程序博客网 时间:2024/06/01 22:05

AOP使用的三种方式: 1.ProxyFactoryBean代理工具类生成  2.自动代理生成器 3.AspectJ对AOP的实现

在spring中使用aop开发时,一般使用aspectj的实现方式

使用aop所需要的jar包 spring-aop 和 aopalliance

一、ProxyFactoryBean代理工具类生成

1.目标类

public class StudentServiceImpl implements IStudentService{    public StudentServiceImpl() {        System.out.println("执行无参构造方法");    }    public void doSome(){        System.out.println("执行doSome");    }}
2.切面类

public class MyTransactionProcessor implements MethodBeforeAdvice {    @Override    public void before(Method method, Object[] objects, Object o) throws Throwable {        System.out.println("事务代码,正在执行的是:" + o.getClass().getName());    }}

3.xml配置

<!--静态工厂bean--><bean id="studentService" class="com.mh.spring.factory.StudentServiceFactory" factory-method="getInstance"/><!--配置切面:通知--><bean id="processor" class="com.mh.spring.executor.MyTransactionProcessor"/><!--配置顾问:名称匹配方式--><!--<bean id="pointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">    <property name="advice" ref="processor"/>    <property name="mappedNames" value="doSome,doOther"/></bean>--><!--配置顾问:正则匹配方式--><bean id="pointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">    <property name="advice" ref="processor"/>    <property name="pattern" value=".*doS.*"/></bean><!--配置代理--><bean id="studentServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">    <property name="target" ref="studentService"/>    <property name="interfaces" value="com.mh.spring.service.IStudentService"/>    <property name="interceptorNames" value="pointcutAdvisor"/></bean>
4.测试

@Testpublic void testDoSome() throws Exception {    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");    IStudentService studentService = (IStudentService) ac.getBean("studentServiceProxy");    studentService.doSome();}

二、自动代理生成器(1. 默认advisor自动代理生成器 2. Bean名称自动代理生成器)

只是xml配置有变动和测试类有变动,其它同上

1. 默认advisor自动代理生成器

1.1 xml配置

<!--静态工厂bean--><bean id="studentService" class="com.mh.spring.factory.StudentServiceFactory" factory-method="getInstance"/><!--配置切面:通知--><bean id="processor" class="com.mh.spring.executor.MyTransactionProcessor"/><!--配置顾问:名称匹配方式--><!--<bean id="pointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">    <property name="advice" ref="processor"/>    <property name="mappedNames" value="doSome,doOther"/></bean>--><!--配置顾问:正则匹配方式--><bean id="pointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">    <property name="advice" ref="processor"/>    <property name="pattern" value=".*doS.*"/></bean><!--配置自动代理--><bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

1.2 测试

@Testpublic void testDoSome() throws Exception {    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");    IStudentService studentService = (IStudentService) ac.getBean("studentService");    studentService.doSome();}

 2.Bean名称自动代理生成器

1).添加目标类

public class OtherServiceImpl implements IStudentService {    public void doSome(){        System.out.println("正的执行 OtherService.doSome()");    }}
2).xml配置

<!--目标类:普通方式创建bean--><bean id="otherService" class="com.mh.spring.service.impl.OtherServiceImpl" /><!--目标类:静态工厂bean--><bean id="studentService" class="com.mh.spring.factory.StudentServiceFactory" factory-method="getInstance"/><!--配置切面:通知--><bean id="processor" class="com.mh.spring.executor.MyTransactionProcessor"/><!--配置顾问:名称匹配方式--><!--<bean id="pointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">    <property name="advice" ref="processor"/>    <property name="mappedNames" value="doSome,doOther"/></bean>--><!--配置顾问:正则匹配方式--><bean id="pointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">    <property name="advice" ref="processor"/>    <property name="pattern" value=".*doS.*"/></bean><!--配置自动代理--><bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">    <property name="beanNames" value="otherService,studentService"/>    <property name="interceptorNames" value="pointcutAdvisor"/></bean>
3)测试

@Testpublic void testDoSome() throws Exception {    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");    IStudentService studentService = (IStudentService) ac.getBean("studentService");    studentService.doSome();    IStudentService otherService = (IStudentService) ac.getBean("otherService");    otherService.doSome();}

3.AspectJ式的aop

 要引入两个jar包spring-aspects和weaver,aop约束

1)定义业务接口及实现类

public class StudentServiceImpl implements IStudentService{    public void doSome(){        System.out.println("执行doSome");    }    @Override    public void doOther(int a, int b) {        System.out.println("执行doOther方法");    }    @Override    public String doThird() {        System.out.println("执行doThird方法");        return "aaaa";    }}
2)定义切面

public class MyAspect {    public void before(){        System.out.println("前置增强");    }    public void afterReturning(Object result){        System.out.println("后置增强:目标方法执行结果为:" + result);    }}
3)xml配置

<!--目标对象--><bean id="studentService" class="com.mh.spring.service.impl.StudentServiceImpl"/><!--切面--><bean id="myAspect" class="com.mh.spring.executor.MyAspect"/><!--配置AOP--><aop:config>    <!--切入点-->    <aop:pointcut id="myDoSomePointcut" expression="execution(* *..StudentServiceImpl.doSome())"/>    <aop:pointcut id="myDoOtherPointcut" expression="execution(* *..StudentServiceImpl.doOther())"/>    <aop:pointcut id="myDoThirdPointcut" expression="execution(* *..StudentServiceImpl.doThird())"/>    <!--切面-->    <aop:aspect ref="myAspect">        <aop:before method="before" pointcut-ref="myDoSomePointcut"/>        <aop:after-returning method="afterReturning" pointcut-ref="myDoThirdPointcut" returning="result"/>    </aop:aspect></aop:config>
4)测试

@Testpublic void testDoSome() throws Exception {    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");    IStudentService studentService = (IStudentService) ac.getBean("studentService");    studentService.doSome();    studentService.doThird();}

0 0
原创粉丝点击