AOP注解实现(AspectJ)
来源:互联网 发布:印巴分治原因知乎 编辑:程序博客网 时间:2024/05/22 14:49
.注解实现
架包
aspectjrt.jar
asm-2.2.3.jar
asm-commons-2.2.3.jar
asm-util-2.2.3.jar
spring.jar
cglib-nodep-2.1_3.jar
log4j-1.2.11.jar
commons-logging-1.0.4.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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="shopwaiterProxy" class="aopaspectjauto.ShopWaiter" />
<bean class="aopaspectjauto.TestAspect" />
</beans>
1.接口NeedTest 注解接口
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface NeedTest {
boolean value() default false;
}
2.类ShopWaiter
public class ShopWaiter implements Waiter {
@NeedTest(value = false)
public void sayHello(String name) {
System.out.println("say hello to:" + name);
}
@NeedTest(value = true)
public void sayhi() {
System.out.println("say hi to Bill");
}
}
3.类TestAspect 注解界面
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class TestAspect {
@AfterReturning("@annotation(NeedTest)") //注解表达式
public void needTestFun() {
System.out.println("after needTestFun() executed");
}
@Before("@annotation(NeedTest)")
public void needTestFu() {
System.out.println("before needTestFun() executed");
}
}
4.接口Waiter
public interface Waiter {
void sayHello(String name);
void sayhi();
}
5.使用Main
import java.lang.reflect.Method;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/AOP.xml");
Waiter waiter = (ShopWaiter) ctx.getBean("shopwaiterProxy");
waiter.sayHello("========");
waiter.sayhi();
}
public static void testComment() {
Class clazz = ShopWaiter.class;
Method[] methods = clazz.getDeclaredMethods();
for (Method m : methods) {
NeedTest nt = m.getAnnotation(NeedTest.class);
if(nt != null){
if(nt.value()){
System.out.println(m.getName()+"()需要测试");
}else{
System.out.println(m.getName()+"()不需要测试");
}
}
}
}
}
- AOP注解实现(AspectJ)
- AspectJ实现AOP(注解方式)
- AspectJ注解方式实现AOP
- Spring Aop(二)——基于Aspectj注解的Aop简单实现
- (15)Spring AOP 注解 AspectJ
- 使用AspectJ提供的注解方式实现aop
- Spring AOP+AspectJ注解实例
- Spring AOP Aspectj 注解配置
- 基于aspectj的注解aop
- Spring详解(五)------AspectJ 实现AOP
- Spring详解(五)------AspectJ 实现AOP
- AspectJ对AOP的实现(xml)
- AspectJ实现AOP(xml配置方式)
- AspectJ如何实现AOP
- AspectJ AOP实现
- Spring @AspectJ 实现AOP
- AspectJ框架实现AOP
- 基于AspectJ实现AOP
- GridView动态生成模板列
- 字体之间的间距调整
- 虚指针,虚继承的sizeof
- 为多功能片上系统处理器编写 Linux 设备驱动程序
- 条款43:学习处理模板化基类内的名称
- AOP注解实现(AspectJ)
- 动态代理实现(AspectJ)
- GridView控件的批量删除操作
- Mendeley -paper管理软件
- 静态实现(AspectJ)
- 世上不可能有真正的完美,但应该有一个追求完美的心态
- java面试题选编
- 有向图的强连通分量算法
- C#水晶报表显示sqlserver2000储存的图片 C#水晶报表动态显示sqlserver储存的图片