AOP的解释

AOP:

       AOPAspect Oriented Programing的简称,面向切面编程。AOP适合于那些具有横切逻辑的应用:如性能监测,访问控制,事务管理、缓存、对象池管理以及日志记录。AOP将这些分散在各个业务逻辑中的代码通过横向切割的方式抽取到一个独立的模块中。AOP 实现的关键就在于 AOP 框架自动创建的 AOP 代理,AOP 代理则可分为静态代理和动态代理两大类,其中静态代理是指使用 AOP 框架提供的命令进行编译,从而在编译阶段就可生成 AOP 代理类,因此也称为编译时增强;而动态代理则在运行时借助于 JDK 动态代理、CGLIB 等在内存中临时生成 AOP 动态代理类,因此也被称为运行时增强。

代理对象的方法= 增强处理+ 被代理对象的方法

       Spring AOP 则采用运行时生成 AOP 代理类,因此无需使用特定编译器进行处理。由于 Spring AOP 需要在每次运行时生成 AOP 代理,因此性能略差一些。

AOP使用场景

AOP用来封装横切关注点,具体可以在下面的场景中使用

       Authentication 权限

       Caching 缓存

       Context passing 内容传递

       Error handling 错误处理

       Lazy loading 懒加载

       Debugging 调试

       logging, tracing, profiling andmonitoring 记录跟踪优化校准

       Performance optimization 性能优化

       Persistence 持久化

       Resource pooling 资源池

       Synchronization 同步

       Transactions 事务

AOP相关概念

方面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象。事务管理是J2EE应用中一个很好的横切关注点例子。方面用Spring Advisor或拦截器实现。

连接点(Joinpoint):程序执行过程中明确的点,如方法的调用或特定的异常被抛出

通知(Advice):在特定的连接点,AOP框架执行的动作。各种类型的通知包括“around”“before”“throws”通知。通知类型将在下面讨论。许多AOP框架包括Spring都是以拦截器做通知模型,维护一个围绕连接点的拦截器链。Spring中定义了四个advice: BeforeAdvice, AfterAdvice, ThrowAdviceDynamicIntroductionAdvice

切入点(Pointcut): 指定一个通知将被引发的一系列连接点的集合。AOP框架必须允许开发者指定切入点:例如,使用正则表达式。 Spring定义了Pointcut接口,用来组合MethodMatcherClassFilter,可以通过名字很清楚的理解, MethodMatcher是用来检查目标类的方法是否可以被应用此通知,而ClassFilter是用来检查Pointcut是否应该应用到目标类上

引入(Introduction): 添加方法或字段到被通知的类。 Spring允许引入新的接口到任何被通知的对象。例如,你可以使用一个引入使任何对象实现 IsModified接口,来简化缓存。Spring中要使用Introduction, 可有通过DelegatingIntroductionInterceptor来实现通知,通过DefaultIntroductionAdvisor来配置Advice和代理类要实现的接口

目标对象(Target Object): 包含连接点的对象。也被称作被通知或被代理对象。POJO

AOP代理(AOP Proxy): AOP框架创建的对象,包含通知。Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。

织入(Weaving): 组装方面来创建一个被通知对象。这可以在编译时完成(例如使用AspectJ编译器),也可以在运行时完成。Spring和其他纯Java AOP框架一样,在运行时完成织入。

日志应用:

实现登陆和日志管理(使用Spring AOP)

1LoginService   LogService   TestMain

2)用Spring 管理  LoginService LogService 的对象

3)确定哪些连接点是切入点,在配置文件中

4)将LogService封装为通知

5)将通知植入到切入点

6)客户端调用目标

<aop:config>

<aop:pointcutexpression="execution(* cn.com.spring.service.impl.*.*(..))"id="myPointcut"/>

<!--将哪个-->

<aop:aspectid="dd" ref="logService">

<aop:beforemethod="log" pointcut-ref="myPointcut"/>

</aop:aspect>

</aop:config>

execution(* *cn.com.spring.service.impl.*.*(..))

1)* 所有的修饰符

2)* 所有的返回类型

3)* 所有的类名

4)* 所有的方法名

5)* ..所有的参数名

