标签:style blog http java color 使用
AOP(Aspect Oriented Programming),是面向切面编程的技术。AOP基于IoC基础,是对OOP的有益补充。
AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分:核心业务逻辑(Core business concerns)及横向的通用逻辑,也就是所谓的切面Crosscutting enterprise concerns。例如,所有大中型应用都要涉及到的持久化管理(Persistent)、事务管理(Transaction Management)、权限管理(Privilege Management)、日志管理(Logging)和调试管理(Debugging)等。
使用AOP技术,可以让开发人员只专注核心业务,而通用逻辑则使用AOP技术进行横向切入,由专人去处理这些通用逻辑,会使得任务简单明了,提高开发和调试的效率。
使用AOP,我们要注意关注横切性的功能,即抽象出独立服务,进行模块化使我们以前习惯性的纵向思维的方法再改变,注意横向思考问题的方式,我们结合现在的系统可以把判断文本框一些了的验证、日志的记录、事务的开启、数据库的开启和关闭等等,都可以抽象出使用切面的形式把这些方法切入进去,我们只需要关心我们的业务逻辑,这样代码简单,间接,开发效率大大提高,更重要的是复用效率大大提高了。
要想了解AOP,首先得了解几个重要的基本概念:
本文采用的JDK代理方式
AspectJ注解方式配置AOP,实例讲解
SecurityHandler,这个通知类可以换成安全性检测、日志管理、事务开启关闭等等。
<span style="font-family:FangSong_GB2312;font-size:18px;">package com.bjpowernode.spring;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SecurityHandler {
/**
* 定义Pointcut,Pointcut的名称为addAddMethod(),此方法没有返回值和参数
* 该方法就是一个标识,不进行调用
*/
@Pointcut("execution(* add*(..))")
private void addAddMethod(){};
/**
* 定义Advice,表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上
*/
//@Before("addAddMethod()")
@After("addAddMethod()")
private void checkSecurity() {
System.out.println("-------checkSecurity-------");
}
}
</span>UserManager接口
<span style="font-family:FangSong_GB2312;font-size:18px;">package com.bjpowernode.spring;
public interface UserManager {
public void addUser(String username, String password);
public void delUser(int userId);
public String findUserById(int userId);
public void modifyUser(int userId, String username, String password);
}
</span>UserManagerImpl实现
<span style="font-family:FangSong_GB2312;font-size:18px;">package com.bjpowernode.spring;
public class UserManagerImpl implements UserManager {
public void addUser(String username, String password) {
//checkSecurity();
System.out.println("---------UserManagerImpl.add()--------");
}
public void delUser(int userId) {
//checkSecurity();
System.out.println("---------UserManagerImpl.delUser()--------");
}
public String findUserById(int userId) {
//checkSecurity();
System.out.println("---------UserManagerImpl.findUserById()--------");
return "张三";
}
public void modifyUser(int userId, String username, String password) {
//checkSecurity();
System.out.println("---------UserManagerImpl.modifyUser()--------");
}
// private void checkSecurity() {
// System.out.println("-------checkSecurity-------");
// }
}
</span>
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 启用AspectJ对Annotation的支持 -->
<aop:aspectj-autoproxy/>
<bean id="userManager" class="com.bjpowernode.spring.UserManagerImpl"/>
<bean id="securityHandler" class="com.bjpowernode.spring.SecurityHandler"/>
</beans>
</span>Client客户端
<span style="font-family:FangSong_GB2312;font-size:18px;">package com.bjpowernode.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager = (UserManager)factory.getBean("userManager");
userManager.addUser("张三", "123");
}
}
</span>
效果图
当然我们通过注解的方式可以实现灵活的配置
<span style="font-family:FangSong_GB2312;font-size:18px;">/**
* 定义Advice,表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上
*/
@Before("addAddMethod()")
//@After("addAddMethod()")
private void checkSecurity() {
System.out.println("-------checkSecurity-------");
} </span>
修改后的效果:
总结
Annotation
优点
保存在 class 文件中,降低维护成本。
无需工具支持,无需解析。
编译期即可验证正确性,查错变得容易。
缺点
若要对配置项进行修改,不得不修改 Java 文件,重新编译打包应用。
总的来说,AOP这样的好处是我们抽象出公共的方法之后,我们的代码,方法复用性大大提高,实现了可灵活的配置,提高了系统的可靠性。
接下来
SSH深度历险(十一) AOP原理及相关概念学习+xml配置实例(对比注解方式的优缺点)
SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP,布布扣,bubuko.com
SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP
标签:style blog http java color 使用
原文地址:http://blog.csdn.net/lishehe/article/details/37317363