1 spring容器中bean特性

Spring容器的javabean对象默认是单例的。

通过在xml文件中,配置可以使用某些对象为多列。

Spring容器中的javabean对象默认是立即加载(立即实例化:spring加载完成,立即创建对象)

scope:属性

singleton:默认值为单例,默认也是立即加载,在加载完成spring容器的时候,bean对象已经创建完成

prototype:多例的,默认懒加载,spring容器加载完成的时候,不会创建bean的对象,只有从容器获得bean对象的时候,才进行bean对象的实例化

request: 将创建的javabean对象,封装到request范围

session:将创建的javabean对象,封装到session范围

Spring容器bean的对象生命周期:

Bean对象的创建一直到销毁为bean的生命周期。

生命周期的开始:

如果为单例,由加载完spring容器开始

如果为多例,由从容器获得bean对象开始

实例化

初始化

服务

销毁(单例:关闭容器的时候,多例由jvm自动回收)

2 spring的AOP面向切面编程

2.1 模拟银行转账业务

需求:实现银行的转账功能,在转账的时候需要完成

1 身份认证(登陆)

2 权限的验证

3 转账实现

4 历史交易记录,

分析:1,2,4三个功能对于银行的业务,属于公共的功能(共性的功能)

在功能实现的时候,需要将1,2,4抽取出来,单独实现,

做到了将共性的功能和核心的业务功能进行了分离

通过动态代理实现:共性的功能和核心业务功能的合并,产生核心业务对象的

在代码实现的时候,进行了功能实现的分离:

代码开发的进行分离,程序在运行的时候进行合并。

2.2 springAOP的思想

在系统开发中,将系统的共性的公共的功能独立实现,在程序运行的过程中,将共性功能和核心的业务功能,进行整合。

好处:

1 完成共性功能和核心业务功能的解耦合

2 提供共性功能的复用性。

2.3springAOP的概念

Aspect切面:封装共性功能的(增强功能的)类

Advice通过:切面类中封装的增强功能的方法。

PointCut:切入点,是一个集合的概念,该集合的表达使用一个正则表达式表达

所有核心业务对象的所有方法的前后(事务处理AOP典型的应用)

JoinPoint:连接点,程序中需要加入advice的地方,而且正在执行的ponitCut

织入(Weaving):将aspect和核心业务对象,进行整合的过程。

3 springAOP的实现

3.1通过特定接口实现

Aop通知的类型:

Before:前置通知

After:后置通知

Around:环绕通知

Throwing:异常通知

需求:实现在业务对象中的方法执行的时候,记录日志功能

3.1.1前置通知

 package org.guangsoft.utils;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import org.springframework.aop.MethodBeforeAdvice;
/****
* 前置增强:
* MethodBeforeAdvice 接口表示重写的方法为前置advice
* ***/
public class BeforeLog implements MethodBeforeAdvice
{
@Override
public void before(Method method,
Object[] args, Object obj)
throws Throwable
{
System.out.println(method);
System.out.println(Arrays.toString(args));
System.out.println(obj);
System.out.println("BeforeLog-------------" + new Date());
}
}

AOP配置:

 <?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 实例化BeforeLog对象 -->
<bean id="bf" class="org.guangsoft.utils.BeforeLog"></bean>
<!-- 实例化service对象 -->
<bean id="us" class="org.guangsoft.service.impl.UsersServiceImpl" />
<!-- 进行aop的配置,产生代理对象 -->
<aop:config>
<!-- 声明切入点 -->
<aop:pointcut expression="execution(* org.guansoft.service.impl.*.*(..))"
id="pc" />
<!-- 织入 将通知和切入点进行合并(切面+核心业务对象) -->
<aop:advisor advice-ref="bf" pointcut-ref="pc" />
</aop:config>
</beans>

3.1.2后置通知

对业务对象的方法进行后增强。

 package org.guangsoft.utils;
import java.lang.reflect.Method;
import java.util.Date;
import org.springframework.aop.AfterReturningAdvice;
/***
* 后置通知
* ***/
public class AfterLog implements AfterReturningAdvice
{
@Override
public void afterReturning(Object obj1,// obj1 接收目标方法的返回值
Method method,
Object[] args,
Object obj2) throws Throwable
{
// System.out.println(obj1+"----------------------"+obj2);
System.out.println("AfterLog-------------------" + new Date());
}

AOP配置:

 <?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 实例化BeforeLog对象 -->
<bean id="bf" class="org.guangsoft.utils.BeforeLog"></bean>
<bean id="af" class="org.guangsoft.utils.AfterLog"></bean>
<!-- 实例化service对象 -->
<bean id="us" class="org.guangsoft.service.impl.UsersServiceImpl" />
<!-- 进行aop的配置,产生代理对象 -->
<aop:config>
<!-- 声明切入点 -->
<aop:pointcut expression="execution(* org.guangsoft.service.impl.*.*(..))"
id="pc" />
<!-- 织入 将通知和切入点进行合并(切面+核心业务对象) -->
<aop:advisor advice-ref="bf" pointcut-ref="pc" />
<aop:advisor advice-ref="af" pointcut-ref="pc" />
</aop:config>
</beans>

3.1.3环绕通知