1.ILoginService.java

packagecn.com.spring.service;

public interfaceILoginService {

       public boolean login(String userName,String password);

}

2.LoginServiceImpl.java

packagecn.com.spring.service.impl;

importcn.com.spring.service.ILoginService;

public classLoginServiceImpl implements ILoginService {

       public boolean login(String userName,String password) {

              System.out.println("login:"+ userName + "," + password);

              return true;

              }

}

3.ILogService.java

packagecn.com.spring.service;

importorg.aspectj.lang.JoinPoint;

public interfaceILogService {

       //无参的日志方法

       public void log();

       //有参的日志方法

       public void logArg(JoinPoint point);

       //有参有返回值的方法

       public void logArgAndReturn(JoinPointpoint,Object returnObj);

}

4.LogServiceImpl.java

package cn.com.spring.service.impl;

import org.aspectj.lang.JoinPoint;

import cn.com.spring.service.ILogService;

public class LogServiceImpl implementsILogService {

       @Override

       publicvoid log() {

              System.out.println("*************Log*******************");

       }

       //有参无返回值的方法

       publicvoid logArg(JoinPoint point) {

       //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象

              Object[]args = point.getArgs();

              System.out.println("目标参数列表:");

              if(args != null) {

                     for(Object obj : args) {

                            System.out.println(obj + ",");

                     }

                     System.out.println();

              }

       }

       //有参并有返回值的方法

       publicvoid logArgAndReturn(JoinPoint point, Object returnObj) {

              //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象

              Object[]args = point.getArgs();

              System.out.println("目标参数列表:");

       if(args != null) {

              for(Object obj : args) {

                     System.out.println(obj + ",");

              }

              System.out.println();

              System.out.println("执行结果是:" + returnObj);

       }

       }

}

5.applicationContext.java

<?xml version="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

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">

<bean id="logService"class="cn.com.spring.service.impl.LogServiceImpl"></bean>

<bean id="loginService"class="cn.com.spring.service.impl.LoginServiceImpl"></bean>

<aop:config>

<!-- 切入点 -->

<aop:pointcut

expression="execution(*cn.com.spring.service.impl.LoginServiceImpl.*(..))"

id="myPointcut" />

<!-- 切面:将哪个对象中的哪个方法,织入到哪个切入点 -->

<aop:aspect id="dd"ref="logService">

<!-- 前置通知

<aop:before method="log"pointcut-ref="myPointcut" />

<aop:after method="logArg"pointcut-ref="myPointcut">

-->

<aop:after-returningmethod="logArgAndReturn" returning="returnObj"pointcut-ref="myPointcut"/>

</aop:aspect>

</aop:config>

</beans>

6.TestMain.java

public class TestMain {

       publicstatic void testSpringAOP(){

       ApplicationContextctx = new ClassPathXmlApplicationContext("app*.xml");

       ILoginServiceloginService = (ILoginService)ctx.getBean("loginService");

              loginService.login("zhangsan","12344");

       }

       publicstatic void main(String[] args) {

       testSpringAOP();

       }

       }

7.输出结果:

       login:zhangsan,12344

       目标参数列表:

       zhangsan,

       12344,

       执行结果是:true

       解析:1.先调用了login()方法System.out.println("login:"+ userName + "," +      password);

       2.再调用了logArgAndReturn()方法输出了日志,并且返回了login()方法是否成功

       System.out.println("目标参数列表:");

       if(args != null) {

              for (Object obj : args) {

                     System.out.println(obj + ",");

              }

              System.out.println();

              System.out.println("执行结果是:" + returnObj);

       }

权限控制

首先定义一个用户:

public class User { 

       privateString username; 

