D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\aop.xml

<?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="com.xiya.spring.aop"/>
<!--使AspectJ注释起作用:自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy/>
</beans>

<context:component-scan base-package="com.xiya.spring.aop"/>

在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,

如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean

com/xiya/spring/aop/advice/LoggingAspect.java

package com.xiya.spring.aop.advice;

/**
* Created by N3verL4nd on 2017/3/24.
*/ import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; import java.util.Arrays;
import java.util.List; /**
* 如何把这个类声明为一个切面:
* 1、把该类放入IOC容器中
* 2、再声明为一个切面
*/
@Aspect
@Component
public class LoggingAspect {
//声明该方法是一个前置通知:在目标方法执行之前执行
@Before("execution(public int com.xiya.spring.aop.service.impl.ArithmeticCalculatorImpl.*(int,int))")
public void doBefore(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " begins with " + args); } //后置通知:在目标方法执行后(无论是否发生异常)执行的通知
//在后置通知中还不能访问目标方法执行的结果
@After("execution(* com.xiya.spring.aop.service.impl.ArithmeticCalculatorImpl.*(int,int))")
public void doAfter(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("the method " + methodName + " ends!");
}
}

com/xiya/spring/aop/service/ArithmeticCalculator.java

package com.xiya.spring.aop.service;

/**
* Created by N3verL4nd on 2017/3/24.
*/
public interface ArithmeticCalculator {
int add(int x, int y);
int sub(int x, int y);
int mul(int x, int y);
int div(int x, int y);
}

com/xiya/spring/aop/service/impl/ArithmeticCalculatorImpl.java

package com.xiya.spring.aop.service.impl;

import com.xiya.spring.aop.service.ArithmeticCalculator;
import org.springframework.stereotype.Component; /**
* Created by N3verL4nd on 2017/3/24.
*/
@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int x, int y) {
int result = x + y;
//int z = 1 / 0;
return result;
} @Override
public int sub(int x, int y) {
int result = x - y;
return result;
} @Override
public int mul(int x, int y) {
int result = x * y;
return result;
} @Override
public int div(int x, int y) {
int result = x / y;
return result;
}
}

com/xiya/spring/aop/test/Main.java

package com.xiya.spring.aop.test;

import com.xiya.spring.aop.service.ArithmeticCalculator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by N3verL4nd on 2017/3/24.
*
*/
public class Main {
public static void main(String[] args) {
//1、创建Spring的IOC容器
ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml"); //2、从IOC容器中获取bean实例
ArithmeticCalculator calculator = context.getBean(ArithmeticCalculator.class); //3、使用bean
int result = calculator.add(10, 20);
System.out.println("result = " + result); result = calculator.div(10, 2);
System.out.println("result = " + result);
}
}

Spring基于注解配置AOP的更多相关文章

  1. 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置

    复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...

  2. Spring 基于注解的AOP实现

    在本文开始之前,我要引入一张图,这张图的来源 https://blog.csdn.net/chenyao1994/article/details/79708496 ,版权归原作者所有,我借鉴了原作者的 ...

  3. Spring基于XML配置AOP

    目录结构: D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\service\StudentService.java pac ...

  4. spring-AOP框架(基于AspectJ注解配置AOP)

    基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...

  5. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

  6. Spring 基于注解零配置开发

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...

  7. Spring注解配置Aop

    之前学习了SpringAop的基本原理.http://www.cnblogs.com/expiator/p/7977975.html 现在尝试使用注解来配置SpringAop. Aop,面向切面编程. ...

  8. Spring 基于 AspectJ 的 AOP 开发

    Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...

  9. Spring基于注解的Cache支持

    Spring为我们提供了几个注解来支持Spring Cache.其核心主要是@Cacheable和@CacheEvict.使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回 ...

随机推荐

  1. 啊哈!C语言课后参考答案上

    最近看到一本好评量很高的的C语言入门书,课本真的很好,入门的话.专业性没有那么强,但入门足够了!!好评!看着看着就想把这本书的题课后习题都写出来,最后就有了这个小结.可能有的不是最好,不那么专业,但主 ...

  2. nginx介绍及相关实验

    一.nginx介绍 1.nginx简介 Nginx是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP 服务.Nginx 是由伊戈尔·赛索耶夫为俄罗斯访问量第二的 R ...

  3. (一)Angular+spring-security-cas前后端分离(基于ticket)认证时序图

    一.静态资源认证时序图 2. ajax请求认证拦截时序图 3.退出登录时序图

  4. ArcEngine 里面的日期

    问题: 将自己做的GIS系统放到其他系统上的时候发现用 IQueryFilter 进行时间查询的时候报错,原来的系统没有这个问题. 原因: 后来调试代码发现查询的时间里面有中文,显示格式 " ...

  5. .Net Core Web Api实践(三).net core+Redis+docker实现Session共享

    前言:上篇文章介绍了.net core+Redis+IIS+nginx实现Session共享,本来打算直接说明后续填坑过程,但毕竟好多坑是用docker部署后出现的,原计划简单提一下.net core ...

  6. Epplus Excel 导入 MSSQL 数据库

    效果: 下载EXE     源码

  7. 区间dp - 全部按下一列石头

    There is one last gate between the hero and the dragon. But opening the gate isn't an easy task. The ...

  8. Redux 一步到位

    简介 Redux 是 JavaScript 状态容器,提供可预测化的状态管理 Redux 除了和 React 一起用外,还支持其它库( jquery ... ) 它体小精悍(只有2kB,包括依赖) 由 ...

  9. Nginx配置文件模板

    主配置文件nginx.conf user nginx; #设置nginx服务的系统使用用户 worker_processes 1; #工作进程数(和cpu核心数保持一致) error_log /var ...

  10. Burpsuite设置拦截response

    一.Burpsuite设置拦截HTTP/HTTPS代理 1, .拦截修改request 首先进入Proxy-Options-Intercept Client Requests设置request拦截的规 ...