javaee spring aop 的五种通知方式

spring配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    
    <context:component-scan base-package="com.test" />

   
    <bean id="myAdvice" class="com.test.advice.MyAdvice" />

    
    <aop:config>
          
          <aop:pointcut id="pc" expression="execution(* com.test.service.impl.*.add(..))" />
          
          <aop:aspect ref="myAdvice">
              
              <aop:before method="before" pointcut-ref="pc" />

              <aop:after method="after" pointcut-ref="pc" />

              <aop:around method="around" pointcut-ref="pc" />

              <aop:after-returning method="afterReturning" pointcut-ref="pc" />

              <aop:after-throwing method="afterThrowing" pointcut-ref="pc" />

          aop:aspect>
    aop:config>


beans>

切面类

package com.test.advice;

import org.aspectj.lang.ProceedingJoinPoint;

//增强类
public class MyAdvice {

    //将这个增强方法切入到service层的add方法前
    public void before()
    {
        System.out.println("添加用户之前");
    }

    //目标方法执行后(不管是出异常还是成功执行)
    public void after()
    {
        System.out.println("添加用户之后");
    }
    //环绕通知,用这个增强代码替换掉目标方法
    public void around(ProceedingJoinPoint point) throws Throwable {
          System.out.println("执行目标方法之前");
          point.proceed(); //放行切点处的方法(目标方法)
    }

    //目标方法成功执行后
    public void afterReturning()
    {
       System.out.println("目标方法成功执行后");
    }

    //目标方法出异常
    public void afterThrowing()
    {
        System.out.println("目标方法出异常以后才执行");
    }

}

目标类

package com.test.service.impl;

import com.test.service.IUsersService;
import org.springframework.stereotype.Service;

@Service
public class UsersService implements IUsersService {

    @Override
    public void add()  {

        System.out.println("添加用户...");
    }

    @Override
    public void update() {
        System.out.println("修改用户...");
    }

    @Override
    public void delete() {
        System.out.println("删除用户...");
    }
}

测试结果

javaee spring aop 的五种通知方式_第1张图片

依赖



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>org.examplegroupId>
  <artifactId>testSpring06artifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>testSpring06 Maven Webappname>
  
  <url>http://www.example.comurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.7maven.compiler.source>
    <maven.compiler.target>1.7maven.compiler.target>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>4.3.18.Releaseversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>4.3.18.Releaseversion>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>4.3.18.Releaseversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>4.3.18.Releaseversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-expressionartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjweaverartifactId>
      <version>1.8.10version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>

  dependencies>

  <build>
    <finalName>testSpring06finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-war-pluginartifactId>
          <version>3.2.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

你可能感兴趣的:(Mac开发,java-ee,spring,java)