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

Spring AOP 之 HelloWorld

    博客分类:
  • java
阅读更多


java代码
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("benx.xml");
		HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
		helloWorld.sayHelloWorld();
	}

}

interface HelloWorld {
	void sayHelloWorld();
}

interface HelloChina {
	void sayHelloChina();
}

class HelloWorldImpl implements HelloWorld {

	public void sayHelloWorld() {
		System.out.println("Hello World!");
		//制造异常
		String str = null;
		str.substring(1);

	}
}

/**
 * 日志拦截器
 * 
 * @author jin.xiong
 * 
 */
class LogAdvice {

	/**
	 * 执行方法前拦截器
	 * 
	 * @param joinPoint
	 */
	public void methodBefore(JoinPoint joinPoint) {
		System.out.println(joinPoint.getTarget().getClass().getName() + "."
				+ joinPoint.getSignature().getName() + " Start");
	}

	/**
	 * 方法执行后拦截器
	 * 
	 * @param joinPoint
	 */
	public void methodAfter(JoinPoint joinPoint) {
		System.out.println(joinPoint.getTarget().getClass().getName() + "."
				+ joinPoint.getSignature().getName() + " end");
	}

	/**
	 * 方法出现异常拦截器
	 * 
	 * @param joinPoint
	 */
	public void methodException(JoinPoint joinPoint) {
		System.out.println(joinPoint.getTarget().getClass().getName() + "."
				+ joinPoint.getSignature().getName() + " mett Error");
	}

	/**
	 * 方法环绕拦截器,如果使用了这个,可以忽视上面的方法
	 * 注意该方法参数为ProceedingJoinPoint ,这是可以执行的,只有round可以使用
	 * @param joinPoint
	 * @return
	 */
	public Object methodRound(ProceedingJoinPoint joinPoint) {

		methodBefore(joinPoint);
		Object ob = null;
		try {
			ob = joinPoint.proceed();
		} catch (Throwable error) {
			methodException(joinPoint);
		}
		methodAfter(joinPoint);
		return ob;
	}

}
 
benx.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:aop="http://www.springframework.org/schema/aop"
	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">
            
            
	<bean id="helloWorld" class="HelloWorldImpl"/>
	
	<bean id="logAdvice" class="LogAdvice"/>
	
	<aop:config>
		<aop:aspect ref="logAdvice">
			<aop:pointcut id="logPointcut" expression="execution(* *.*(..))" />
			<aop:before method="methodBefore" pointcut-ref="logPointcut" />
			<aop:after method="methodAfter" pointcut-ref="logPointcut" />
			<aop:after-throwing method="methodException" pointcut-ref="logPointcut" />
			
			<!-- aroun最好不要和他们同时使用 -->
			<aop:around method="methodRound" pointcut-ref="logPointcut" />
		</aop:aspect>
	</aop:config>
</beans>
 


打印结果,之所以会重复,使用为使用了round,可以把benx.xml中的methodRound去掉
HelloWorldImpl.sayHelloWorld Start
HelloWorldImpl.sayHelloWorld Start
Hello World!
HelloWorldImpl.sayHelloWorld end
HelloWorldImpl.sayHelloWorld mett Error
HelloWorldImpl.sayHelloWorld mett Error
HelloWorldImpl.sayHelloWorld end
 
分享到:
评论

