spring AOP之二

package com.kruders.spring.aop;
 
public class BusinessImpl implements BusinessService {
    public void doSomeThing() {
        System.out.println("Do Something Here");
        int x = 5/0;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="businessService" class="com.kruders.spring.aop.BusinessImpl" />
    <!-- Advice -->
    <bean id="businessAdvice" class="com.kruders.spring.advice.BusinessAdvice" />
    
    <bean id="businessServiceProxy" 
                 class="org.springframework.aop.framework.ProxyFactoryBean">
 
		<property name="target" ref="businessService" />
 
		<property name="interceptorNames">
			<list>
				<value>businessAdvice</value>
			</list>
		</property>
	</bean>
</beans>

package com.kruders.spring.advice;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;

public class BusinessAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice, MethodInterceptor {
	
	@Override
	public void before(Method method, Object[] args, Object target)
            throws Throwable {
		System.out.println("Before method is called (by " + this.getClass().getName() + ")");
	}
	
	@Override
	public void afterReturning(Object returnValue, Method method,
			Object[] args, Object target) {
		System.out.println("After method is called (by " + this.getClass().getName() + ")");
	}
	
	public void afterThrowing(ArithmeticException e) throws Throwable {
		System.out.println("After throwing method is called");
	}
	
	@Override
	public Object invoke(MethodInvocation method) throws Throwable {
		System.out.println("Around method is called");
	    System.out.println("Around before is running");
        method.proceed();
        System.out.println("Around after is running");
        return null;
	}
}

package com.kruders.spring.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kruders.spring.aop.BusinessService;

public class Main {
	public static void main(String args[]) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("Spring-Business.xml");
        BusinessService businessService = (BusinessService)appContext.getBean("businessServiceProxy");
        //这句话相当于spring框架通过配置文件的第10行new一个BusinessServcie类的实例出来
        try {
        	businessService.doSomeThing();
        } catch(ArithmeticException ae) {
        	
        }
    }
}

原文:http://kruders.com/spring/spring-aop-advice/

源代码:http://pan.baidu.com/share/link?shareid=425056&uk=3878681452

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
水利改革发展 中国政府高度重视水利建设,将水利作为国家基础设施建设的优先领域。政策文件强调了防洪抗旱、水资源管理、水环境保护和水生态修复等方面的全面要求,推动了水利信息化的发展。 智慧水利建设目标 智慧水利的建设目标是通过数据共享、应用惠民、应急预警等手段,打破信息孤岛,提升应急抢险协作能力,加强水利数据在惠民信息化方面的应用。同时,提出了共享联动化、解决信息安全问题、提高水利信息科技创新能力等目标。 智慧水利建设模式 智慧水利的建设模式包括构建统一平台、数据中心、信息整合平台、决策支持系统等,以实现水利、海洋、环保等政府部门和公众的信息共享和服务。此外,还包括了云计算虚拟化、网络传输、采集工程等多个方面的技术应用。 智慧水利应用实例 智慧水利的应用实例包括视频水文工程监控、多要素一体化检测设备、汛情预警智能联动、三防决策指挥、河长综合信息展示等。这些应用通过集成GIS、互联网地图服务、物联网设备等技术,实现了对水利设施的实时监控、数据分析和应急响应。 成功案例与展望 智慧水利的成功案例展示了通过视频监控、预警信息发布、移动办公信息APP等技术,有效提升了灾害应对能力、水资源管理和河长制的实施效果。这些案例表明,智慧水利的建设不仅提高了水利管理的效率和水平,也为未来的水利信息化发展提供了方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值