一、AOP 的概念

AOP(Aspect Oriented Programming)的缩写,面向切面编程,主要作用就是对代码进行增强处理。

理解面向切面编程的含义:就是在不改变原有程序的基础上为代码增加新的功能。

实现面向切面编程需要了解两个概念:

>切入点:可以插入增强处理的方法,比如原对象的fun()方法。

>增强处理类型:在原对象fun()方法之前还是之后插入新功能。

二、Spring AOP简单应用

1.新建一个java项目

2.到官网下载Spring AOP和AspectJ框架所需要的jar包,并将下载所需的jar文件添加到项目中。

3.在项目中添加User实体类

package com.jbit.fsd.entity;

public class User {

	private int id;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
} }

4.添加数据访问层接口和实现类,并添加相应的方法

package com.jbit.fsd.dao;

import java.util.List;

import com.jbit.fsd.entity.User;
// 数据访问层接口
public interface UserDao { public List<User> getAll(); public void addUser(User user);
}

实现类:

package com.jbit.fsd.dao.impl;

import java.util.ArrayList;
import java.util.List; import com.jbit.fsd.dao.UserDao;
import com.jbit.fsd.entity.User; public class UserDaoImpl implements UserDao { @Override
public List<User> getAll() {
//操作数据库读到所有数据
List<User> list=new ArrayList<User>();
list.add(new User());
list.add(new User());
list.add(new User());
return list;
} @Override
public void addUser(User user) {
System.out.println("添加成功");
} }

5.添加业务处理层接口和实现类

package com.jbit.fsd.service;

import java.util.List;

import com.jbit.fsd.dao.UserDao;
import com.jbit.fsd.entity.User; public interface UserService { public List<User> getAll(); public void addUser(User user); }

实现类:

package com.jbit.fsd.service.impl;

import java.util.List;

import com.jbit.fsd.dao.UserDao;
import com.jbit.fsd.entity.User;
import com.jbit.fsd.service.UserService; public class UserServiceImpl implements UserService{
private UserDao dao;
//通过spring注入进来
public void setDao(UserDao dao) {
this.dao = dao;
} @Override
public List<User> getAll() {
System.out.println("正在执行添加用户的业务!");
return dao.getAll();
} @Override
public void addUser(User user) {
System.out.println("正在执行添加用户的业务!");
dao.addUser(user);
} }

6.添加AOP增强处理类

package com.jbit.fsd.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; public class ServiceLoging {
private Log log = LogFactory.getLog(this.getClass()); public void beforeService(){
log.info("aop方法被调用!");
}
}

7.在src目录下新建applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 以面向接口思想编程实现解耦和 -->
<bean id="userDao" class="com.jbit.fsd.dao.impl.UserDaoImpl">
</bean>
<bean id="userServiceImpl" class="com.jbit.fsd.service.impl.UserServiceImpl">
<property name="dao" ref="userDao"></property> <!-- 属性注入 -->
</bean>
<!-- 配置切面 -->
<bean id="serviceLogging" class="com.jbit.fsd.aop.ServiceLoging"></bean>
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(public void addUser(com.jbit.fsd.entity.User))" id="servicePointcut"/>
<!-- 将切面和切入点编制在一起-->
<aop:aspect ref="serviceLogging">
<aop:before method="beforeService" pointcut-ref="servicePointcut"/>
</aop:aspect>
</aop:config>
</beans>

8.main方法测试:

