`
weinan
  • 浏览: 38205 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring中AOP实现的4中方式

阅读更多
引用
配置可以通过xml文件来进行,大概有四种方式:

1.  配置ProxyFactoryBean,显式地设置advisors, advice, target等

2.  配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从容器中获得的其实已经是代理对象

3.  通过<aop: aspectj-autoproxy>来配置,使用AspectJ的注解来标识通知及切入点

4.  通过<aop:config>来配置

一、基于代理的AOP
1、创建通知
package com.demo.aop.bean;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
/**
 * 通知
 *
 */
public class ActionAdvice implements MethodBeforeAdvice,AfterReturningAdvice{

    public void afterReturning(Object returnValue, Method method,
            Object[] args, Object target) throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("执行方法之后调用"+System.currentTimeMillis());
    }

    public void before(Method method, Object[] args, Object target)
            throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("执行方法之前调用"+System.currentTimeMillis());
        
    }

}

2、Service层

package com.demo.aop.service;

public interface IPersonService {
    public void doUpdate();
}


3、Service实现层
package com.demo.aop.service.impl;

import org.springframework.stereotype.Component;

import com.demo.aop.service.IPersonService;

public class PersonService implements IPersonService {

    public void doUpdate() {
        // TODO Auto-generated method stub
        System.out.println("执行更新方法"+System.currentTimeMillis());
    }

}

4、文件配置
第一种:不实用自动代理
<?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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
    <!-- 基于代理的AOP -->
    <!-- 创建通知 -->
    <bean id="actionAdvice" class="com.demo.aop.bean.ActionAdvice" />
    <!-- 使用正则表达式切点 -->
    <bean id="actionPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
        <property name="pattern" value=".*doUpdate" />
    </bean>
    <!-- 通知 与切点结合-->
    <bean id="actionAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="advice" ref="actionAdvice" />
        <property name="pointcut" ref="actionPointcut" />
    </bean>
    <!-- 代理对象 -->
    <bean id="actionProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="personService" />
        <property name="interceptorNames" value="actionAdvisor" />
        <property name="proxyInterfaces" value="com.demo.aop.service.IPersonService" />
    </bean><!--此种代理,创建:IPersonService actionProxy声明,使用代理进行调用 -->
    <bean id="personService" class="com.demo.aop.service.impl.PersonService"/>
    
</beans>

第二种:使用自动代理
<?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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
    <!-- 基于代理的AOP -->
    <!-- 创建通知 -->
    <bean id="actionAdvice" class="com.demo.aop.bean.ActionAdvice" />
    <!-- 使用正则表达式切点 -->
    <bean id="actionPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
        <property name="pattern" value=".*doUpdate" />
    </bean>
    <!-- 通知 与切点结合-->
    <bean id="actionAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="advice" ref="actionAdvice" />
        <property name="pointcut" ref="actionPointcut" />
    </bean>
    <!-- 代理对象 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> <!--自动代理,创建对象正常使用,会在调用指定方法时进行执行-->
    <bean id="personService" class="com.demo.aop.service.impl.PersonService"/>
    
</beans> 

5、测试
第一种:不使用自动代理情况下,注意创建对象时要将代理注入
package com.demo.aop.AopTest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.demo.aop.service.IPersonService;

/**
 * 测试基于代理的AOP
 * @author wwl
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/context-aop.xml" })
public class AopTest {
    /**
     * 用接口声明,注入代理对象
     */
    @Autowired
    private IPersonService actionProxy;
    @Test
    public void testAop(){
        actionProxy.doUpdate();
    }
}

第二种:使用自动代理
package com.demo.aop.AopTest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.demo.aop.service.IPersonService;

/**
 * 测试基于代理的AOP
 * @author wwl
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/context-aop.xml" })
public class AopTest {
       /**
     * 自动创建代理
     */
    @Autowired
    private IPersonService personService;
    @Test
    public void testAop(){
        personService.doUpdate();
    }
}   

二、使用AspectJ提供的注解
1、创建通知
package com.demo.aop.bean;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
 * 通知
 *
 */
@Component
@Aspect
public class Action1Advice{
    @Pointcut("execution(* *.doUpdate())")
    public void actionPoint(){}
    
    @AfterReturning("actionPoint()")
    public void afterAction(){
        System.out.println("执行方法之后调用"+System.currentTimeMillis());
    }
    @Before("actionPoint()")
    public void beforeAction(){
        System.out.println("执行方法之前调用"+System.currentTimeMillis());
        
    }

}

2、Service层

package com.demo.aop.service;

public interface IPersonService {
    public void doUpdate();
}


3、Service实现层
package com.demo.aop.service.impl;

import org.springframework.stereotype.Component;

import com.demo.aop.service.IPersonService;
@Component
public class PersonService implements IPersonService {

    public void doUpdate() {
        // TODO Auto-generated method stub
        System.out.println("执行更新方法"+System.currentTimeMillis());
    }

}

4、文件配置
<?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:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"   
    xsi:schemaLocation="
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  ">
    <!-- @AspectJ注解 -->
    <!-- 激活组件扫描功能-->
    <context:component-scan base-package="com.demo"/>
    <!-- 激活自动代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

5、测试
package com.demo.aop.AopTest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.demo.aop.service.IPersonService;

/**
 * 测试使用AspectJ提供的注解切面
 * @author wwl
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/context-aop1.xml" })
public class Aop1Test {
    @Autowired
    private IPersonService personService;
    @Test
    public void testAop(){
        personService.doUpdate();
    }
}

三、纯POJO切面(不使用注解,只在配置中注入)
1、创建通知
package com.demo.aop.bean;
import org.springframework.stereotype.Component;
/**
 * 通知
 *
 */
@Component
public class Action1Advice{

    public void afterAction(){
        System.out.println("执行方法之后调用"+System.currentTimeMillis());
    }
    public void beforeAction(){
        System.out.println("执行方法之前调用"+System.currentTimeMillis());
        
    }

}

2、Service层
...........

3、Service实现层
...........

4、文件配置
<?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:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  ">
    <!-- @AspectJ注解 -->
    <!-- 激活组件扫描功能 -->
    <context:component-scan base-package="com.demo" />

    <aop:config>
        <aop:aspect ref="action1Advice">
            <aop:before method="beforeAction" pointcut="execution(* *.doUpdate(..))" />
            <aop:after method="afterAction" pointcut="execution(* *.doUpdate(..))" />
        </aop:aspect>
    </aop:config>
 <!--或者-->

<!--

  <aop:config>
    <aop:pointcut id="myPointcut" expression="execution(* *.doUpdate(..))"/>
    <aop:aspect id="action1Advice" ref="action1Advice" >
        <aop:before method="beforeAction" pointcut-ref="myPointcut"/>
        <aop:after method="afterAction" pointcut-ref="myPointcut"/>
    </aop:aspect>
  </aop:config>

-->



</beans>

5、测试
引用


...

总结:总体看其实两种方式,一种是使用代理,一种是使用AOP(是否使用注解)。

流程:先创建通知,创建切点,再将通知与切点绑定使用代理进行处理调用


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics