欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring-AOP(面向切面编程)-注解方式配置

程序员文章站 2022-07-24 12:13:28
项目结构: 增强(切面)类: 配置类AppConfig: 接口类: 接口实现类: 程序测试类: 如果半注解半配置文件实现的话, new ClassPathXmlApplicationContext("applicationContext.xml");实例 然后再配置一个xml application ......

项目结构:

spring-AOP(面向切面编程)-注解方式配置

 

 


 

 

增强(切面)类:

package edu.nf.ch12.service.aspect;

import org.aspectj.lang.joinpoint;
import org.aspectj.lang.proceedingjoinpoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.component;

/**
 * @author wangl
 * @date 2018/10/24
 */
@aspect //这个注解标识当前的类为一个切面
@component //标识容器受管的bean对象
public class userserviceaspect {

    /**
     * 声明一个切入点,并编写切入点表达式
     */
    @pointcut("execution(* edu.nf.ch12.service.*.*(..))")
    public void pointcut(){
    }

    /**
     * 前置通知,指定切入点函数
     * 也可在注解中自定义不同的切入点表达式
     * @before("execution(...)")
     *
     */
    @before("pointcut()")
    public void before(joinpoint joinpoint){
        system.out.println("前置通知..."+joinpoint.getargs()[0]);
    }

    /**
     * 后置通知
     */
    @afterreturning(value = "pointcut()", returning = "returnval")
    public void afterreturn(string returnval){
        system.out.println("后置通知..." + returnval);
    }

    /**
     * 环绕通知
     */
    @around("pointcut()")
    public object around(proceedingjoinpoint pjp) throws throwable {
        system.out.println("环绕通知前...");
        object returnval = pjp.proceed();
        system.out.println("环绕通知后...");
        return returnval;
    }

    /**
     * 异常通知
     * 通过pointcut指定切入点
     */
    @afterthrowing(pointcut = "pointcut()", throwing = "e")
    public void throwableadvice(throwable e){
        system.out.println("异常通知..." + e.getmessage());
    }

    /**
     * 最终通知
     */
    @after("pointcut()")
    public void after(){
        system.out.println("最终通知...");
    }

}

配置类appconfig:

package edu.nf.ch12.service.config;

import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.enableaspectjautoproxy;

/**
 * @author wangl
 * @date 2018/10/24
 */
@componentscan("edu.nf.ch12") //启用包扫描
@enableaspectjautoproxy //启用aspectj注解自动配置,proxytargetclass用于指定是否强制使用cglib代理
public class appconfig {
}

接口类:

package edu.nf.ch12.service;

/**
 * @author wangl
 * @date 2018/10/24
 */
public interface userservice {

    /**
     * 查询用户
     * @param uid
     * @return
     */
    string getusernamebyid(string uid);
}

接口实现类:

package edu.nf.ch12.service.impl;

import edu.nf.ch12.service.userservice;
import org.springframework.stereotype.service;

/**
 * @author wangl
 * @date 2018/10/24
 */
@service("userservice")
public class userserviceimpl implements userservice {

    @override
    public string getusernamebyid(string uid) {
        system.out.println("查询用户..."+uid);
        return "user1";
    }
}

程序测试类:

package edu.nf.ch12.test;

import edu.nf.ch12.service.userservice;
import edu.nf.ch12.service.config.appconfig;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

/**
 * @author wangl
 * @date 2018/10/24
 */
public class userservicetest {

    @test
    public void testgetuser(){
        //applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml");
        applicationcontext context = new annotationconfigapplicationcontext(appconfig.class);
        userservice service = context.getbean("userservice", userservice.class);
        service.getusernamebyid("1001");
    }
}

如果半注解半配置文件实现的话, new classpathxmlapplicationcontext("applicationcontext.xml");实例 然后再配置一个xml

applicationcontext:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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="edu.nf.ch12"/>
    <!-- 启用aspectj注解自动配置,proxy-target-class是否强制使用cglib动态代理,true表示强制-->
    <aop:aspectj-autoproxy/>

</beans>

 

运行结果:

spring-AOP(面向切面编程)-注解方式配置