项目结构:


切面类:

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(面向切面编程)-注解方式配置的更多相关文章

  1. 详细解读 Spring AOP 面向切面编程(二)

    本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...

  2. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  3. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  4. spring AOP面向切面编程学习笔记

    一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...

  5. 【Spring系列】Spring AOP面向切面编程

    前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...

  6. 从源码入手,一文带你读懂Spring AOP面向切面编程

    之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...

  7. Spring AOP面向切面编程详解

    前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...

  8. 详细解读 Spring AOP 面向切面编程(一)

    又是一个周末, 今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之. ...

  9. Spring Aop面向切面编程&&自动注入

    1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...

随机推荐

  1. sudo的使用和配置

    1 sudo是什么 Sudo是Unix/Linux平台上的一个非常有用的工具,它允许系统管理员分配给普通用户一些合理的“权利”,让他们执行一些只有超级用户或其他特许用户才能完成的任务,比如:运行一些像 ...

  2. mysql字符串查找(统计客源)

    如客源状态为1:2:3:5:6:9,其中6代表成交状态 如果要统计查询出有6这个状态的客源,可以用函数LOCATE(字符,搜索的字符串)来, 示例:统计每个分组下全部客源数total,成交客源数dea ...

  3. Python中的可变、不可变对象和赋值技巧序列解包

    可变对象和不可变对象 在python中一切皆对象.在Python中不存在所谓的值传递调用,一切传递都是对象的引用,也可认为是传址. python中,对象分为可变(mutable)和不可变(immuta ...

  4. Spring Cloud Stream如何消费自己生产的消息?

    在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...

  5. python安装Jieba中文分词组件并测试

    python安装Jieba中文分词组件 1.下载http://pypi.python.org/pypi/jieba/ 2.解压到解压到python目录下: 3.“win+R”进入cmd:依次输入如下代 ...

  6. spark高可用集群搭建及运行测试

    文中的所有操作都是在之前的文章spark集群的搭建基础上建立的,重复操作已经简写: 之前的配置中使用了master01.slave01.slave02.slave03: 本篇文章还要添加master0 ...

  7. List<T>集合的Sort自定义排序用法简单解析

    List<T>集合的Sort自定义排序用法简单解析: 如下:一系列无序数字,如果想要他们倒序排列,则使用如下代码: 那么如何理解这段代码呢? (x,y)表示相邻的两个对象,如果满足条件:x ...

  8. MyBatis3-配置使用log4j输出日志

    配置步骤: 1.POM的依赖引入 <!-- log4j --> <!-- https://mvnrepository.com/artifact/log4j/log4j --> ...

  9. linux基本命令手册

    常用指令 ls         显示文件或目录 -l           列出文件详细信息l(list) -a          列出当前目录下所有文件及目录,包括隐藏的a(all) mkdir    ...

  10. [android] 短信的广播接收者

    比较重要的一个广播事件,短信 界面布局,比如播放视频,默认是横屏全屏的,清单文件中进行设置, 在<activity/>节点设置屏幕朝向属性,android:screenOrientatio ...