 package org.guangsoft.utils;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/***
* 环绕通知
* ***/
public class AoundLog implements MethodInterceptor
{
/**
* MethodInvocation中封装了目标对象,调用的方法,方法需要的参数
* ***/
@Override
public Object invoke(MethodInvocation mi) throws Throwable
{
Method method = mi.getMethod();
Object[] args = mi.getArguments();
Object obj = mi.getThis();
System.out.println(method);
System.out.println(Arrays.toString(args));
System.out.println(obj);
System.out.println("around------before--------" + new Date());
Object rv = method.invoke(obj, args);// 调用目标对象的方法,放行
System.out.println("around------after--------" + new Date());
return rv;
}
}

AOP配置:同上

3.1.4 异常通知

 package org.guangsoft.utils;
import java.util.Date;
import org.springframework.aop.ThrowsAdvice;
/****
* 异常通知
* **/
public class ExceptionLog implements ThrowsAdvice
{
/***
* 该类中的方法参考AfterReturningAdvice写
* 该参数是用来接收异常信息的
* ***/
public void afterThrowing(Throwable ex) throws Throwable
{
// System.out.println(obj1+"----------------------"+obj2);
System.out.println("ExceptionLog-----------" + ex.getMessage()
+ "--------" + new Date());
}
}

Pointcut:核心业务对象

Advice:通知

Spring面向切面编程(AOP)的更多相关文章

  1. Spring面向切面编程(AOP,Aspect Oriented Programming)

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  2. Spring面向切面编程(AOP)方式二

    使用注解进行实现:减少xml文件的配置. 1 建立切面类 不需要实现任何特定接口,按照需要自己定义通知. package org.guangsoft.utils; import java.util.D ...

  3. Spring面向切面编程AOP(around)实战

    spring aop的环绕通知around功能强大,我们这里就不细说,直接上代码,看着注释就能明白 需要的可以点击下载源码 1.如果使用注解的方式则需要先创建个注解类 package com.mb.a ...

  4. Spring学习手札(二)面向切面编程AOP

    AOP理解 Aspect Oriented Program面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 但是,这种说法有些片面,因为在软件工程中,AOP的价值体现的并 ...

  5. Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)

    一.面向切面编程AOP 目标:让我们可以“专心做事”,避免繁杂重复的功能编码 原理:将复杂的需求分解出不同方面,将公共功能集中解决 *****所谓面向切面编程,是一种通过预编译方式和运行期动态代理实现 ...

  6. Spring框架学习笔记(2)——面向切面编程AOP

    介绍 概念 面向切面编程AOP与面向对象编程OOP有所不同,AOP不是对OOP的替换,而是对OOP的一种补充,AOP增强了OOP. 假设我们有几个业务代码,都调用了某个方法,按照OOP的思想,我们就会 ...

  7. Spring之控制反转——IoC、面向切面编程——AOP

      控制反转——IoC 提出IoC的目的 为了解决对象之间的耦合度过高的问题,提出了IoC理论,用来实现对象之间的解耦. 什么是IoC IoC是Inversion of Control的缩写,译为控制 ...

  8. Spring 面向切面编程(AOP)

    Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...

  9. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

随机推荐

  1. debian8 配置使用 nfs

    操作过的步骤: 1.dpkg-reconfigre rpcbind. 2.在终端上退出要挂载的目录. 错误:mount -t nfs 172.16.0.121:/home/junda /mnt,出现以 ...

  2. redis--key1

    package com.ztest.redis; import java.util.Set; import com.sun.istack.internal.logging.Logger; import ...

  3. Effective Java 学习笔记之所有对象都通用的方法

    一.覆盖equals时请遵守通用约定 1.满足下列任何一个条件时,不需要覆盖equals方法 a.类的每个实例本质上都是唯一的.此时就是Object中equals方法所表达的含义. b.不关心类是否提 ...

  4. Codeforces 260 B. Fedya and Maths

    题目链接:http://codeforces.com/contest/456/problem/B 解题报告:输入一个n,让你判断(1n + 2n + 3n + 4n) mod 5的结果是多少?注意n的 ...

  5. python 环境安装

    wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz  tar zxf Python-2.7.3.tgz  cd Python-2. ...

  6. [KOJ6023]合并果子·改

    [COJ6023]合并果子·改 试题描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多把这些果子堆排成一排,然后所有的果子合成一堆.    每一次合并,多多可以 ...

  7. Oracle 管道化表函数

    在PL/SQL中,如果要返回数据的多个行,必须通过返回一个REF CURSOR的游标,或者一个数据集合(如临时表或物理表)来完成,而REF CURSOR的局限于可以从查询中选择的数据,而数据集合的局限 ...

  8. linux 文件系统sysvinit 流程分析

    参考网上许多的教程. 然后有一下相关的笔记: kernel 在挂载完文件系统后,会执行第一个进程init 这个进程的PID为1 这个进程是所有进程的父进程 init 进程,首先要去读取inittab中 ...

  9. BZOJ 2822: [AHOI2012]树屋阶梯

    Description 求拼成阶梯状的方案数. Sol 高精度+Catalan数. 我们可以把最后一行无线延伸,所有就很容易看出Catalan数了. \(f_n=f_0f_{n-1}+f_1f_{n- ...

  10. 17.2---#字棋(CC150)

    牛客网的在线题.思路,比较简单.就是判断一下是否有连起来的1. public static boolean checkWon(int[][] board){ boolean res = false; ...