首页 > 编程语言 >javaee spring 测试aop 切面

javaee spring 测试aop 切面

时间:2023-09-03 12:38:00浏览次数:44  
标签:spring void javaee springframework aop test org public


切面类

package com.test.advice;

import org.aspectj.lang.ProceedingJoinPoint;

//增强类
public class MyAdvice {

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

    

}

目标类

package com.test.service;

public interface IUsersService {

    public void add();

    public void update();

    public void delete();
}
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("删除用户...");
    }
}

spring配置文件

<?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: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">
              <!-- 将增强类对象myAdvice中的before方法切入到pc对应的切点所在的方法前面 -->
              <aop:before method="before" pointcut-ref="pc" />


          </aop:aspect>
    </aop:config>


</beans>

依赖

<?xml version="1.0" encoding="UTF-8"?>

<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.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>testSpring06</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>testSpring06 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.18.Release</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.18.Release</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.18.Release</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.18.Release</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <!-- aop -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.10</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>testSpring06</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

测试类

package com.test.testAop;

//import com.test.service.IItemsService;
import com.test.service.IUsersService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop {

    @Test
    public void test()
    {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

        IUsersService usersServiceProxy=applicationContext.getBean("usersService", IUsersService.class);

        try {
            usersServiceProxy.add();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

测试结果

javaee spring 测试aop 切面_maven


标签:spring,void,javaee,springframework,aop,test,org,public
From: https://blog.51cto.com/u_15295225/7339374

相关文章

  • javaee spring 静态代理
    静态代理packagecom.test.staticProxy;publicinterfaceIUsersService{publicvoidinsert();}packagecom.test.staticProxy;//目标类publicclassUsersServiceimplementsIUsersService{@Overridepublicvoidinsert(){System.out.print......
  • javaee spring jdbc 查询方法
    javaeespringjdbc查询方法packagecom.test.dao.impl;importcom.test.dao.IUsersDao;importcom.test.pojo.Users;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.jdbc......
  • javaee spring jdk动态代理
    jdk动态代理packagecom.test.jdkProxy;publicinterfaceIUsersService{publicvoidinsert();}packagecom.test.jdkProxy;//目标类publicclassUsersServiceimplementsIUsersService{@Overridepublicvoidinsert(){System.out.println(&qu......
  • javaee spring注解设置单例模式和懒加载模式
    @Lazy懒加载@Scope(scopeName=“prototype”)设置多例模式,不加默认单例模式@Lazy@Component@Scope(scopeName="prototype")publicclassDrink{@Value("橙汁")privateStringname;@Value("半糖")privateStringsugar;@Value(&quo......
  • javaee spring 自动注入,如果满足条件的类有多个如何区别
    如图IDrinkDao有两个实现类方法一方法二@Resource(name=“对象名”)@Resource(name="oracleDrinkDao")privateIDrinkDaodrinkDao;......
  • javaee spring 用注解的方式实现ioc
    spring用注解的方式实现iocspring核心依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http:/......
  • javaee spring jdbcTemplate的使用
    依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="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.......
  • javaee spring 依赖注入之复杂类型的注入数组 集合 等
    spring依赖注入之复杂类型的注入packagecom.test.pojo;importjava.util.List;importjava.util.Map;importjava.util.Properties;/***@description:*@projectName:testSpring*@see:com.test.pojo*@createTime:2023/8/2714:39*/publicclassAA{pri......
  • javaee spring 依赖注入之构造方法注入
    依赖注入之构造方法注入constructor-arg标签指定构造方法的参数packagecom.test.pojo;/***@description:*@projectName:testSpring*@see:com.test.pojo*@author:杨钧博*@createTime:2023/8/2711:18*/publicclassDrink{privateStringname;pri......
  • javaee spring依赖注入之property注入
    spring依赖注入之property注入如下图,如果是基本数据类型,用value属性,如果是对象,则用ref......