项目结构:


切面类:

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. 我在Fackbook的这三年[转]

    本周开始是我在Facebook的第四个年头.我的经验在这里发生了巨大的变化:退学后我就来到了这里,在这里遇到了前所未有的挑战.单从这方面讲,我经历和遇到的挑战比这里4/5的人都要多.所以,我想分享一些 ...

  2. MaskRCNN-Keypoints

    这个月先写一篇吧,后面要复习数学考试了,可能到时候就忘了.今天写一个比较有意思的东西,关于人体的分割与姿态估计.如下图所示: 图片选自mask rcnn的论文,这里由于时间的关系,就不多叙述技术细节了 ...

  3. JavaScript经典片段

    typeof jQuery != "undefined" || importjQuery(); 判断jQuery对象是否存在,如果不存在就调用importjQuery()方法加载j ...

  4. leetcode — permutations-ii

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...

  5. Hibernate学习(九)———— 二级缓存和事务级别详讲

    序言 这算是hibernate的最后一篇文章了,下一系列会讲解Struts2的东西,然后说完Struts2,在到Spring,然后在写一个SSH如何整合的案例.之后就会在去讲SSM,在之后我自己的个人 ...

  6. Python 的反射机制

    什么叫做反射 利用字符串的形式去对象(模块)中操作(查找/添加/获取/删除)成员,一种基于字符串的事件驱动. 可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型.然后,可以 ...

  7. 利用aiohttp制作异步爬虫

      asyncio可以实现单线程并发IO操作,是Python中常用的异步处理模块.关于asyncio模块的介绍,笔者会在后续的文章中加以介绍,本文将会讲述一个基于asyncio实现的HTTP框架--a ...

  8. 一个Fragment的实例

    @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method s ...

  9. C#基础知识总结(二)

    摘要 第二篇主要讲:变量.连接符占位符等.转义字符.数据的计算.数据的转换.try-catch的简单熟悉.复合运算符和自加自减 一.变量 1.数据存储在内存中:内存叫做RAM,内存被分隔为一小格一小格 ...

  10. [android] 内容提供者实现

    [android] 内容提供者实现 上一节的主机名类似网络上的域名,协议是content://,可以定义一下规则 content://主机名/insert 添加操作 content://主机名/del ...