注解方式实现aop

导入 AOP 相关坐标

3.3 Spring集成Junit步骤
①导入spring集成Junit的坐标
<!--此处需要注意的是,spring5 及以上版本要求 junit 的版本必须是 4.12 及以上-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
②使用@Runwith注解替换原来的运行期
③使用@ContextConfifiguration指定配置文件或配置类
④使用@Autowired注入需要测试的对象
⑤创建测试方法进行测试
在测试类中
 
 

创建目标接口和目标类(内部有切点)

package com.jiang3;

public interface targetInterface {
    public void save();
}

 

package com.jiang3;

import org.springframework.stereotype.Component;

@Component("target")//相当于<bean id="target" class="com.jiang3.target"></bean>
public class target implements targetInterface { @Override public void save() { System.out.println("target"); } }

 

创建切面类(内部有增强方法)

package com.jiang3;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect//相当于定义切面<aop:aspect ref="myaspect">
@Component("myaspect")//相当于<bean id="myaspect" class="com.jiang3.myaspect"></bean>
public class myaspect { 
//
<aop:pointcut id="pointcut" expression="execution(* com.jiang3.*.*(..))"/>
//<aop:around method="around" pointcut-ref="pointcut"></aop:around>
@Around("execution(* com.jiang3.*.*(..))") 
public void around(ProceedingJoinPoint prj){
Object proceed
=null;
System.out.println(
"前置增强");
try {
proceed
= prj.proceed();
System.out.println(
"后置增强");
}
catch (Throwable throwable){
throwable.printStackTrace();
System.out.println(
"抛出异常"); }

finally {
System.out.println(
"最终增强");
} } }

 

将目标类和切面类的对象创建权交给 spring

 

在 applicationContext.xml 中配置织入关系

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

">

    <context:component-scan base-package="com.jiang3"/>
    <aop:aspectj-autoproxy/>


</beans>

 

测试代码 

import com.jiang3.targetInterface;
import com.jiang3.target;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)//使用@Runwith注解替换原来的运行期
@ContextConfiguration("classpath:applicationContext3.xml")//使用@ContextConfifiguration指定配置文件或配置类
public class test3 {
    @Autowired//使用@Autowired注入需要测试的对象
private targetInterface target;
    @Test
    public void test(){

        target.save();
    }
}

 

 对比之前测试类

public class test3 {
    @Test
    public void test(){
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext3.xml");
        targetInterface target = (targetInterface)app.getBean("target");
       target.save();

    }
}

 

posted @ 2020-05-11 15:13  每月工资一万八  阅读(182)  评论(0)    收藏  举报