Spring核心概念之AOP
一、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的更多相关文章
- SSM 五:Spring核心概念
第五章:Spring核心概念 一.Spring Ioc 优点: 1.低侵入式设计 2.独立于各种应用服务器 3.依赖注入特性将组建关系透明化,降低耦合度 4.面向切面编程的特性允许将通用性任务集中式处 ...
- Spring 核心概念
Spring 核心概念 引言 本文主要介绍 Spring 源码中使用到的一些核心类 1. BeanDefinition BeanDefinition表示Bean定义,BeanDefinition 中存 ...
- Spring系列(一):Spring核心概念
一.Spring概念 Spring是一种多层的J2EE应用程序框架,其核心就是管理资源组件以及依赖关系,Spring框架为现代基于java的企业应用程序提供了一个全面的编程和配置模型. 二.Sprin ...
- Spring核心概念和案例
一.Spring概念 1.Spring框架概述 轻量级的Java EE开源框架,它是由Rod Johnson为了解决企业应用程序开发的复杂性而创建, Spring框架提供了一个开发平台,用于整合其他技 ...
- 第一章 spring核心概念
一.Spring作用:管理项目中各种业务Bean(service类.Dao类.Action类),实例化类,属性赋值 二.Spring IOC(Inversion of Control )控制反转,也被 ...
- Spring核心 IoC和AOP原理
1. 什么是Spring Spring是一个轻量的Java开源框架,它简化了应用开发,实现基于POJO的编程模型.它的两大核心是:IoC(控制反转),AOP(面向切面编程). 2. IoC控制反转 简 ...
- spring 核心思想:AOP 理解
什么是AOP? AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 面向切面编程Aspect-Orlented-Programming,即AO ...
- 30个类手写Spring核心原理之AOP代码织入(5)
本文节选自<Spring 5核心原理> 前面我们已经完成了Spring IoC.DI.MVC三大核心模块的功能,并保证了功能可用.接下来要完成Spring的另一个核心模块-AOP,这也是最 ...
- Spring 核心概念以及入门教程
初始Spring 在学习Spring之前我们首先要了解一下企业级应用.企业级应用是指那些为商业组织,大型企业而创建并部署的解决方案及应用. 这些大型企业级应用的结构复杂,涉及的外部资源众多,事务密集, ...
随机推荐
- java ConcurrentModificationException探究
当集合结构被修改,会抛出Concurrent Modification Exception. fail-fast会在以下两种情况下抛出ConcurrentModificationException ( ...
- SD卡中FAT32文件格式快速入门(图文详细介绍)【转】
本文转自:http://blog.csdn.net/mjx91282041/article/details/8904705 说明: MBR :Master Boot Record ( 主引导记录) D ...
- 一些值得学习和借鉴的.Net 开源项目
1.DotNetFramework .NET Reference Source 发布了 beta 版,可以在线浏览 .NET Framework 4.5.1 的源代码,并且可以通过配置,在 Visua ...
- Oracle10g新特性——正则表达式 - 转
在进行查询时,有时候需要按照一定的特殊规则来查找某个字符串,比如,你可能需要查询第三位为5-8,最后四位为’8888’的所有电话.在9i之前,你可能需要写一个很复杂的条件:Select usernam ...
- android中的提示信息显示方法(toast应用)
android中的提示信息显示方法(toast应用) (2011-10-17 11:02:06) 转载▼ 标签: android toast 杂谈 分类: Android android中toast的 ...
- (转)2G到C-RAN网络架构的演进
这是我一个学霸师弟写的一篇知识普及的文章,涉及到的知识结构很全面,很丰富,对网络通信技术感兴趣的童鞋可以看看,内容我就不贴过来了,直接到他博客上去看吧. 2G到C-RAN网络结构演进
- GDC2014免费slide整理下载
GDC2014图形和程序相关的部分免费slide整理下载:)http://pan.baidu.com/s/1eQzMrxo gdc14_An Indie Approach to procedural ...
- 最近买了个kindle,为了方便阅读,写了个程序抓取网页内容发送到Kindle
主要觉得往kindle里加书籍太麻烦了,要下载下来,还要通过邮件发送,特别一些网页文字版的书籍没办法放到kindle里,所以想着还不如自己动手丰衣足食,写一个程序直接抓取网页内容,制作成书籍,然后自动 ...
- Android Matirx的简介
在Android中,对图片的处理需要使用到Matrix类,Matrix是一个3 x 3的矩阵,他对图片的处理分为四个基本类型: 1.Translate————平移X,Y轴变换,而不是移动图形 2.Sc ...
- SEO优化之Title 和 Meta 标签
对搜索引擎最友好(Search Engine Friendly)的网页是静态网页,但大部分内容丰富或互动型网站都不可避免采用到相关技术语言来实现内容管理和交互功能.SEO 思想指导下的技术支持,主要是 ...