面向切面编程,要理解很多概念,如切面,连接点,通知,切入点。。。
此时新建的项目不仅要添加spring的核心包(即core),还有添加AOP功能的jar包
然后创建bean类
先创建接口
package com.sun.springaop.test;
public interface IBean {
public void theMethod();
}
然后创建实现类
package com.sun.springaop.test;
public class BeanImpl implements IBean {
@Override
public void theMethod() {
System.out.println(this.getClass().getName()+"."+new Exception().getStackTrace()[0].getMethodName()+"()"+" 正在被执行");
}
}
创建前置通知类,即在操作前执行的功能
package com.sun.springaop.test;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class TestBeforeAdvice implements MethodBeforeAdvice {
// 参数method表示具体连接点,args表示目标对象方法的参数,target返回该方法所属的目标对象
@Override
public void before(Method m, Object[] args, Object target)
throws Throwable {
System.out.println("执行前置通知");
}
}
在配置文件中,进行拦截器和bean的配置
完整的配置文件如下:
<?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-2.0.xsd">
<!-- 定义代理 -->
<bean id="bean"
class="org.springframework.aop.framework.ProxyFactoryBean"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<property name="proxyInterfaces">
<value>com.sun.springaop.test.IBean</value>
</property>
<property name="target">
<ref local="beanTarget" />
</property>
<property name="interceptorNames">
<list>
<value>theAdvisor</value>
</list>
</property>
</bean>
<!-- 注入对象 -->
<bean id="beanTarget" class="com.sun.springaop.test.BeanImpl"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
</bean>
<!-- 使用正则表达式方式定义切入点 -->
<bean id="theAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<property name="advice">
<ref local="theBeforeAdvice" />
</property>
<property name="pattern">
<value>com\.sun\.springaop\.test\.IBean\.theMethod</value>
</property>
</bean>
<!-- 定义前置通知 -->
<bean id="theBeforeAdvice"
class="com.sun.springaop.test.TestBeforeAdvice" abstract="false"
lazy-init="default" autowire="default" dependency-check="default">
</bean></beans>
测试类:
package com.sun.springaop.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Client {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
IBean x = (IBean) factory.getBean("bean");
x.theMethod();
}
}
执行结果:
执行前置通知
com.sun.springaop.test.BeanImpl.theMethod() 正在被执行
分享到:
相关推荐
在这篇由三部分组成的 Spring 系列 的第 1 部分中,我将介绍 Spring 框架。我先从框架底层模型的角度描述该框架的功能,然后将讨论两个最有趣的模块:Spring 面向方面编程(AOP)和控制反转 (IOC) 容器。接着将...
因为上传大小的限制,分为两部分上传,这是第二部分,第一部分会在评论中给出链接 绝对是spring攻略中文版第二版 Spring专家力作 理论与实践完美结合 问题描述→解决方案→实现方法 第一部分 核心概念 第1章 ...
因为上传大小的限制,分为两部分上传,这是第一部分,第二部分会在评论中给出链接 绝对是spring攻略中文版第二版 Spring专家力作 理论与实践完美结合 问题描述→解决方案→实现方法 第一部分 核心概念 第1章 ...
spring 3.0 jar 最新所有开发包及开发项目实例 spring 3.0已经全面支持OSGi了。 各发行包的大致描述如下: org.springframework.asm-3.0.0.M4.jar: 提供对ASM(一个字节码框架)的简单封装 org.springframework....
【第一章】 Spring概述 ——跟我学Spring3 【第二章】 IoC 之 2.1 IoC基础 ——跟我学Spring3 【第二章】 IoC 之 2.2 IoC 容器基本原理 ——跟我学Spring3 【第二章】 IoC 之 2.3 IoC的配置使用——跟我学Spring3 ...
第一部分 核心概念 第1章 控制反转和容器 1.1 使用容器管理组件 1.1.1 问题描述 1.1.2 解决方案 1.1.3 实现方法 1.2 使用服务定位器降低查找组件的复杂性 1.2.1 问题描述 ...
(八) annotatin方式的AOP实例 26 二、 AOP配置xml方式 27 三、 AOP实现动态代理注意 28 第九课:DataSource 28 一、 Sping配置数据源: 28 二、 注入使用 29 三、 dbcp.BasicDataSource 29 第十课 Spring整合...
25.2.2.第一个对象定义 25.2.3.属性注入 25.2.4.构造器参数注入 25.2.5.总结 25.2.6.日志 25.3.应用程序上下文和IMessageSource接口 25.3.1.简介 25.4.应用程序上下文和IEventRegistry接口 25.4.1.简介 25.5.对象池...
第1章:对Spring框架进行宏观性的概述,力图使读者建立起对Spring整体性的认识。 第2章:通过一个简单的例子展现开发Spring Web应用的整体过程,通过这个实例,读者可以快速跨入Spring Web应用的世界。 第3章:...
2. 可以自主完成阅读Spring框架中Bean实例创建流程的源码 3. 可以自主完成阅读Spring框架中依赖注入流程的源码 4. 可以确定aop流程的源码阅读入口 5. 搞清楚aop相关的核心概念(通知、切面、切入点等) 6. 搞清楚...
第一部分,重点阐述Spring的架构。这部分内容循序渐进带领开发者进入 Spring中。主要在于阐述Spring IOC和Spring AOP。第二部分,重点阐述Spring的使用。这部分内容从简化Java/J2EE的角度出发,从J2EE平台各个技术...
(八) annotatin方式的AOP实例 26 二、 AOP配置xml方式 27 三、 AOP实现动态代理注意 28 第九课:DataSource 28 一、 Sping配置数据源: 28 二、 注入使用 29 三、 dbcp.BasicDataSource 29 第十课 Spring整合...
本资源是一篇关于学习和应用SSM框架(Spring、SpringMVC、MyBatis)的学习记录系列文章中的第六天内容,主题为初识Spring框架。 在这一部分中,你将了解到Spring框架的基本概念和使用方法,并通过实例探讨了Spring ...
课程目标 1. 搞清楚BeanFactory家族的接口和类的作用 2. 搞清楚ApplicationContext家族的接口和类的作用 3. 搞清楚BeanDefinitionRegistry和SingletonBeanRegistry的作用 4. 搞清楚注册BeanDefinition流程中各个类的...
第一部分,重点阐述Spring的架构。这部分内容循序渐进带领开发者进入Spring中。主要在于阐述Spring IOC和Spring AOP。第二部分,重点阐述Spring的使用。这部分内容从简化Java/J2EE的角度出发,从J2EE平台各个技术...
1.27 【第六章】 AOP 之 6.8 切面实例化模型 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . .282 1.28 【第六章】 AOP 之 6.9 代理机制 ——跟我学spring3 . . . . . . . . . . . . . ...
9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 9.5.7. 插入事务操作 9.5.8. 结合AspectJ...
9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. 结合AspectJ使用 @Transactional 9.6. 编程...
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...