package com.jbit.fsd.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jbit.fsd.dao.UserDao;
import com.jbit.fsd.entity.User;
import com.jbit.fsd.printer.Printer;
import com.jbit.fsd.service.UserService;
import com.jbit.fsd.service.impl.UserServiceImpl; public class Test { /**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService dao=(UserService) ac.getBean("userServiceImpl");
dao.addUser(new User());
} }

说明:

1.在.xml文件中需要添加aop命名空间,导入与aop配置相关的标签。

2.AOP相关的配置在<aop:config>标签中,切入点的配置在<aop:pointcut>标签中。

3.execution是切入点的标识符,括号是切入点的表达式,配置需要增强的方法。

4.切入点表达式常用几种模糊匹配方式:

>public * addUser(com.able.entity.User),“*”表示配置所有类型的返回值。

>public void * (com.able.entity.User), "*"代表匹配所有的方法名。

>public void addUser(..),  " .. "表示匹配所有参数个数和类型。

> * com.able.service.*.*( . . ),  表示匹配com.able.service包下面所有类的所有方法。

> * com.able.service . . *(), 表示匹配com.able.service包以及子包下所有类的所有方法。

三、增强处理类型简介

1.前置增强处理

特点:在目标方法前织入增强处理。

<aop:before method="beforeService" poontcut-ref="servicePointcut"/>

如果想在这个方法中获得切入点的信息,使用如下方法:

package com.jbit.fsd.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint; public class ServiceLoging {
private Log log = LogFactory.getLog(this.getClass()); public void beforeService(JoinPoint joinPoint){
System.out.println("aop方法被调用");
System.out.println("连接点对象:"+joinPoint.getTarget().getClass().getSimpleName());
System.out.println("连接点方法:"+joinPoint.getSignature());
System.out.println("连接点方法参数:"+joinPoint.getArgs()[0]); }
}

2.后置增强处理

特点:在目标方法正常执行(不出现异常)后织入增强处理。

<aop:after-returning method="afterReturning" pointcut-ref="servicePointcut"/>

如果需要获取返回值,添加returning="returnVal"

<aop:after-returning method="aft" pointcut-ref="ser" returning="returnVal"/>

public void afterService(Object returnVal){
System.out.println(returnVal);
}

3.异常增强处理

特点:在目标方法出现异常后织入增强处理。

<aop:after-throwing mehod=" " pointcut-ref=" " throwing ="ex"/>

如果需要获取异常对象才添加throwing="ex"

public void afterThrowing(Exception ex){
System.out.println(ex.getMessage());
}

4.最终增强处理

特点:不论方法是否抛异常,都会在目标方法最后织如增强处理,类似与finally。

<aop:after method="after" pointcut-ref="servicePointcut"/>

5.环绕增强处理

特点:在目标方法的前后都可以织入增强处理。

<aop:around method="" pointcut-ref="" />

public Boolean around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("目标方法的参数:"+pjp.getArgs()[0]);
Boolean b = null;
if (true) {
b = (Boolean) pjp.proceed(pjp.getArgs());
}
return b; }

四、使用AspectJ 简化AOP配置

Spring的AOP配置常有两种方式:

>通过schema形式实现简单AOP配置,也就是上面事例使用方法。

>使用annotation简化AOP配置。

AspectJ是一个面向切面的框架,AspectJ定义了AOP语法。

使用@AspectJ必须使用jdk5.0以上版本。

步骤:

1.编写业务类(与上面方法相同)。

2.编写切面类。

package com.jbit.ssh.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; @Aspect //1.通过注解标记一个切面
public class AspectTest { @Before("execution(* com.jbit.ssh.service..*.*(..))")//2.定义切点和增强类型(切点表达式)
public void ba(){ //3.增强处理的fun 4.配置xml
System.out.println("前置增强-------");
}
@After("execution(* com.jbit.ssh.service..*.*(..))")
public void aa(){
System.out.println("后置增强------");
}
/**
* @before表示前置增强
* @AfterReturning:表示后置增强处理
* @Around:表示环绕增强处理
* @AfterThrowing:表示异常增强处理
* @After:表示最终增强处理
*/ }

3.编写配置文件实现AOP

<?xml version="1.0" encoding="UTF-8"?>

<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > <!--4.基于@AspectJ切面的驱动器 -->
<aop:aspectj-autoproxy />
<bean class="com.jbit.ssh.aop.AspectTest"></bean>
</beans>

  

