一、什么是AOP

面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
在不影响原来功能代码的基础上,使用动态代理加入自己需要的一些功能(比如权限的验证,事务的控制,日志的记录等等),移除之后,并不影响原来的功能
面向切面编程是通过动态代理实现的,是对面向对象思想的补充。
可以提供声明式的事务管理。

aop的advice有哪些
1)before:在执行切入的方法之前,执行代码
2)after returning:在执行切入的方法正常执行(没有异常)之后,执行代码
3)after throwing:在执行切入的方法发生异常的时候,执行代码
4)after:在执行切入的方法无论是否发生异常,都必须最后执行代码

二、配置切点和切面

1)找到需要加事务的方法(方法的定位,可以类似于通配符来定位)
execution(public * cn.com.bochy.dao.impl.UserDaoImpl.insertUser(..))
开发中,事务的处理是在service层处理的,所以必须切入service层
execution(public * cn.com.bochy.service.impl.*.*(..))
2)找到之后,在方法开始之前,需要加上事务
对应advice:before
3)在方法运行中如果有异常,回滚
对应advice:after throwing
4)在方法运行中没有异常,提交
对应advice:after returning
5)无论是否有异常,关闭释放资源
对应advice:after

<!-- 注解方式完成dao层和service层的自动注入 -->
<context:component-scan base-package="com.zy"></context:component-scan>
<!-- 打开动态代理 -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> <!-- <bean name="myaop" class="com.zy.aop.MyAop"></bean> 配置切点和切面
<aop:config>
配置切点 *返回值任意 ..表示参数任意
以service中的login方法为切点 返回值任意 参数任意
<aop:pointcut expression="execution(public * com.zy.dao.impl.UserDaoImpl.login(..))" id="mypoint" />
配置切面
<aop:aspect ref="myaop"> 在切面上配advice 执行时机不同
<aop:before method="mybefore" pointcut-ref="mypoint"/>
<aop:after method="myafter" pointcut-ref="mypoint"/>
<aop:after-returning method="myafter2" pointcut-ref="mypoint"/>
<aop:after-throwing method="myafter3" pointcut-ref="mypoint"/>
</aop:aspect> </aop:config> -->

切面类

package com.zy.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Component
@Aspect
public class MyAop {
//写方法
Logger log = Logger.getLogger(this.getClass()); @Before("mypoint()")
public void mybefore(){ System.out.println("该方法在登陆之前执行");
log.info("有人准备登录系统");
} @After("mypoint()")
public void myafter(){
System.out.println("该方法在登陆之后执行");
log.info("系统完成一次登录过程");
}
@AfterReturning("mypoint()")
public void myafter2(){
System.out.println("切点无异常后会执行");
log.info("系统登录无异常");
}
@AfterThrowing("mypoint()")
public void myafter3(){
System.out.println("切点有异常后会执行");
log.info("系统登录有异常");
} //定义切点--写注解---写在某个方法上
@Pointcut("execution(public * com.zy.dao.impl.UserDaoImpl.login(..))")
public void mypoint(){}//傀儡 无任何意义 }

Test类

package test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zy.service.impl.UserServiceImpl; public class MyTest2 {
@Test
public void show(){
ClassPathXmlApplicationContext cxc = new ClassPathXmlApplicationContext("spring.xml");
//调用service再切service就会出问题
UserServiceImpl bean = cxc.getBean("userServiceImpl", UserServiceImpl.class);
//默认类名首字母小写
bean.login("小白", "123456"); //test[login()] ---- service----dao
} }

SpringMVC返回json

@ResponseBody
@RequestMapping("/get")
public Object getUser(){//研究springMVC下怎样让一个方法返回json格式数据 return us.getUserone();
}
//{"uid":2,"username":"rose","password":"654321","address":"韩国","uu":null}
//1到springMVC json包
//2spring.xml中配置 <mvc:annotation-driven ></mvc:annotation-driven>(没有这个会406)
//3controller中方法返回值改为Object
//4在controller中方法上加@ResponseBody

Spring学习笔记2的更多相关文章

  1. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  2. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  3. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

  4. Spring学习笔记2——表单数据验证、文件上传

    在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...

  5. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  6. 【Spring学习笔记-MVC-15.1】Spring MVC之异常处理=404界面

    作者:ssslinppp       异常处理请参考前篇博客:<[Spring学习笔记-MVC-15]Spring MVC之异常处理>http://www.cnblogs.com/sssl ...

  7. 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传

    作者:ssslinppp       1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...

  8. 【Spring学习笔记-MVC-9】SpringMVC数据格式化之日期转换@DateTimeFormat

    作者:ssslinppp       1. 摘要 本文主要讲解Spring mvc数据格式化的具体步骤: 并讲解前台日期格式如何转换为java对象: 在之前的文章<[Spring学习笔记-MVC ...

  9. 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回

    作者:ssslinppp      时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...

  10. 【Spring学习笔记-MVC-4】SpringMVC返回Json数据-方式2

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

随机推荐

  1. 一文详解 ARP 协议

    我把自己以往的文章汇总成为了 Github ,欢迎各位大佬 star https://github.com/crisxuan/bestJavaer 公众号连载计算机网络文章如下 ARP,这个隐匿在计网 ...

  2. Java JDK8下载 (jdk-8u251-windows-x64和jdk-8u271-linux-x64.tar)

    jdk-8u251-windows-x64 和 jdk-8u271-linux-x64.tar 链接:https://pan.baidu.com/s/1gci6aSIFhEhjY8F48qH39Q 提 ...

  3. 【Linux】history用法

    通过history命令可以查看我们在系统中输入过的命令 history命令的一些常用参数 -c  清空内存中命令历史 -d #  删除指定的历史命令,比如 history -d 100 ,就是删除第1 ...

  4. 【Oracle】静默安装oracle 11.2.0.4 超详细

    安装oracle 1.执行脚本完成初始化oracle环境 2.解压缩oracle的压缩包,单实例1个,rac是2两个压缩包 3.修改response下的db_install.rsp 修改内容如下: - ...

  5. postgresql数据库升级

    pg_upgrade官网介绍:https://www.postgresql.org/docs/10/pgupgrade.html 1.查看老版本数据库编译参数值并记录 select name,sett ...

  6. PKU2186 Popular Cows 受欢迎的牛

    题目描述 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N(N<=10000)头牛,给你M(M<=50000)对整数(A,B),表示牛A认为牛B受欢迎.这种关系是具有传递性的,如果A认为B ...

  7. [Usaco2007 Dec]Building Roads 修建道路

    题目描述 Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场 ...

  8. winform 扫码识别二维码

    因为公司业务需求,需要在Windows系统下调用摄像头识别二维码需求,就有了这个功能. 我根据网上网友提供的一些资料,自己整合应用到项目中,效果还不错(就是感觉像素不是太好) 现在将调用摄像头+识别二 ...

  9. Linux系统中的Page cache和Buffer cache

    Linux系统中的Page cache和Buffer cache Linux中有两个很容易混淆的概念,pagecache和buffercache,首先简单将一些Linux系统下内存的分布,使用free ...

  10. centralized collectors 中心化 采集器

    Fluent Bit https://fluentbit.io/ FluentBit is an open source specialized data collector. It provides ...