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 ...
随机推荐
- TCP/IP 笔记 - 安全
安全:可扩展身份认证协议.IP安全协议.传输层安全.DNS安全.域名密钥识别邮件 任何由用户或以用户账号执行却违背了用户本身意愿的软件被称为恶意软件 网络安全是一门十分广泛及有深度的学识,而本书旨在了 ...
- python scrapy 爬取西刺代理ip(一基础篇)(ubuntu环境下) -赖大大
第一步:环境搭建 1.python2 或 python3 2.用pip安装下载scrapy框架 具体就自行百度了,主要内容不是在这. 第二步:创建scrapy(简单介绍) 1.Creating a p ...
- Jenkins的一些笔记
公司主要要开发自己的paas平台,集成了Jenkins,真的是遇到了很多很多困难,特别是在api调用的权限这一块,这里,把自己遇到的一些坑的解决方法做一下笔记吧.当然,首先要讲的,就是如何在开启安全的 ...
- 正确重写hashcode hashcode与equals方法 集合元素如何判断是否相等 集合如何查看是否包含某个元素
首先记住两句话 相等的两个对象,即equals(Object)方法判断两个对象相等,那么他们必须要有相同的hashcode hashcode相同的两个对象,他们可能相同,也可能不相同 简单地说可以这么 ...
- MySQL的GROUP_CONCAT函数
先根据如下语句生成测试表并填充数据 CREATE TABLE z ( a INT, b INT); INSERT INTO Z SELECT 1,200; INSERT INTO Z SELECT 1 ...
- Docker的基本操作与示例
一.RunC RunC是一个由OCI(Open Container Initiative)制定的标准化轻量容器运行工具.OCI是专门致力于制定容器格式和运行时开放的工业化标准的组织.那容器标准化后Do ...
- shell编程基础(二): shell脚本语法之分支语句和循环语句
一.分支语句 1.条件测试:test [ 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为1(注意与 ...
- oracle 汉字转化拼音函数
FN_GETPY('中华人民共和国') -------------------------------------------------------------------------------- ...
- 4.6 explain 之 rows
一.说明 根据表统计信息及索引选用情况,大致估算出找到所需的记录所需读取的行数. 二.示例 关注我的公众号,精彩内容不能错过
- 通过示例学习JavaScript闭包
译者按: 在上一篇博客,我们通过实现一个计数器,了解了如何使用闭包(Closure),这篇博客将提供一些代码示例,帮助大家理解闭包. 原文: JavaScript Closures for Dummi ...