SpringAOP(xml文件配置)

配置文件的方式,主要是在xml文件中进行配置,不使用注解!

目录:

AtithmeticCalculator.java
public interface AtithmeticCalculator {
int add(int i,int j);
int sub(int i,int j);
int mul(int i,int j);
int div(int i,int j);
}
AtithmeticCalculatorImp1.java
@Component
public class AtithmeticCalculatorImp1 implements AtithmeticCalculator{
public int add(int i, int j) {
int res = i + j;
return res;
}
public int sub(int i, int j) {
int res = i - j;
return res;
}
public int mul(int i, int j) {
int res = i * j;
return res;
}
public int div(int i, int j) {
int res = i / j;
return res;
}
}
LoggionAspect.java
public class LoggionAspect {
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
Object [] args = joinPoint.getArgs();
System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
}
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends");
}
public void afterReturning(JoinPoint joinPoint, Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends with " + result);
}
public void afterThrowing(JoinPoint joinPoint, Exception e){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " occurs excetion:" + e);
}
}
LoggionAspect2.java
public class LoggionAspect2 {
public void beforeMethod1(JoinPoint joinPoint){
System.out.println("--->beforeMethod1");
String methodName = joinPoint.getSignature().getName();
Object [] args = joinPoint.getArgs();
System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
System.out.println("--->beforeMethod1");
} public void afterMethod1(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends");
} public void afterReturning1(JoinPoint joinPoint, Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends with " + result);
}
public void afterThrowing1(JoinPoint joinPoint, Exception e){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " occurs excetion:" + e);
}

applicationContext.xml

<!-- 配置bean -->
<bean id="atithmeticCalculator" class="com.MrChengsc.AOP.xml.AtithmeticCalculatorImp1"></bean> <!-- 配置切面的Bean -->
<bean id="loggionAspect" class="com.MrChengsc.AOP.xml.LoggionAspect"></bean>
<bean id="loggionAspect2" class="com.MrChengsc.AOP.xml.LoggionAspect2"></bean> <!-- 配置AOP -->
<aop:config>
<!-- 配置切点 表达式-->
<aop:pointcut expression="execution(* com.MrChengsc.AOP.xml.AtithmeticCalculator.*(int, int))" id="pointcut"/> <!-- 配置切面及通知 -->
<aop:aspect ref="loggionAspect2" order="">
<aop:before method="beforeMethod1" pointcut-ref="pointcut"/>
</aop:aspect>
<aop:aspect ref="loggionAspect" order="">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>

main

    public static void main(String[] args) {
// AtithmeticCalculator atithmeticCalculator = null;
// atithmeticCalculator = new AtithmeticCalculatorImp();
//
// atithmeticCalculator.add(1, 3);
// System.out.println("---");
// atithmeticCalculator.sub(3, 1);
// System.out.println("---");
// atithmeticCalculator.mul(1, 3);
// System.out.println("---");
// atithmeticCalculator.div(10, 2);
// System.out.println("---"); ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//强制的类型使用接口的类型
AtithmeticCalculator atithmeticCalculator = (AtithmeticCalculator) ctx.getBean(AtithmeticCalculator.class); int res = atithmeticCalculator.add(, );
System.out.println("res:" + res);
System.out.println("-----");
// int res1 = atithmeticCalculator.mul(2, 3);
// System.out.println("res1:" + res1); //异常代码的测试
// int res2 = atithmeticCalculator.div(10, 0);
// System.out.println("res2:" + res2);
}
The method add begins with [, ]
--->beforeMethod1
The method add begins with [, ]
--->beforeMethod1
res:
-----

注:

1.配置bean,实现aop的类

2.配置切面的bean

3.配置aop需要使用<aop:config>标签

4.使用<aop:pointcut expression="execution(* com.MrChengsc.AOP.xml.AtithmeticCalculator.*(int, int))" id="pointcut"/>

  配置切点表达式

  *:代表任意的

  两个int:可以使用   ..  进行替换

5.配置切面以及通知使用<aop:aspect>

  ref:引用已配置的切面类bean

  order:切面的优先级(数值越小优先级越大)

6.标签:

<aop:before method="beforeMethod" pointcut-ref="pointcut"/> :前置通知
<aop:after method=""/>:后置通知
<aop:after-returning method="" returning="" pointcut="">:执行成功拿返回值
<aop:around method=""/>:环绕通知
<aop:after-throwing method="" >:异常通知

属性:

pointcut-ref:引用切点表达式

 method:切面类中的方法

returning:接受返回值

pointcut:切点表达式

