spring_aop_proxyInterfaces

demo

description:接口方式的aop编程,这里是使用实现接口的类,对实现类进行aop编程

首先准备spring aop所需要的jar包:


Aspect.java

/**
 *切面
 */
public class Aspect {
    
    private Logger logger = Logger.getLogger(Aspect.class);
    
    /**
     * 前置通知
     */
    public void doBefore(JoinPoint jp){
        logger.debug("before "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");
    }
    
    /**
     * 后置通知
     */
    public void doAfter(JoinPoint jp){
        logger.debug("finally after "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");
    }
    
    /**
     * 环绕通知
     */
    public void doAround(ProceedingJoinPoint pjp) throws Throwable{
        logger.debug("around before "+pjp.getTarget().getClass().getName()+"."+pjp.getSignature().getName()+" execute");
        pjp.proceed();
        logger.debug("around after "+pjp.getTarget().getClass().getName()+"."+pjp.getSignature().getName()+" execute");
    }
    
    /**
     * 后置返回通知
     */
    public void doAfterRetuning(JoinPoint jp){
        logger.debug("after return "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");
    }
    
    /**
     * 抛出异常后通知
     */
    public void doThrowing(JoinPoint jp, Throwable e){
        logger.debug("after throwing "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");
        logger.debug(e.getMessage());
    }
    
}

Dao.java

public interface Dao<T> {
    
    void save(T t);
    
    void select(T t);
    
    void delete(T t);
}

DaoImpl.java

public class DaoImpl implements Dao<Mp3>{

    @Override
    public void save(Mp3 t) {
        if(t.getId().equals(1)){
            throw new RuntimeException("save() 运行时异常");
        }
        System.out.println("execute save()");
    }

    @Override
    public void select(Mp3 t) {
        System.out.println("execute select()");
    }

    @Override
    public void delete(Mp3 t) {
        System.out.println("execute delete()");
    }

}

Mp3.java

public class Mp3 implements Serializable{

    private static final long serialVersionUID = 1L;

    private Integer id;
    
    private String url;

    public Mp3() {
    }

    public Mp3(Integer id, String url) {
        this.id = id;
        this.url = url;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
    
    
}

applicationContext-aop.xml

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="     
          http://www.springframework.org/schema/beans     
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
          http://www.springframework.org/schema/context     
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/aop     
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    default-autowire="byName">
    
    <!-- 切面 -->
    <bean id="aspect" class="com.asarja.aop.Aspect"/>
    
    <!-- aop接口代理 -->
    <bean name="daoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    	<property name="proxyInterfaces" value="com.asarja.aop.Dao"/>
    	<property name="target">
    		<bean class="com.asarja.aop.DaoImpl" />
    	</property>
    </bean>
    
    <aop:config>
    	<aop:aspect ref="aspect">
	    	<aop:pointcut expression="execution(* com.asarja.aop.*.*(..))" id="daoservice"/>
    		<aop:before method="doBefore" pointcut-ref="daoservice"/>
    		<aop:around method="doAround" pointcut-ref="daoservice"/>
    		<aop:after-returning method="doAfterRetuning" pointcut-ref="daoservice"/>
    		<aop:after-throwing method="doThrowing" pointcut-ref="daoservice" throwing="e"/>
    		<aop:after method="doAfter" pointcut-ref="daoservice"/>
    	</aop:aspect>
    </aop:config>
</beans>

Test.java

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("com/asarja/aop/applicationContext-aop.xml");
        Dao<Mp3> dao = (Dao<Mp3>)context.getBean("daoProxy");
        dao.delete(new Mp3(2,"http://,,,,,"));
        dao.save(new Mp3(1,"http://,,,,,"));
        dao.select(new Mp3(1,"http://,,,,,"));

结果:

2013-04-09 16:01:52,566 [main] DEBUG com.asarja.aop.Aspect - before $Proxy0.delete execute
2013-04-09 16:01:52,566 [main] DEBUG com.asarja.aop.Aspect - around before $Proxy0.delete execute
2013-04-09 16:01:52,567 [main] DEBUG com.asarja.aop.Aspect - before com.asarja.aop.DaoImpl.delete execute
2013-04-09 16:01:52,567 [main] DEBUG com.asarja.aop.Aspect - around before com.asarja.aop.DaoImpl.delete execute
execute delete()
2013-04-09 16:01:52,567 [main] DEBUG com.asarja.aop.Aspect - around after com.asarja.aop.DaoImpl.delete execute
2013-04-09 16:01:52,567 [main] DEBUG com.asarja.aop.Aspect - after return com.asarja.aop.DaoImpl.delete execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - finally after com.asarja.aop.DaoImpl.delete execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - around after $Proxy0.delete execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - after return $Proxy0.delete execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - finally after $Proxy0.delete execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - before $Proxy0.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - around before $Proxy0.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - before com.asarja.aop.DaoImpl.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - around before com.asarja.aop.DaoImpl.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - after throwing com.asarja.aop.DaoImpl.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - save() 运行时异常
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - finally after com.asarja.aop.DaoImpl.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - after throwing $Proxy0.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - save() 运行时异常
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - finally after $Proxy0.save execute

从输出结果可以看出,在抛出异常之后,环绕后方法不执行




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
optimize_aop是HALCON中的一个函数,用于优化AOP(automatic operator parallelization)模型。它可以根据线程号优化AOP,并检查给定硬件的并行处理能力。optimize_aop会检查每个运算符,并通过在tuple元组、channel通道或domain level域级别上的自动并行化来加快操作速度。它会执行多次运算符,并根据输入参数的变化来评估并行处理的效率。对于正确的优化,需要确保在计算机上没有同时运行其他计算密集型应用程序,以避免影响硬件检查的时间测量。如果程序员不想使用AOP,而是自己实现并行化,那么需要使用多线程技术,将图像进行拆分处理,最后再合并。这需要更多的专业知识,可以参考HALCON的官方例程simulate_aop.hdev和官方说明书parallel_programming.pdf。\[2\]\[3\] #### 引用[.reference_title] - *1* [optimize_aop.hdev对sobel边缘检测算子 AOP的对不同大小图像并行加速效果 相关例程学习](https://blog.csdn.net/u013404374/article/details/48996877)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [关于实现Halcon算法加速的基础知识(CPU多核并行/GPU)](https://blog.csdn.net/libaineu2004/article/details/104202063)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值