AOP是OOP的延续,是软件开发中的一个热点。

AOP技术,是OOP补充。

OOP引入封装。继承和多态建立一种对象层次结构模拟公共行为集合,而对从左到右的关系则显得无能为力。对于AOP则恰恰适应这样的横切技术。

简单说。就与业务无关。却为了业务模块所共同调用的逻辑封装起来,便于降低系统反复代码,降低模块间耦合度。利用维护和可操作性

横切技术将软分为两部分:核心关注点和横切关注点:业务处理流程为核心关注,与之关系不大的是横切关注。

如:系统中各处都相似的日志,事务。权限成为横切关注点。AOP作用是将核心点与横切点分开。

实现的一些技术点有:

Aspect:横切关注点的模块,表示在哪里做和做什么(一个类,是advice和point的结合)

Advice:表示做什么

Point:是joinpoint的集合,表示在哪里做的集合

Jointpoint:程序运行的一个精确点,比如类的一个方法,是抽象的概念,不一定要定义一个joinpoint。

没有aspect:我们怎样设计系统。如模拟一个系统模块中某个方法

//BusinessLogic属于核心关注点,它会调用到Security,Logging。Persistence等横切关注点。
public classBusinessLogic
{
public void SomeOperation()
{
//验证安全性;Securtity关注点;
//运行前记录日志;Logging关注点。 DoSomething(); //保存逻辑运算后的数据;Persistence关注点。
//运行结束记录日志。Logging关注点;
}
}

差点儿每一个业务方法都是这样,反复性太大。AOP的目的,就是将注入logging之类的横切关注点从bussinessz中分类。

形成单独的Aspect

这就保证了横切关注点的复用。因为BusinessLogic类中不再包括横切关注点的逻辑代码。为达到调用横切关注点的目的,能够利用横切技术,截取BusinessLogic类中相关方法的消息,比如SomeOperation()方法,然后将这些“aspect”织入到该方法中

AOP代码实现对加入做权限验证和日志效果。此为annotation实现注解

1  定义eao接口

packagecom.bjpower.node.spring.dao;
public interfaceIUserDao { publicvoid addUser(String username, String password);
}

实现类

package com.bjpower.node.spring.dao;

public class UserDaoOracle implements IUserDao {

	@Override
public void addUser(String username, String password) {
// TODO Auto-generated method stub
System.out.println("----------userDalOral.adduser--------");
}

2  manager接口

package com.bjpower.node.spring.manager;

public interface UserManager {

	public void addUser (String username, String password);
}

manager实现类

package com.bjpower.node.spring.manager;

import com.bjpower.node.spring.dao.IUserDao;
import com.bjpower.node.spring.dao.UserDaoOracle; public class UserManagerImp implements UserManager { public void addUser(String username, String password) {
userDao.addUser(username, password);
} private IUserDao userDao; public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
} ///set方法的默认配置
public UserManagerImp() {
} }

3  建立横切对象

3.1引入依赖包spring.jar ,log4j.jar commons-logging.jar aspectj.jar

3.2建立aspect横切类

3.3注解定义pointcut和advice

package com.bjpower.node.spring;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class SecurityHandler { //advice要指定范围,pointcut为该范围指定用在哪里
@Pointcut("execution(* add*(..)) )")
private void addAddMethod(){}; /***
* before指定advice的方式
* @param joinPoint 获取截获的方法和參数
*/
@Before("addAddMethod()")
//joinpoint为横切获取客户信息
private void checkSecurity(JoinPoint joinPoint){
for (int i = 0; i<joinPoint.getArgs().length;i++) {
System.out.println(joinPoint.getArgs()[i]);
}
System.out.println(joinPoint.getSignature().getName());
System.out.println("----------checkSecurity----");
} }

"execution(* add*(..)) )为查询表达式

第一个*为返回值,

第二个參数为拦截的方法名称。能够有模糊匹配,也能够指定某个类或某个包。若不指定则拦截全部包

第三个參数(..)第一个点号为參数,第二个是匹配

3.4xml文件配置

<!--开启注解-->
<aop:aspectj-autoproxy/>
<beanid="userDalOracle"class="com.bjpower.node.spring.dao.UserDaoOracle" />
<beanid="userManager"class="com.bjpower.node.spring.manager.UserManagerImp">
<!-- 描写叙述set方法 -->
<propertyname="userDao" ref="userDalOracle"></property>
</bean>
<!--引入横切类-->
<beanid="securityHandler"class="com.bjpower.node.spring.SecurityHandler" />
<!--========================= ASPECT CONFIGURATION ======================== -->

4  client的调用

package com.bjpower.node.spring.client;

import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjpower.node.spring.dao.UserDaoMysql;
import com.bjpower.node.spring.manager.UserManager;
import com.bjpower.node.spring.manager.UserManagerImp; public class Client { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager=(UserManager) factory.getBean("userManager");
userManager.addUser("hanhan", "passowrd");} }

拦截到的效果为下面打印,达到了在方法运行前对其进行检查的效果。

hanhan

passowrd

方法名称为 = addUser

----------checkSecurity----

小结: 



整体上aop降低了我们同样代码量,节省了时间,同一时候使得我们仅仅须要关注核心业务,建立了松耦合,可复多功能性。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Spring【AOP】的更多相关文章

  1. 【核心核心】8.Spring【AOP】注解方式

    1.引入jar包 sprig框架基础包+JUntil整合包+日志包+AOP包 spring的传统AOP的开发的包 spring-aop-4.2.4.RELEASE.jar com.springsour ...

  2. 6.Spring【AOP】XML方式

    1.AOP术语 1. Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点 2. Pointcut(切入点):所谓切 ...

  3. 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)

    [AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...

  4. Spring【AOP模块】就是这么简单

    前言 到目前为止,已经简单学习了Spring的Core模块.....于是我们就开启了Spring的AOP模块了...在讲解AOP模块之前,首先我们来讲解一下cglib代理.以及怎么手动实现AOP编程 ...

  5. 【AOP】Spring AOP基础 + 实践 完整记录

    Spring AOP的基础概念 ============================================================= AOP(Aspect-Oriented Pr ...

  6. spring 【二】学习之spring EL

    spring EL-spring 表达式语言,支持在xml和注解的形式,类似于JSP的el表达式的形式. 其主要使用@Value注解的结构形式 其主要功能 [1].注入普通字符串 [2].注入操作系统 ...

  7. 【Spring】---【AOP】

    转发几篇文章 专治不会看源码的毛病--spring源码解析AOP篇 Spring3:AOP 理解AOP 什么是AOP? 转自: http://www.cnblogs.com/xiexj/p/73668 ...

  8. 【AOP】spring 的AOP编程报错:[Xlint:invalidAbsoluteTypeName]error

    AOP来发过程中,报错如下: warning no match for this type name: net.shopxx.wx.institution.controller [Xlint:inva ...

  9. spring【一】 学习

    Spring 源码学习 通过注解的形式注入IOC 简单的创建一个maven的项目的 下载指定的spring的核心jar包(https://mvnrepository.com/artifact/org. ...

随机推荐

  1. poj1182食物链(种类并查集)

    http://poj.org/problem?id=1182 r[x] = 0 表示x和父亲是同类r[x] = 1 表示x吃父亲r[x] = 2 表示x被父亲吃因为只存在三种动物,且三种动物构成了环形 ...

  2. Java中动态代理技术生成的类与原始类的区别 (转)

    用动态代理的时候,对它新生成的类长什么样子感到好奇.有幸通过一些资料消除了心里的疑惑. 平时工作使用的Spring框架里面有一个AOP(面向切面)的机制,只知道它是把类重新生成了一遍,在切面上加上了后 ...

  3. Google Map API V2密钥申请

    之前用的都是v1,用的是MapView,好吧,仅仅能认命了.废话不再多说,開始android 的Google Maps Android API v2吧 之前參考了http://www.cnblogs. ...

  4. 【原创】leetCodeOj ---Partition List 解题报告

    原题地址: https://oj.leetcode.com/problems/partition-list/ 题目内容: Given a linked list and a value x, part ...

  5. Linux 没有 my.cnf 解决方案文件完全我自己的整个教程很多口才

    我看过好多关于Linux下没有my.cnf的博客,都是什么rmp安装没有my.cnf文件啊,然后什么两个方法啊,我就无语了,大家要是知道就不会查资料了,你们敢不敢负责点?说具体点?有的说从 /usr/ ...

  6. lua-TestMore(转)

    http://fperrad.github.io/lua-TestMore/ http://www.softpedia.com/get/Programming/Debuggers-Decompiler ...

  7. newinstance()和new有什么区别?(转)

    在初始化一个类,生成一个实例的时候:newInstance() 和 new 有什么区别? 用newInstance与用new是区别的,区别在于创建对象的方式不一样,前者是使用类加载机制,那么为什么会有 ...

  8. The area面积计算

    Problem Description Ignatius bought a land last week, but he didn't know the area of the land becaus ...

  9. AsyncSocket长连接棒包装问题解决

    project正在使用长连接快来server沟通.因此,指定我们的协议前两个字节为数据长度来区分数据包 app这边数据有两种传输形式: 1.app主动请求所须要的数据: 2.app异步接收来自服务端的 ...

  10. 7 JavaScript Basics Many Developers Aren't Using (Properly)【转】

    JavaScript, at its base, is a simple language that we continue to evolve with intelligent, flexible ...