Spring核心概念之AOP的更多相关文章

  1. SSM 五:Spring核心概念

    第五章:Spring核心概念 一.Spring Ioc 优点: 1.低侵入式设计 2.独立于各种应用服务器 3.依赖注入特性将组建关系透明化,降低耦合度 4.面向切面编程的特性允许将通用性任务集中式处 ...

  2. Spring 核心概念

    Spring 核心概念 引言 本文主要介绍 Spring 源码中使用到的一些核心类 1. BeanDefinition BeanDefinition表示Bean定义,BeanDefinition 中存 ...

  3. Spring系列(一):Spring核心概念

    一.Spring概念 Spring是一种多层的J2EE应用程序框架,其核心就是管理资源组件以及依赖关系,Spring框架为现代基于java的企业应用程序提供了一个全面的编程和配置模型. 二.Sprin ...

  4. Spring核心概念和案例

    一.Spring概念 1.Spring框架概述 轻量级的Java EE开源框架,它是由Rod Johnson为了解决企业应用程序开发的复杂性而创建, Spring框架提供了一个开发平台,用于整合其他技 ...

  5. 第一章 spring核心概念

    一.Spring作用:管理项目中各种业务Bean(service类.Dao类.Action类),实例化类,属性赋值 二.Spring IOC(Inversion of Control )控制反转,也被 ...

  6. Spring核心 IoC和AOP原理

    1. 什么是Spring Spring是一个轻量的Java开源框架,它简化了应用开发,实现基于POJO的编程模型.它的两大核心是:IoC(控制反转),AOP(面向切面编程). 2. IoC控制反转 简 ...

  7. spring 核心思想:AOP 理解

    什么是AOP? AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 面向切面编程Aspect-Orlented-Programming,即AO ...

  8. 30个类手写Spring核心原理之AOP代码织入(5)

    本文节选自<Spring 5核心原理> 前面我们已经完成了Spring IoC.DI.MVC三大核心模块的功能,并保证了功能可用.接下来要完成Spring的另一个核心模块-AOP,这也是最 ...

  9. Spring 核心概念以及入门教程

    初始Spring 在学习Spring之前我们首先要了解一下企业级应用.企业级应用是指那些为商业组织,大型企业而创建并部署的解决方案及应用. 这些大型企业级应用的结构复杂,涉及的外部资源众多,事务密集, ...

随机推荐

  1. saiku 展示优化

    saiku版本:3.7.4 下面是修改步骤,如果觉得麻烦,可以直接下载源代码:https://github.com/lihehuo/saiku 1.关闭自动执行 修改文件:saiku-ui/js/sa ...

  2. WinStore之Application Data

    一.Application Data简介 Applicaion Data相当于桌面应用的注册表,存储一些用户配置信息,如运行时状态,用户喜好等,需要注意的时,当卸载应用时,这些数据会被删除,所以不要存 ...

  3. CSS基础(一):开篇

    背景 HTML是一种超文本标记语言,用来定义文档的结构和内容,例如标题.段落和列表等等,而文档内容如何渲染.如何展示,这就需要样式来修饰了.CSS正是可以与HTML很好地结合.如果将HTML比作水,那 ...

  4. 查询修改linux 打开文件句柄数量

    查询系统支持最大可打开文件句柄数量: #vi /proc/sys/fs/file-max 查询当前连接用户最大可打开文件句柄数量: #ulimit -a 修改当前连接用户最大可打开文件句柄数量: #u ...

  5. 浅析MongoDB数据库的海量数据存储应用

    [摘要]当今已进入大数据时代,特别是大规模互联网web2.0应用不断发展及云计算所需要的海量存储和海量计算发展,传统的关系型数据库已无法满足这方面的需求.随着NoSQL数据库的不断发展和成熟,可以较好 ...

  6. IOS的启动画面的适配问题

    iPhone4,iPhone4s 分辨率960*640 长宽比1.5iPhone5,iPhone5s 分辨率1136*640 长宽比1.775iPhone6 分辨率1334*750 长宽比1.778i ...

  7. Node快速安装

    1.安装nvm  nvm是一个快速安装和切换nodejs版本的管理器 直接从 github clone nvm 到本地, 这里假设大家都使用 ~/git 目录存放 git 项目: $ cd ~/git ...

  8. CSDN 2013年度博客之星评选——分享几张厦门杭州的美图

    亲爱的小伙伴们,作者在6号至20号,一直在休假中,出去也没带电脑,今天回家意外的发现自己有幸成为“CSDN 2013年度博客之星评选”的候选人,在此也谢谢各位小伙伴们的支持,谢谢CSDN的鼓励.我的投 ...

  9. css3 画圆记录

    <style> .radar-wrapper * { -moz-box-sizing: border-box; box-sizing: border-box; margin:; paddi ...

  10. Linux下grep显示前后几行信息

    Linux下grep显示前后几行信息 标准unix/linux下的grep通过下面參数控制上下文 grep -C 5 foo file 显示file文件里匹配foo字串那行以及上下5行grep -B ...