使用AspectJ提供的注解方式实现aop
来源:互联网 发布:重庆seo外包服务价格 编辑:程序博客网 时间:2024/06/07 16:05
spring实现aop的方式有一下几种
1、基于代理的AOP
2、纯简单java对象切面
3、@Aspect注解形式的
4、注入形式的Aspcet切面
<aop:aspectj-autoproxy />有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class="true"/>时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。
下面是用@aspect注解形式实现的,首先是导入一些的jar包
切面的代码
@Component@Aspectpublic class Advice { @Before("init()")//通知 public void log(){ System.out.println("before do..."); } @Pointcut("execution(* service.*.*(..))")//方法切入点,execution为执行的意思,*代表任意返回值,然后是包名,.*意思是包下面的所有子包。(..)代表各种方法.
public void init(){ }}
实现类
@Component("serviceImpl")public class ServiceImpl implements Service { @Override public void saySomething() { System.out.println("do.."); }}
spring的配置文件中添加
<context:annotation-config></context:annotation-config> <context:component-scan base-package="service,advice"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。当然,spring
在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被<aop:aspectj-autoproxy />隐藏起来了
测试代码
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ac = new ClassPathXmlApplicationContext("service/bean.xml"); Service ser = (Service) ac.getBean("serviceImpl"); ser.saySomething(); }}
结果就是在输出do..之前输出了before do...
实际应用中可以用来实现日志功能
你把
<aop:aspectj-autoproxy/>
也放在了application.xml中了。可能是springmvc的bug吧。你把
<aop:aspectj-autoproxy/>
放到DispatcherServlet所对应的**-servlet.xml配置文件中,就可以了。
阅读全文
0 0
- 使用AspectJ提供的注解方式实现aop
- AOP实现方式2——通过AspectJ提供的注解实现AOP
- AOP实现方式2——通过AspectJ提供的注解实现AOP
- AspectJ注解方式实现AOP
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- Spring AOP之AspectJ的注解方式使用
- Spring使用AspectJ进行AOP的开发:注解方式
- AspectJ实现AOP(注解方式)
- Spring 之AOP 使用@AspectJ注解方式
- 使用AspectJ基于注解的Aop
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- Spring使用注解的方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- MyBatis-Spring(一)
- Elasticsearch与Solr 选型
- js倒计时
- 二项堆 Binomial Heap
- Thymeleaf教程 (四) Thymeleaf标准表达式语法(上)
- 使用AspectJ提供的注解方式实现aop
- 【模板】树状数组求逆序对
- 知识点(总结)
- SeekBar(拖动条)-SeekBar基本用法
- 不求甚解-Shiro
- 解决Invalid character found in the request target. The valid characters are defined in RFC 7230 and RF
- Kotlin say Hello Android
- jQuery圆形百分比图
- flv 文件格式解析