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(面向切面编程)-注解方式配置的更多相关文章
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- Spring AOP 面向切面编程相关注解
Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ...
- spring AOP面向切面编程学习笔记
一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...
- 【Spring系列】Spring AOP面向切面编程
前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...
- 从源码入手,一文带你读懂Spring AOP面向切面编程
之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...
- Spring AOP面向切面编程详解
前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...
- 详细解读 Spring AOP 面向切面编程(一)
又是一个周末, 今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之. ...
- Spring Aop面向切面编程&&自动注入
1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...
随机推荐
- Python快速学习04:循环 & 函数
前言 系列文章:[传送门] 也就今天认识了 LC ,很开心. 本文目录 循环 for while 中断 函数 函数定义 函数调用 for循环 Python 中的for 循环象shell 脚本里的for ...
- Spring Security之动态配置资源权限
在Spring Security中实现通过数据库动态配置url资源权限,需要通过配置验证过滤器来实现资源权限的加载.验证.系统启动时,到数据库加载系统资源权限列表,当有请求访问时,通过对比系统资源权限 ...
- 一个用于分页的page类
今天周一,趁工作轻松,自己就写了一个基于MySQl数据库的分页查询,做分页,最主要的是以下几点: 一:写sql语句:比如查询某张数据表的数据,sql语句为:select * from table li ...
- TensorflowSharp安装和使用入门
Tensorflow是一个人工智能框架.TensorflowSharp是对Tensorflow C语言版接口的封装,便于C#开发人员在项目中使用Tensorflow. 一.使用方法 Tensorflo ...
- MySQL GTID你知多少
MySQL在5.6的版本推出了GTID复制,相比传统的复制,GTID复制对于运维更加友好,这个事务是谁产⽣,产⽣多少事务,⾮常直接的标识出来,当然GTID也有限制,对于什么是GTID可以参考我之前的文 ...
- C#.Net Core 操作Docker中的redis数据库
做软件开发的人,会在本机安装很多开发时要用到的软件,比如数据库,有MS SQL Server,MySQL,等,如果每种数据库都按照在本机确实有点乱,这个时候我们就想用虚拟机来隔离,这样就不会扰乱本机一 ...
- OnlineJudge难度与正确度的相关性检验
本着做题的心态,上了东莞理工学院的 oj 网:在选择难度的时候发现有些题目通过率和难度可能存在着某些关系,于是决定爬下这些数据简单查看一下是否存在关系. 一.新建项目 我是用 Scrapy 框架爬取的 ...
- Deep learning with Python 学习笔记(11)
总结 机器学习(machine learning)是人工智能的一个特殊子领域,其目标是仅靠观察训练数据来自动开发程序[即模型(model)].将数据转换为程序的这个过程叫作学习(learning) 深 ...
- js如何获取url参数
匹配URL参数的正则是: var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", ...
- C++程序实例唯一方案,窗口只打开一次,程序只打开一次
首先是方法: // IsAlreadyRunning - 是否已经运行 BOOL IsAlreadyRunning() { BOOL bRet = FALSE; HANDLE hMutex = ::C ...