`
istone
  • 浏览: 62199 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Spring的Hello World:理解AOP

阅读更多

Spring的通知类型:

  • Around  org.aopalliance.intercept.MethodInterceptor         拦截对目标对象的方法调用
  • Before   org.springframework.aop.BeforeAdvice                 在目标方法被调用之前调用
  • After      org.springframework.aop.AfterReturningAdvice    在目标方法被调用之后调用
  • Throws  org.springframework.aop.ThrowsAdvice               当目标方法抛出异常时调用

 

    切面Advice:

package advice;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;

import service.HelloService;
import service.HelloServiceImpl;


public class HelloAdvice implements MethodBeforeAdvice {

	public void before(Method method, Object[] args, Object target) throws Throwable {
		HelloService hello = (HelloServiceImpl)target;
		
		Logger log = Logger.getLogger(target.getClass());
		log.debug("Before Class:" + hello.getClass().getName() + "'s " + method.getName());
	}
}

 

Spring配置文档:
<?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="helloDao" class="dao.HelloDaoImpl">
		<property name="helloworld" value="Hello World!" />
	</bean>
	
	<bean id="helloTarget" class="service.HelloServiceImpl">
		<property name="helloDao" ref="helloDao" />
	</bean>

	<bean id="helloAdvice" class="advice.HelloAdvice" />
	
	<bean id="helloService" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<list>
				<value>service.HelloService</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>helloAdvice</value>
			</list>
		</property>
		<property name="target" ref="helloTarget"/>
	</bean>
</beans>

 

输出结果
2009-07-11 17:22:35,625 DEBUG [HelloAdvice.java:18] : Before Class:service.HelloServiceImpl's print
Hello World!
factory

 

注:将spring.jar、junit.jar、commons-logging.jar、log4j.jar复制进WEB-INF/lib

分享到:
评论

相关推荐

    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的AOP示例DEMO HELLOWORLD

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

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

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

    hello Spring

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

    Spring入门笔记.md

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

    spring-versioned:一个用于版本 spring bean 和方法的 poc

    这是使用 Spring AOP 在方法级别显示版本控制功能的简单概念证明。如何? 在任何 spring bean 中,您都可以在方法级别添加 @Versioned(feature, version) 注释。 并在任何 spring 属性中提供功能版本。 运行时将仅...

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

    因为这些涉及了尤其是spring底层的好多类以及控制翻转(IOC)和面向切面(AOP)编程的思想,不过在讲述过程中我们尽量以实例来解释spring中的这些思想,不做空的理论上的讲解,在实践中理解spring框架的精髓。 本文只要是...

    Spring:Spring的一些应用

    SpringAOP: AOP(面向层面编程)的应用 SpringIoCTest: IoC(控制反转)的应用 SpringJDBC: JDBC链接数据库应用 SpringMVCException: SpringMVC异常处理应用 SpringMVCHelloWorld: SpringMVC入门HelloWorld ...

    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在项目实战中的运用。

    跟我学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...

    spring学习笔记

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

    spring-boot示例项目

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

    跟我学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 切面实例化模型

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

Global site tag (gtag.js) - Google Analytics