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 ...
随机推荐
- leetcode — add-binary
/** * Source : https://oj.leetcode.com/problems/add-binary/ * * * Given two binary strings, return t ...
- Go实用开源库收集
框架 https://github.com/go-martini/martini 图形验证码 https://github.com/dchest/captcha ORM https://github. ...
- RabbitMQ系列(三)RabbitMQ交换器Exchange介绍与实践
RabbitMQ交换器Exchange介绍与实践 RabbitMQ系列文章 RabbitMQ在Ubuntu上的环境搭建 深入了解RabbitMQ工作原理及简单使用 RabbitMQ交换器Exchang ...
- tp3.2多个或者并且语句语法
$map['tid1&cid1'] =array($jmid,'0','_multi'=>true); $map['tid2&cid2'] =array($jmid,'0','_ ...
- 华为路由器 HDLC 实验
HDLC 简介 高级数据链路控制(High-Level Data Link Control 或简称 HDLC),是一个在同步网上传输 数据.面向比特的数据链路层协议,它是由国际标准化组织(ISO)根据 ...
- #if 与 #ifdef 之间的区别
先来看个例子: #define TARGET_LITTLE_ENDINA 1 #define TARGET_BIG_ENDINA 0 #ifdef TARGET_LITTLE_ENDINA call ...
- ListView的setOnItemClickListener位置错乱问题
如果你对一个ListView同时addHeaderView(listhHeaderView),也就是头部视图,再加setAdapter,当你加上setOnItemClickListener事件后你会发 ...
- asp.net core webapi 生成导出excel
/// <summary> /// 下载订单 /// </summary> /// <param name="model"></param ...
- 12个非常有用的JavaScript技巧
在这篇文章中,我将分享12个非常有用的JavaScript技巧.这些技巧可以帮助你减少并优化代码. 1) 使用!!将变量转换成布尔类型 有时,我们需要检查一些变量是否存在,或者它是否具有有效值,从而将 ...
- EF(EntityFramework) 插入或更新数据报错
报错信息:Store update, insert, or delete statement affected an unexpected number of rows (0). Entities m ...