Spring, 基于代理的AOP

原创 2015年07月07日 15:32:43

先定义接口

package net.wchdai.spring.dao;

public interface StudentDAO {
    public void saveStudent();
    public void queryStudent();
}

接口的实现类:

package net.wchdai.spring.dao.impl;

import net.wchdai.spring.dao.StudentDAO;


public class StudentDAOImpl implements StudentDAO{

    @Override
    public void saveStudent() {
        System.out.println("StudentDAOImpl save student");
    }

    @Override
    public void queryStudent() {
        System.out.println("StudentDAOImpl query student");
    }

}

代理类:

package net.wchdai.spring.proxy;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class MyStudentProxy implements MethodBeforeAdvice,AfterReturningAdvice  {

    @Override
    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
            Object arg3) throws Throwable {
        System.out.println("afterReturning");
    }

    @Override
    public void before(Method arg0, Object[] arg1, Object arg2)
            throws Throwable {
        System.out.println("before");

    }

}

ApplicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="studentDaoImpl" class="net.wchdai.spring.dao.impl.StudentDAOImpl"/>

<bean id="studentProxy" class="net.wchdai.spring.proxy.MyStudentProxy"/>

<bean id="studentDaoPointCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
    <property name="pattern" value=".*Student"></property>
</bean>


<bean id="studentDaoAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
    <property name="advice" ref="studentProxy"></property>
    <property name="pointcut" ref="studentDaoPointCut"></property>
</bean>

<bean id="studentDaoProxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="studentDaoImpl"></property>
    <property name="interceptorNames" value="studentDaoAdvisor"></property>
    <property name="proxyInterfaces" value="net.wchdai.spring.dao.StudentDAO"></property>
</bean>
</beans>

单元测试:

package net.wchdai.spring.test;

import net.wchdai.spring.dao.StudentDAO;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import junit.framework.TestCase;

public class StudentAOPTest extends TestCase{
    public void testAOPProxy(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        StudentDAO sdao=(StudentDAO)ctx.getBean("studentDaoProxyFactory");
        sdao.saveStudent();
    }
}

执行结果:

before
StudentDAOImpl save student
afterReturning
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

cglib动态代理例子和浅剖Spring Aop机制

1.cglib动态代理例子   要导入cglib的jar包 package com.arvon.cglibproxydemo; /** * 目标类 *@author Huangwen *20...

Spring模拟aop静态代理案例

  • 2015-12-05 17:01
  • 2.47MB
  • 下载

Spring-AOP-JDK动态代理

  • 2015-05-12 13:45
  • 75KB
  • 下载

Spring3.1.0实现原理分析(九).AOP创建代理对象的过程

大家好,今天我会用一个例子来讲解Spring创建bean代理对象的过程,为大家揭开Spring AOP的神秘面纱。在看这篇博客前我强烈建议读者先看下这两篇博客《Spring3.1.0实现原理分析(六)...

JavaEE spring自动实现AOP代理

  • 2016-10-16 21:51
  • 3.93MB
  • 下载

Spring AOP代理机制

Spring AOP通过代理模式实现,目前支持两种代理:JDK动态代理、CGLIB代理来创建AOP代理,Spring建议优先使用JDK动态代理。 JDK动态代理:使用java.lang.refl...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)