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之前我们首先要了解一下企业级应用.企业级应用是指那些为商业组织,大型企业而创建并部署的解决方案及应用. 这些大型企业级应用的结构复杂,涉及的外部资源众多,事务密集, ...
随机推荐
- Rxlifecycle(三):坑
坑1 Observable.just("hello world!") .compose(this.<String>bindUntilEvent(ActivityEven ...
- [原]OpenGL基础教程(四)VBO+纹理绘制四边形
工程下载地址:http://pan.baidu.com/s/1ntr7NHv 提取码:yf1h 一.本文牵扯知识点梳理: (1)VBO (2)纹理 (3)libpng(加载png) (4)shader ...
- 再也不必当心我的密码了,多个SAP 客户端自动输入密码
再也不必当心我的密码了,多个SAP 客户端自动输入密码问题: 通常对于OFFICE人员来说,一天有很多的密码,OA密码,多个ERP密码,邮箱密码,代理密码等等,还经常60天过期之类,实在是焦头烂额. ...
- java-cef系列视频第二集:搭建开发环境
上一集我们介绍了如何从官方代码编译java-cef. 本视频介绍了如何使用eclipse搭建java-cef二次开发环境. 下一集我们将给java-cef添加flash支持. 本作品采用知识共享署名- ...
- volley中网络请求
首先使用Volley类创建 RequestQueue queue = Volley.newRequestQueue(this); Making GET Requests final String u ...
- Oracle的model语句入门-转
Model语句是Oracle 10g的新功能之一. 本文通过一些简单的例子帮助理解Model语句的用法,复杂使用场景请参考其他文章. 环境:当然需要Oracle 10g以上,本人是在11g上测试的. ...
- 【转载】CSS 盒子模型
转处:http://www.cnblogs.com/sunyunh/archive/2012/09/01/2666841.html 说在Web世界里(特别是页面布局),Box Model无处不在.下面 ...
- eclipse中对项目进行分类管理
我们在用Eclipse开发的时候通常会建很多类型的项目,如公司项目.自己项目.Demo等等,并且一个项目又可能有一个主项目和多个引用包,如果包所有的项目都放到一个workspace下面,则会引起混来, ...
- ionic 通过下载apk升级App
上篇通过更新文件升级APP,如果遇到了比如更新插件之类的问题,上篇是无法解决的,所以为了解决这个问题,需要下载apk文件升级APP. 1.配置文件如下:{'appVersion':'1.0.0', ...
- Only top uni produces good ppt.
重要的事说三遍: 学技术,我只相信那几所top高校 学技术,我只相信那几所top高校 学技术,我只相信那几所top高校 <Good Habits you must own> Wake up ...