本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用.

转载请注明 出自 : luogg的博客园 谢谢配合!

Spring_day02

一.AOP面向切面编程

1.1 什么是AOP

AOP就是面向切面编程,通过预编译的方式和运行期动态代理实现程序功能的统一维护的技术.

主要的功能是 : 日志记录,性能统计,安全控制,事务处理,异常处理等.

1.2 AOP实现方式

一. 预编译

  • AspectJ

二.运行期动态代理

  • JDK动态代理
  • CGLIB动态代理

1.3 Spring中的切面类型

  • 1.Advisor : spring中传统切面,

    Advisor : 都是一个切点和一个通知组成

    Aspect : 多个切点和多个通知组成

Advisor : 代表一般切面,Advice本身就是一个切面,对目标类所有方法进行拦截(* 不带有切点的切面.针对所有方法进行拦截)

PointcutAdvisor : 代表具有切点的切面,可以指定拦截目标类哪些方法(带有切点的切面,针对某个方法进行拦截)

1.4 Spring中AOP开发(针对所有方法增强)

  • 1.导入相应的jar包

    spring-aop-3.2.0.RELEASE.jar AOP联盟的jar包

    com.springsource.org.aopalliance-1.0.0.jar 依赖包

  • 2.编写被代理的对象:

    CustomerDao 接口

    CustomerDaoImpl 实现类

  • 3.编写增强的代码

public class MyBeforeAdvice implements MethodBeforeAdvice{

	@Override
/**
* method:执行的方法
* args:方法的参数
* target:目标对象
*/
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置增强");
}
}
  • 4.生成代理(通过配置生成代理)

1.5 Spring的AspectJ的AOP

AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。

AspectJ是一个基于Java语言的AOP框架

Spring2.0以后新增了对AspectJ切点表达式支持

@AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面

新版本Spring框架,建议使用AspectJ方式来开发AOP

AspectJ表达式:

  • 语法:execution(表达式)

    execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

  • execution(“* cn.itcast.spring3.demo1.dao.*(..)”) ---只检索当前包

  • execution(“* cn.itcast.spring3.demo1.dao..*(..)”) ---检索包及当前包的子包.

  • execution(* cn.itcast.dao.GenericDAO+.*(..)) ---检索GenericDAO及子类

AspectJ增强(注解):

  • @Before 前置通知,相当于BeforeAdvice,没有办法阻止目标方法的执行
    /**
* 前置增强
*/
@Before("execution(* com.luogg.demo1.UserDao.add(..))")
public void before(){
System.out.println("前置增强===>");
}
  • @AfterReturning 后置通知,相当于AfterReturningAdvice,可以获得方法的返回值
    /*
* 后置增强
*/
@AfterReturning(returning="returnVal",value="execution(* com.luogg.demo1.UserDao.delete(..))")
public void after(Object returnVal){
System.out.println("后置增强===>方法的返回值为:" + returnVal);
}
  • @Around 环绕通知,相当于MethodInterceptor,而且可以阻止目标方法的执行,在前边加个if判断即可.
    /*
* 环绕增强
*/
@Around(value="execution(* com.luogg.demo1.UserDao.find(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕前增强");
proceedingJoinPoint.proceed();
System.out.println("环绕后增强");
}
  • @AfterThrowing抛出通知,相当于ThrowAdvice
    /**
* 抛出异常
*/
@AfterThrowing(value="execution(* com.luogg.demo1.UserDao.find(..))",throwing="e")
public void err(Throwable e){
System.out.println("出异常了"+e.getMessage());
}
  • @After 最终final通知,不管是否异常,该通知都会执行
    /*
* 最终通知
*/
@After("execution(* com.luogg.demo1.UserDao.find(..))")
public void after(){
System.out.println("最终通知");
}
  • @DeclareParents 引介通知,相当于IntroductionInterceptor (不要求掌握)

