spring的aop编程(半自动、全自动)

摘要:
--代理类用于创建代理工厂bean并生成一个特殊的代理对象--˃˂!

1、spring的半自动代理(从spring中获取代理对象)

(1)spring中的通知类型

spring中aop的通知类型有五种:

前置:在目标方法执行前实施加强

后置:在目标方法执行后实施加强

环绕:在目标方法执行前后实施加强,必须手动执行目标方法,如果只在目标方法前面书写方法,就叫前置通知,前置通知可以阻止目标方法的执行,因为抛出异常后进入catch块,后置通知可以获得方法的返回值。

异常:在方法抛出异常后实施加强

引介:在目标类中添加一些新的方法和属性

(2)导入jar包

核心:4+1

spring的aop编程(半自动、全自动)第1张

aop:aop联盟(规范)、spring-aop(实现)

spring的aop编程(半自动、全自动)第2张

 (3)创建目标类接口和接口的实现类:

public interface StudentService {
     void addStudent();
     void updateStudent();
     void deleteStudent();
}
public class StudentServiceImpl implements StudentService{
    @Override
    public void addStudent() {
        System.out.println("addStudent");
    }

    @Override
    public void updateStudent() {
        System.out.println("updateStudent");
    }

    @Override
    public void deleteStudent() {
        System.out.println("deleteStudent");
    }
}

(4)创建切面:

public class MyAspect implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("前");
        methodInvocation.proceed();
        System.out.println("后");
        return null;
    }
}

(5)配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
    <!--目标类-->
    <bean id="studentService" class="pers.zhb.proxy.StudentServiceImpl"></bean>
    <!--切面类-->
    <bean id="myAspect" class="pers.zhb.proxy.MyAspect"></bean>
    <!--代理类
    用于创建代理工厂bean,生成特殊代理对象
    -->
    <bean id="proxystudentService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="interfaces" value="pers.zhb.proxy.StudentService"> </property><!--确定接口-->
        <property name="target" ref="studentService"></property><!--目标类-->
        <property name="interceptorNames" value="myAspect"></property><!--切面类的名称-->
    </bean>
</beans>
  <property name="optimize" value="true"></property><!--强制使用cglib-->

如果目标类有接口使用jdk动态代理,没有接口的话采用cglib字节码增强。如果声明为true,则无论是否有接口都是采用cglib

(6)测试:

public class TestFactoryBean {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService studentService= (StudentService) applicationContext.getBean("proxystudentService");
        studentService.addStudent();
        studentService.deleteStudent();
        studentService.updateStudent();
    }
}
前
addStudent
后
前
deleteStudent
后
前
updateStudent
后

2、全自动(从spring容器获得目标类,如果配置aop,spring将自动生成代理)

(1)导包:

要确定目标类,aspectj切入点表达式,导入jar包:

spring的aop编程(半自动、全自动)第3张

(2)创建目标类接口和接口的实现类:

public interface StudentService {
     void addStudent();
     void updateStudent();
     void deleteStudent();
}
public class StudentServiceImpl implements StudentService{
    @Override
    public void addStudent() {
        System.out.println("addStudent");
    }

    @Override
    public void updateStudent() {
        System.out.println("updateStudent");
    }

    @Override
    public void deleteStudent() {
        System.out.println("deleteStudent");
    }
}

(3)切面:

public class MyAspect implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("前");
        methodInvocation.proceed();
        System.out.println("后");
        return null;
    }
}

(4)配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--目标类-->
    <bean id="studentService" class="pers.zhb.proxy.StudentServiceImpl"></bean>
    <!--切面类-->
    <bean id="myAspect" class="pers.zhb.proxy.MyAspect"></bean>
    <!--aop编程-->
    <aop:config >
        <!--从目标对象获得具体方法,使用了切入点表达式-->
        <aop:pointcut id="myPointCut" expression="execution(* pers.zhb.proxy.*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="myAspect" pointcut-ref="myPointCut"></aop:advisor><!--特殊的通知,只有一个通知和切入点-->
    </aop:config>
</beans>

(5)测试类:

public class TestFactoryBean {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");
        //获得目标类
        StudentService studentService= (StudentService) applicationContext.getBean("studentService");
        studentService.addStudent();
        studentService.deleteStudent();
        studentService.updateStudent();
    }
}
前
addStudent
后
前
deleteStudent
后
前
updateStudent
后

免责声明:文章转载自《spring的aop编程(半自动、全自动)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇jquery源码 DOM加载GDI+显示GIF动画下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Spring 1 控制反转、依赖注入

1.1 Spring的核心是控制反转(IoC)和面向切面(AOP) 学习spring之前的开发中通过new创建一个对象,有了spring之后,spring创建对象实例-IoC控制反转,之后需要实例对象时从spring工厂(容器)中获得。 1.2 配置文件 位置:src 名称:applicationContext.xml <?xml version="...

Spring定时任务的几种实现

近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将结合 spring框架来介绍。 一.分类 从实现的技术上来分类,目前主要有三种技术(或者说有三种产品): Java自带的java.util.Timer类,这个类允许你调度一个java....

Netty通过心跳保持长链接

 Netty自带心跳检测功能,IdleStateHandler,客户端在写空闲时主动发起心跳请求,服务器接受到心跳请求后给出一个心跳响应。当客户端在一定时间范围内不能够给出响应则断开链接。 Java代码   public class NettyClient {       public void connect(String remoteServer...

springboot中的AOP开发

三步: 1.引入springboot-boot-start-aop jar包 <!--springboot与aop集成jar包--> <dependency> <groupId>org.springframework.boot</groupId>...

SpringMVC in IDEA开发实践

按照上篇装过Tomcat之后。 本机本来装了IDEA和Maven。 参考以下这篇 https://my.oschina.net/gaussik/blog/385697 《使用IntelliJ IDEA开发SpringMVC网站(一)开发环境》 其中初始化项目的时候非常慢,需要参考以下这篇来进行: http://www.cnblogs.com/beiyere...

EJB3 阶段总结+一个EJB3案例 (2)

这篇博文接着上一篇博文的EJB案例。 在上一篇博文中,将程序的架构基本给描述出来了,EJB模块分为5层。 1)DB层,即数据库层     在则一部分,我使用的数据库为mysql。在EJB程序中,访问数据库是通过Jboss中配置好的数据源进行的,然后在数据库中建立相应的数据库,不用建立表,在程序中使用JPA后通过Jboss启动会自动在数据库中间表     具...