AOP具体实现--AspectJ(2)
来源:互联网 发布:郑州经济数据造假 编辑:程序博客网 时间:2024/06/08 19:00
上篇文章我们对AOP的相关概念进行了解,接下来我们将采用Annotation的方式完成AOP的实现.当然也可以用XML的配置方式进行实现.
采用Annotation完成AOP示例
1、spring的依赖包配置
SPRING_HOME/dist/spring.jar
SPRING_HOME/lib/log4j/log4j-1.2.14.jar
SPRING_HOME/lib/jakarta-commons/commons-logging.jar
SPRING_HOME/lib/aspectj/*.jar
2、将横切性关注点模块化,建立SecurityHandler.java
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;//采用注解指定SecurityHandler为Aspect@Aspectpublic 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-------");}}
3、采用注解指定SecurityHandler为Aspect
4、采用注解定义Advice和Pointcut
5、启用AspectJ对Annotation的支持,并且将目标类和Aspect类配置到IoC容器中
<?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>
6、开发客户端
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");}}
小结:
上述实例的主要功能是在进行增加用户的时候进行安全检查,本实例通过System.out.println("-------checkSecurity-------");来代替具体的检查内容.如果不用AOP思想来实现的话,那些与商业逻辑无关的重复代码遍布在整个程序中。实际的工程项目中涉及到的类和函数,远远不止几个。AspectJ令代码更精简,结构更良好.
- AOP具体实现--AspectJ(2)
- AOP具体实现--AspectJ(1)
- 8.4.2: AspectJ 实现AOP
- AspectJ如何实现AOP
- AspectJ AOP实现
- Spring @AspectJ 实现AOP
- AspectJ框架实现AOP
- 基于AspectJ实现AOP
- AOP注解实现(AspectJ)
- AspectJ注解方式实现AOP
- AOP具体实现
- AOP实现方式2——通过AspectJ提供的注解实现AOP
- AOP实现方式2——通过AspectJ提供的注解实现AOP
- AspectJ AOP 简单示例2
- AOP实现机制AspectJ和SpringFramework
- Spring通过@AspectJ方式实现AOP
- 8.4.1: AspectJ 实现AOP的准备
- Spring3.1 使用AspectJ实现AOP
- Uva-10881-Piotr's Ants
- java异常总结
- Android开发——如何在第三方应用中调用系统未开放的主题和样式
- synchronized
- oracl sql 优化 2
- AOP具体实现--AspectJ(2)
- Asp.Net/C#/WCF - C#实现谷歌相似图片查询算法
- 如何查看MATLAB的版本
- 漫步云中网络
- 32位和64位系统区别及int字节数
- Java实现IP地址定位地址信息
- HTML-HTTP状态码详解
- oracle sql 3
- 如果你不引用,是我的失败