基于注解

  • 第一步 : 引入jar包.

    aspectj依赖aop环境.

    spring-aspects-3.2.0.RELEASE.jar

    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

  • 第二步 : 编写被代理对象UserDao

/**
* 编写被代理对象 用户dao层
* @author luogg
*
*/ @Repository("userDao")
public class UserDao {
public void add(){
System.out.println("添加用户");
}
public void delete(){
System.out.println("删除用户");
}
public void update(){
System.out.println("修改用户");
}
public void find(){
System.out.println("查询用户");
}
}
  • 第三步 : 使用AspectJ注解形式 定义切面,并定义前置增强
/**
* 定义一个切面,就是切点和增强的结合
* @author luogg
*
*/
@Service("myAspect")
@Aspect //定义切面
public class MyAspect { /**
* 前置增强
*/
@Before("execution(* com.luogg.demo1.UserDao.*(..))")
public void before(){
System.out.println("前置增强");
}
}
  • 第四步 : 配置applicationContext.xml配置文件,开启自动生成代理,并扫描bean
    <!-- 开启自动生成代理 -->
<aop:aspectj-autoproxy/> <!-- 去扫描注解装配的Bean -->
<context:component-scan base-package="com.luogg.demo1"></context:component-scan>
  • 第五步 : 编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest1 {
@Autowired
//@Qualifier("userDao")
private UserDao userDao; @Test
public void test1(){
userDao.add();
userDao.delete();
userDao.update();
userDao.find();
}
}

输出结果

添加用户

前置增强

删除用户

前置增强

修改用户

前置增强

查询用户

切点的定义

在注解中写切点太麻烦了,直接同意定义,然后通过类名.方法名调用同意的切点.

@Pointcut("execution(* com.luogg.demo1.UserDao.find(..))")
public void myPointCut(){} /*
* 最终通知
*/
@After("MyAspect.myPointCut()")
public void after(){
System.out.println("最终通知");
}

面试:

  • Advisor和Aspect的区别?
  • Advisor:Spring传统意义上的切面:支持一个切点和一个通知的组合.
  • Aspect:可以支持多个切点和多个通知的组合.

1.6 Spring的JdbcTemplate

Spring对持久层技术的支持

JDBC : org.springframework.jdbc.core.JdbcTemplate

Hibernate3.0 : org.springframework.orm.hibernate3.HibernateTemplate

IBatis(MyBatis) : org.springframework.orm.ibatis.SqlMapClientTemplate

JPA : org.springframework.orm.jpa.JpaTemplate

二.Spring的JDBC模板

2.1 DBCP连接池

导入jar包:
* com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
* com.springsource.org.apache.commons.pool-1.5.3.jar <!-- 配置DBCP连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///spring3_day02"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>

2.2 C3P0连接池

导入jar包:
* com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring3_day02"/>
<property name="user" value="root"/>
<property name="password" value="123"/>
</bean>

2.3 将参数设置到属性文件jdbc.properties中

在src下创建jdbc.properties

jdbc.driver = com.mysql.jdbc.Driver

jdbc.url = jdbc:mysql:///spring3_day02

jdbc.user = root

jdbc.password = 123

需要在applicationContext.xml 中使用属性文件配置的内容.
* 第一种写法:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> * 第二种写法:
<context:property-placeholder location="classpath:jdbc.properties"/>

2.4 jdbcTemplate 的 CRUD 操作