7.spring:SpringAOP(配置文件)的更多相关文章

  1. spring*.xml配置文件明文加密

    spring*.xml配置文件明文加密 说明:客户要求spring*.xml中Oracle/Redis/MongoDB的IP.端口.用户名.密码不能明文存放,接到需求的我,很无奈,但是还是的硬着头皮搞 ...

  2. Spring的配置文件 (SSM maven项目)

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. Spring的配置文件

    Web.xml将会配置Spring的配置文件位置: <servlet>        <servlet-name>x</servlet-name>        & ...

  4. java Spring使用配置文件读取jdbc.properties

    Spring使用配置文件读取jdbc.properties 在beans.xml中加入两个必须的bean [html]<bean id="propertyConfigurer" ...

  5. 使用JDom解析XML文档模拟Spring的配置文件解析

    在J2EE项目中可能会涉及到一些框架的使用,最近接触到了SSH,拿Spring来说配置文件的使用是相当重要的,Spring的配置文件是一个xml文件,Spring是如何读取到配置文件并进行依赖注入的呢 ...

  6. Spring Boot 配置文件详解

    Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...

  7. Spring boot 配置文件详解 (properties 和yml )

    从其他框架来看 我们都有自己的配置文件, hibernate有hbm,mybatis 有properties, 同样, Spring boot 也有全局配置文件. Springboot使用一个全局的配 ...

  8. Springboot 系列(二)Spring Boot 配置文件

    注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 不管是通过官方提供的方式获取 Spring ...

  9. java web路径和spring读取配置文件

    此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...

  10. 史上最全的Spring Boot配置文件详解

    Spring Boot在工作中是用到的越来越广泛了,简单方便,有了它,效率提高不知道多少倍.Spring Boot配置文件对Spring Boot来说就是入门和基础,经常会用到,所以写下做个总结以便日 ...

随机推荐

  1. [转]时序列数据库武斗大会之什么是TSDB

    由于工作上的关系,最近看了一些关于时序列数据库的东西,当然,我所看的也都是以开源方案为主. 趁着这股热劲还没退,希望能整理一些资料出来.如果正好你也有这方面的需求,那么希望这一系列的介绍能够帮助到你. ...

  2. Eclipse 工具栏无法移动的解决办法

    升级到Juno后发现工具栏有些乱 而且无法拖动,试了下http://blog.csdn.net/cxx504659987/article/details/38532599的方法 发现配置文件里没有文中 ...

  3. ASP.NET MVC4 新手入门教程之一 ---1.介绍ASP.NET MVC4

    你会建造 您将实现一个简单的电影清单应用程序支持创建. 编辑. 搜索和清单数据库中的电影.下面是您将构建的应用程序的两个屏幕截图.它包括显示来自数据库的电影列表的网页: 应用程序还允许您添加. 编辑和 ...

  4. mac隐藏和显示隐藏文件

    显示:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏:defaults write com.apple.finder Ap ...

  5. Hibernate学习2--对象的三种状态以及映射关系的简单配置

    上篇hibernate的博客总体简单梳理了对象持久化的一些思想以及hibernate中对象持久化化的方法,下面说说对象持久化过程的三种状态. 一.hibernate缓存的概念 1.session与缓存 ...

  6. Window 10 安装 MySQL

    1.下载 点击官网下载路径下载压缩包 点击第一个按钮后出现如下界面 下载后解压缩,我这里是放在C:\web\目录下 2.创建配置文件 注意:my.ini文件是自己创建的配置文件 文件内容如下: [my ...

  7. java读取txt文件,对字符串进行操作后导出txt文件

    嘿嘿,代码略为简单,不再多做解释,直接上码! package org.lq.com.util; import java.io.File; import java.io.InputStreamReade ...

  8. redis的安全问题

    1.修改redis.conf配置文件 2.重启redis服务,使其生效 3.成功登陆以后,使用auth+密码 或者在登录的时候使用-a 密码的授权方式

  9. Servlet开发(三)之ServletConfig,ServletContext

    1. ServletConfig Servlet是开发动态web的技术,而web.xml是Tomcat工程中最基础也最重要的配置文件,Tomcat启动项目的时候会加载并读取这个文件,其中web.xml ...

  10. 【转】百亿级实时大数据分析项目,为什么不用Hadoop?

    百亿数量级的大数据项目,软硬件总体预算只有30万左右,需求是进行复杂分析查询,性能要求多数分析请求达到秒级响应.        遇到这样的项目需求,预算不多的情况,似乎只能考虑基于Hadoop来实施. ...