接口IAccount:
package study.spring.aop.account;
import java.util.List;
public interface IAccount {
public List<String> getAllAccount();
public String getAccountById(String id);
}
实现类:
package study.spring.aop.account.impl;
import java.util.List;
import study.spring.aop.account.IAccount;
public class AccountImpl implements IAccount {
public List<String> getAllAccount() {
// TODO Auto-generated method stub
return null;
}
public String getAccountById(String id) {
// TODO Auto-generated method stub
return null;
}
}
Advice类:
package study.spring.aop.advice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyBeforeMethodAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("方法调用之前...");
System.out.println("下面是方法调用的信息:");
System.out.println("所执行的方法是:" + method);
System.out.println("调用方法的参数是:" + args);
System.out.println("目标对象是:" + target);
}
}
Spring 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans default-autowire="byName" default-lazy-init="true">
<bean id="account" class="study.spring.aop.account.impl.AccountImpl" />
<!-- 第一个拦截器-->
<bean id="myBeforeAdvice" class="study.spring.aop.advice.MyBeforeMethodAdvice"/>
<!-- 使用ProxyFactoryBean 产生代理对象-->
<bean id="accountProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置目标对象 -->
<property name="target">
<ref bean="account" />
</property>
<!-- 这里是代理的接口 -->
<property name="proxyInterfaces">
<list>
<value>study.spring.aop.account.IAccount</value>
</list>
</property>
<!-- 代理对象所使用的拦截器-->
<property name="interceptorNames">
<list>
<value>myBeforeAdvice</value>
</list>
</property>
</bean>
</beans>
测试代码:
package study.spring.aop;
import org.springframework.test.AbstractSingleSpringContextTests;
import study.spring.aop.account.IAccount;
public class SpringTest extends AbstractSingleSpringContextTests {
@Override
protected String[] getConfigLocations() {
return new String[] { "classpath:applicationContext.xml" };
}
public void testApp() {
IAccount account = (IAccount) this.getApplicationContext().getBean("account");
account.getAccountById("1");
}
}
运行测试代码,advice里的输出为什么没有打印,求指教,谢谢大家。
相关推荐
Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的重要组成部分,它提供了一种在不修改源代码的情况下,对程序进行功能增强的技术。这个"spring aop jar 包"包含了实现这一功能所需的类和接口,...
Spring AOP,全称为Aspect Oriented Programming,是面向切面编程的一种编程范式,它是对传统的面向对象编程(OOP)的一种补充。在OOP中,核心是对象,而在AOP中,核心则是切面。切面是关注点的模块化,即程序中的...
Spring AOP(面向切面编程)是Spring框架的重要组成部分,它提供了一种模块化和声明式的方式来处理系统中的交叉关注点问题,如日志、事务管理、安全性等。本示例将简要介绍如何在Spring应用中实现AOP,通过实际的...
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
Spring AOP(面向切面编程)是Spring框架的核心特性之一,它允许开发者在不修改源代码的情况下,通过插入切面来增强或改变程序的行为。在本教程中,我们将深入探讨Spring AOP的不同使用方法,包括定义切点、通知类型...
现在,我们回到主题——"springaop依赖的jar包"。在Spring 2.5.6版本中,使用Spring AOP通常需要以下核心jar包: - `spring-aop.jar`:这是Spring AOP的核心库,包含了AOP相关的类和接口。 - `spring-beans.jar`:...
在 Spring AOP 的自动代理创建过程中,`AbstractAutoProxyCreator` 类起着关键作用。它实现了 `BeanPostProcessor` 接口,会在 Bean 初始化后对其进行处理,生成代理对象。`AbstractAutoProxyCreator` 会查找 Spring...
Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的一个重要模块,它通过提供声明式的方式来实现面向切面编程,从而简化了应用程序的开发和维护。在Spring AOP中,我们无需深入到每个...
- 如果目标对象没有实现任何接口,Spring会使用CGLIB库创建一个目标对象的子类,并在子类中插入通知代码。 - CGLIB是通过字节码生成技术动态创建子类,因此无需目标对象实现接口。 ### 3. 自定义类加载器 在某些...
### Spring AOP面向方面编程原理:AOP概念详解 #### 一、引言 随着软件系统的日益复杂,传统的面向对象编程(OOP)逐渐暴露出难以应对某些横切关注点(cross-cutting concerns)的问题。为了解决这一挑战,面向方面编程...
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
在描述中提到的"spring aop 五个依赖jar"是实现Spring AOP功能必不可少的库文件,让我们逐一了解它们的作用: 1. **aspectj-1.7.4.jar**:这是AspectJ库的核心部分,提供了完整的面向切面编程支持。AspectJ是一个...
3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑...
在`springAop1`这个压缩包中,可能包含了一个简单的应用示例,展示了如何定义一个切面类,以及如何在该类中定义通知方法。例如,我们可能会看到一个名为`LoggingAspect`的类,其中包含了`@Before`注解的方法,用于在...
Spring AOP 和 Spring IOC 是 Spring 框架的两个核心组件,它们对于任何基于 Java 的企业级应用开发都至关重要。Spring AOP(面向切面编程)允许开发者在不修改源代码的情况下,通过“切面”来插入新的行为或增强已...
Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的重要组成部分,用于实现横切关注点的模块化。它允许开发者定义“切面”,将那些与业务逻辑无关,却为多个对象共有的行为(如日志、...
Spring AOP(面向切面编程)是Spring框架的重要组成部分,它提供了一种强大的方式来实现横切关注点,如日志、事务管理、安全性等,从而解耦应用程序的核心业务逻辑。在Spring AOP中,关注点被模块化为独立的“切面”...
Spring AOP,即Spring的面向切面编程模块,是Spring框架的重要组成部分,它允许开发者在不修改源代码的情况下,对程序进行横切关注点的处理,如日志、事务管理等。实现这一功能,主要依赖于三个核心的jar包:aop...
这里我们将深入探讨Spring AOP、相关jar文件以及它们在实际开发中的作用。 首先,我们来看一下提供的文件: 1. aopalliance.jar:这是一个小型库,定义了面向切面编程的基本接口。它被许多AOP框架使用,包括Spring...
如果被代理的类没有实现接口,Spring AOP会采用CGLIB来生成代理对象。CGLIB(Code Generation Library)是一个开源的代码生成库,它允许运行时在内存中动态生成类和对象。 在Spring AOP中,我们通常使用@Aspect注解...