Spring学习_day02_AOP,AspectJ,JdbcTemplate的更多相关文章

  1. Spring学习--用 ASpectJ 注解实现 AOP

    用 AspectJ 注解声明切面: 要在 Spring 中声明 AspectJ 切面 , 只需要在 IOC 容器中将切面声明为 bean 实例.当在 Spring IOC 容器中初始化 AsjectJ ...

  2. Spring学习笔记:jdbcTemplate和数据源配置

    一.使用Spring框架jdbcTemplate实现数据库的增删改查 1.数据库 /* SQLyog Ultimate v8.32 MySQL - 5.7.19-log : Database - in ...

  3. Spring学习之Aspectj开发实现AOP

    Aspectj是一个基于Java语言的Aop框架,它提供了强大的Aop功能. Aspectj简介: 1.Aspectj是一个面向切面的框架,它扩展了Java语言,它定义了一个Aop语法. 2.所以它有 ...

  4. [原创]java WEB学习笔记105:Spring学习---AOP介绍,相关概念,使用AOP,利用 方法签名 编写 AspectJ 切入点表达式

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  6. spring 学习(四): spring 的 jdbcTemplate 操作

    spring 学习(四): spring 的 jdbcTemplate 操作 spring 针对 javaee 的每一层,都提供了相应的解决技术,jdbcTemplate 的主要操作在 dao 层. ...

  7. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

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

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

  9. Spring学习(一)--Spring的设计与整体架构

    之前只是在学校里大概的学习了一下Spring框架的使用以及一些最基本.浅显的原理,并没有做出深入的学习,等到工作之后想提升自己的时候发现所掌握的Spring框架的简直烂如狗屎,为监督自己的学习进度,立 ...

随机推荐

  1. Nginx 重写规则指南1

    作者:运维生存时间 - 默北 链接:www.ttlsa.com/nginx/nginx-rewriting-rules-guide/ 当运维遇到要重写情况时,往往是要程序员把重写规则写好后,发给你,你 ...

  2. maven更改镜像路径为阿里镜像,以便下载速度快

    1.maven更改镜像路径为阿里镜像,以便下载速度快 2.maven每更新一次镜像地址,都会重新下载一次包 3. 怎么配maven链接阿里云的镜像详细步骤 修改maven根目录下的conf文件夹中的s ...

  3. docker: 解决centos7下cgroup.procs: no such device的错误

    在centos7下,运行docker run的时候会发生cgroup.procs: no such device的错误,解决方法是编辑 /lib/systemd/system/docker.servi ...

  4. How can I add files to a Jar file? (or add a file to a zip archive)

    https://stackoverflow.com/questions/12239764/how-can-i-add-files-to-a-jar-file M.java class M{ publi ...

  5. 相当精简的CentOS个人桌面版--从CentOS6.3 32b-mini版開始(mini版过程略)

    利用网络实现相当精简的CentOS个人桌面版--从CentOS6.3 32位mini版開始(mini版过程略).升级后即是CENTOS6.5. 特别感谢163网易的镜像空间[http://mirror ...

  6. Python网络爬虫(一):初步认识网络爬虫

    不管你是因为什么原因想做一个网络爬虫,首先做的第一件事情就是要了解它. 在了解网络爬虫之前一定要牢记下面4个要点,这是做网络爬虫的基础: 1.抓取 py的urllib不一定去用.可是要学.假设你还没用 ...

  7. js 实现对ajax请求面向对象的封装

             AJAX 是一种用于创建高速动态网页的技术.通过在后台与server进行少量数据交换.AJAX 能够使网页实现异步更新.这意味着能够在不又一次载入整个网页的情况下,对网页的某部分进行 ...

  8. Wireshark 抓包遇到 you don’t have permission to capture on that device mac 错误的解决方案

    Wireshark 抓包遇到 you don’t have permission to capture on that device mac 错误的解决方案 上次有篇博客讲了如何利用wireshark ...

  9. openGl学习之基本图元

    从本篇開始,会给出一些代码实例,所以要配置好编译环境. 环境配置: vs2012下配置链接http://www.cnblogs.com/dreampursuer/archive/2014/05/27/ ...

  10. POJ 3683 Priest John&#39;s Busiest Day (2-SAT+输出可行解)

    题目地址:POJ 3683 第一次做须要输出可行解的题目. . .大体思路是先用强连通来推断是否有可行解,然后用逆序建图.用拓扑排序来进行染色.然后输出可行解. 详细思路见传送门 由于推断的时候少写了 ...