相关推荐

    Spring的AOP示例DEMO HELLOWORLD

    根据学习笔记整理的HelloWorld,需要自行下载Spring3相关的包

    SpringMVC3.1.2 入门级HelloWorld源码

    [INFO] | \- org.springframework:spring-aop:jar:3.1.2.RELEASE:compile [INFO] +- org.springframework:spring-core:jar:3.1.2.RELEASE:compile [INFO] | +- org.springframework:spring-asm:jar:3.1.2.RELEASE:...

    Spring的Hello World:理解AOP

    NULL 博文链接:https://istone.iteye.com/blog/423895

    hello Spring

    下面是Spring的HelloWorld的程序的文件结构: C:. │ .classpath │ .project │ ├─build │ └─classes │ └─com │ ├─dineshonjava │ │ └─sdnext │ │ └─springConfig │ │ spring.xml │ │ │ ...

    跟我学spring3(1-7)

    【第六章】 AOP 之 6.2 AOP的HelloWorld ——跟我学spring3 【第六章】 AOP 之 6.3 基于Schema的AOP ——跟我学spring3 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3 【第六章】 AOP 之 6.5 AspectJ...

    跟我学spring3(1-7).pdf

    —— 5.1 概述 5.2 SpEL基础5.3 SpEL语法5.4在Bean定义中使用EL6.1 AOP基础6.2 AOP的HelloWorld6.3 基于Schema的AOP6.4 基于@AspectJ的AOP 6.5 AspectJ切入点语法详解6.6 通知参数6.7 通知顺序6.8 切面实例化模型

    Java Framework 关于IOC、AOP、Log的案例源码

    该源码是课程 Java Spring案例精讲 ---- Spring框架 的源码,包含Java Spring的最简单的Hello World、IOC、AOP及Log的源码 Spring整体框架中的核心功能,例如:IOC、AOP、Bean生命周期、上下文、作用域、资源处理等...

    SSH(Struts1.0+Spring+Hibernate)框架集成笔记

    SSH框架集成是较复杂和难理解的,只有在不断的练习和使用中才能慢慢的理解其中的原理,仅凭看视频是远远不够的,因为这些涉及了尤其是spring底层的好多类以及控制翻转(IOC)和面向切面(AOP)编程的思想,不过在讲述...

    Spring入门笔记.md

    ## Spring入门学习 首先认识下Spring的结构 ![架构图]...&lt;bean id="helloBean" class="mybatis.study.start.bean.HelloWorld"&gt; ``` list Map,provincecitymysqq

    跟开涛学Spring

    1.19 【第六章】 AOP 之 6.2 AOP的HelloWorld ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . .208 http://jinnianshilongnian.iteye.com 第 2 / 366 页 1.20 【第六章】 AOP 之 6.3 基于...

    JBoss ESB 学习笔记

    12——第十一个ESB代码Spring Hello World 106 13——第十二个ESB代码Spring AOP 113 14——第十三个ESB代码Transform CSV to XML 122 15——第十四个ESB代码Transform XML to POJO 128 16——第十五个ESB代码Web ...

    helloworld.zip

    资源概要: springboot项目中怎么使用aop全局拦截controller的url以及参数;拦截器的使用等示例demo;springbootAop的应用可以使开发者很...可以学到spring中关于aop切面的示例。以及熟练掌握aop在项目实战中的运用。

    spring-boot示例项目

    该项目包含helloworld(快速入门)、web(ssh项目快速搭建)、aop(切面编程)、data-redis(redis缓存)、quartz(集群任务实现)、shiro(权限管理)、oauth2(四种认证模式)、shign(接口参数防篡改重放)、encoder(用户...

    spring学习笔记

    spring从HelloWorld到ioc,aop,对JDBC,hibernate,struts1,struts2的支持笔记

    SpringFramework中的AOP编程之入门篇

    使用跟踪和记录方面(面向方面领域的HelloWorld)作为例子,本文展示了如何使用Spring框架所独有的特性来声明切入点和通知以便应用方面。本系列的第二部分将更深入地介绍如何运用Spring中的所有通知类型和切入点来...

    Spring-Reference_zh_CN(Spring中文参考手册)

    使用BeanPostProcessor的Hello World示例 3.7.1.2. RequiredAnnotationBeanPostProcessor示例 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.2.1. PropertyPlaceholderConfigurer示例 3.7.2.2. ...

    SpringFramework中的面向方面编程(AOP),第二部分

    火龙果软件工程技术中心 在本系列的第一部分,我介绍了如何实现面向方面领域的“HelloWorld”:跟踪和记录方面。利用Spring框架所提供的面向方面编程(Aspect-OrientedProgramming,AOP)功能,您看到了如何使用...

Global site tag (gtag.js) - Google Analytics