       publicString getUsername() { 

              return username; 

      

       publicvoid setUsername(String username) { 

              this.username = username; 

      

用户有三种人:未注册用户,注册用户,与管理员

注册用户可以可以发表,回复帖子

管理员除了可以发表,回复帖子,还可以删除帖子!

下面定义TestCommunity接口:

public interface TestCommunity { 

       publicvoid answerTopic(); 

       publicvoid deleteTopic(); 

实现上面接口的TestCommunityImpl:

 

public class TestCommunityImpl implementsTestCommunity { 

       //注册用户与管理员拥有的功能 

       publicvoid answerTopic() { 

              System.out.println("可以发表,回复帖子"); 

      

       //管理员拥有的功能 

       publicvoid deleteTopic() { 

              System.out.println("可以删除帖子!"); 

      

下一步,建立一下依赖注入的实现类TestResultImpl:

 

public class TestResultImpl { 

       privateTestCommunity test; 

       publicvoid setTest(TestCommunity test) { 

              this.test= test; 

       }    

       publicvoid answerTopic() { 

              test.answerTopic(); 

      

       publicvoid deleteTopic() { 

              test.deleteTopic(); 

      

接下来,就是最重要的一个类,拦截器,Around处理类型的,TestAuthorityInterceptor:

importorg.aopalliance.intercept.MethodInterceptor; 

importorg.aopalliance.intercept.MethodInvocation; 

//创建Around处理应该实现MethodInterceptor接口 

public class TestAuthorityInterceptorimplements MethodInterceptor { 

              privateUser user; 

              publicUser getUser() { 

                     returnuser; 

             

              public void setUser(User user) { 

                     this.user= user; 

             

              //invoke方法返回调用的结果 

              publicObject invoke(MethodInvocation invocation) throws Throwable { 

                     StringmethodName = invocation.getMethod().getName(); 

                     if(user.getUsername().equals("unRegistedUser")) { 

                            System.out.println("你的身份是未注册用户,没有权限回复,删除帖                                   !"); 

                            return null; 

                     } 

                     if((user.getUsername().equals("user")) 

                            &&(methodName.equals("deleteTopic"))) { 

                            System.out.println("你的身份是注册用户,没有权限删除帖子"); 

                            returnnull; 

                    

                     //proceed()方法对连接点的整个拦截器链起作用,拦截器链中的每个拦截                器都执行该方法,并返回它的返回值 

                     returninvocation.proceed(); 

             

      

配置文件:

 

<?xml version="1.0"encoding="UTF-8"?> 

<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans> 

<bean id="authTarget" class="org.test.lighter.TestCommunityImpl"/> 

<!-- 其中的username可以写为admin,user,unRegistedUser--> 

<bean id="user"class="org.test.lighter.User"> 

<property name="username"value="user" /> 

</bean> 

<!-- 配置拦截器 --> 

<beanid="TestAuthorityInterceptor" 

class="org.test.lighter.TestAuthorityInterceptor"> 

<property name="user"ref="user" /> 

</bean> 

<!-- 配置代理工厂bean --> 

<bean id="service" 

class="org.springframework.aop.framework.ProxyFactoryBean"> 

<propertyname="proxyInterfaces"> 

<value>org.test.lighter.TestCommunity</value> 

</property> 

<property name="target"ref="authTarget"/> 

<propertyname="interceptorNames"> 

<list> 

<value>TestAuthorityInterceptor</value> 

</list> 

</property> 

</bean> 

<bean id="testResult"class="org.test.lighter.TestResultImpl"> 

<property name="test"ref="service" /> 

</bean> 

</beans> 

再写一个执行文件BeanTest:

 

importorg.springframework.context.ApplicationContext; 

importorg.springframework.context.support.FileSystemXmlApplicationContext; 

public class BeanTest { 

public static void main(String[] args)throws Exception 

ApplicationContext ctx = newFileSystemXmlApplicationContext("src/bean.xml"); 

TestResultImpl test =(TestResultImpl)ctx.getBean("testResult"); 

test.answerTopic(); 

test.deleteTopic(); 

执行结果:大家猜一下啦

 

1、如果是管理员,打印出

可以发表,回复帖子 

可以删除帖子

2、如果是注册用户

可以发表,回复帖子 

你的身份是注册用户,没有权限删除帖子 

3、未注册用户

你的身份是未注册用户,没有权限回复,删除帖子

Spring为我们提供了,根据beanName匹配后进行自动代理的解决方法

mark fromhttp://blog.csdn.net/sd0902/article/details/8393770

http://lighter.iteye.com/blog/42673

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值