今天记录一下学习aspectj的一点代码
在做aspectj的代码需要引入的jar
aspectj/
aspectjlib.jar
aspectrt.jar
aspectjtools.jar
aspectweaver.jar
cglib/
cglib-2.2.2.jar
asm.jar
spring-aspectj/
spring-aspects.jar
配置文件 aop.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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy />
<bean id="aspect" class="com.jimmy.aop.AspectHello" />
<bean id="hello" class="com.jimmy.aop.Hello" />
</beans>
代码
AspectHello.java
package com.jimmy.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AspectHello {
@After("execution(* com.jimmy.aop.Hello.say(..))&& args(words)")
public void aa(String words) {
System.out.println(words+" kkkk");
}
@Before("execution(* com.jimmy.aop.Hello.say(..)) && args(words, a)")
public void aa(String words, int a) {
System.out.println(a+" "+words+" kkkk");
}
}
Hello.java
package com.jimmy.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Hello {
public void say(String words){
System.out.println("hello "+words);
}
public void say(String words, int a){
System.out.println(a + " hello "+words);
}
public static void main(String[] args){
ApplicationContext context=
new ClassPathXmlApplicationContext(new String[]{"aop.xml"});
Hello hello=(Hello)context.getBean("hello");
hello.say("jimmy");
hello.say("jimmy", 1);
}
}
运行结果:
hello jimmy
jimmy kkkk
1 jimmy kkkk
1 hello jimmy
分享到:
相关推荐
Spring AOP的实现主要依赖于两种技术:动态代理和AspectJ。对于接口,Spring使用Java动态代理(JDK Proxy)创建代理对象;而对于类,Spring则使用CGLIB库生成子类代理。这两种方式都是在运行时生成代理对象,从而在...
- **@AspectJ注解**:使用AspectJ注解实现Spring AOP。 **3.5 基于<aop:config/>元素的AOP** - **配置策略**:利用`<aop:config/>`元素进行AOP配置。 **3.6 在AspectJ 6应用中启用@Configurable注解** - **@...
3.4 基于@AspectJ的Spring AOP 3.5 基于<aop:config/>元素的AOP 3.5.1 巧用<aop:include/>元素 3.6 在AspectJ 6应用中启用@Configurable注解 3.6.1 显式使用AnnotationBeanConfigurerAspect切面 3.6.2 阐述@...
AspectJ是一个流行的AOP框架,Spring 3.0支持与AspectJ的集成,利用AspectJ的强大功能来增强Spring应用程序的AOP能力。 **3.3 老式Spring AOP** 在Spring早期版本中,AOP主要通过代理模式实现。虽然这种方式仍然...
1. **AOP与SpringAOP基础**:深入浅出地介绍了面向切面编程的基本原理和SpringAOP的核心概念,为后续的实践打下理论基础。 2. **AspectJ初探与@AspectJ支持**:探索了AspectJ6的基础特性和Spring框架如何与其集成,...
- **基于@AspectJ的Spring AOP**:讲解了如何使用@AspectJ注解来定义切面,这种方式更加灵活且功能强大。 - **基于<aop:config/>元素的AOP**: - **巧用<aop:include/>元素**:介绍了如何使用`<aop:include/>`元素...
- **@AspectJ 支持**:Spring 3.0 开始支持使用@AspectJ 来定义切面,增强了AOP 的灵活性和功能。 **知识点13:基于<aop:config/>元素的AOP** - **<aop:include/> 元素**:用于引入其他配置文件中的切面定义,便于...
AspectJ是另一种流行的AOP框架,与Spring AOP相比,它提供了更强大的编译期和运行期的切面编程支持。Spring 3.1增加了对AspectJ的支持,允许开发者在Spring应用中使用AspectJ的切面。 #### 四、Spring的其他特性 *...