AOP实现方式2——通过AspectJ提供的注解实现AOP
来源:互联网 发布:js修改input的type 编辑:程序博客网 时间:2024/06/07 15:46
1.Runable接口
package com.animal;/** * Created by kenneth on 2017/4/6. */public interface Runable { void run();}
2.Runable接口的实现类Dog
package com.animal;/** * Created by kenneth on 2017/4/6. */public class Dog implements Runable { @Override public void run() { System.out.println("小狗在跑"); }}
3.Spring注解对bean的方法进行拦截器
package com.animal;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;/** * Created by kenneth on 2017/4/6. */@Aspectpublic class RunHelper { @Pointcut("execution(* com.animal.Dog.*(..))") public void runMethod() { } @Before("runMethod()") public void beforeRun() { System.out.println("跑前要热身"); } @AfterReturning("runMethod()") public void afterRun() { System.out.println("跑完要喝水"); }}
4.Spring配置文件
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--启用Aspect对Annotation的支持--> <aop:aspectj-autoproxy/> <bean id="dog" class="com.animal.Dog"/> <bean id="runHelper" class="com.animal.RunHelper"/></beans>
5.测试类
package com.animal;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by kenneth on 2017/4/6. */public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Runable dog = (Runable) context.getBean("dog"); dog.run(); }}
6.测试结果:
四月 06, 2017 1:33:21 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@533ddba: startup date [Thu Apr 06 13:33:21 CST 2017]; root of context hierarchy
四月 06, 2017 1:33:21 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [ApplicationContext.xml]
跑前要热身
小狗在跑
跑完要喝水
阅读全文
0 0
- AOP实现方式2——通过AspectJ提供的注解实现AOP
- AOP实现方式2——通过AspectJ提供的注解实现AOP
- 使用AspectJ提供的注解方式实现aop
- AspectJ注解方式实现AOP
- AspectJ实现AOP(注解方式)
- Spring通过@AspectJ方式实现AOP
- Spring Aop(二)——基于Aspectj注解的Aop简单实现
- AOP注解实现(AspectJ)
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- spring学习(八)—AOP通过注解方式实现
- Spring整理系列(14)——基于注解的AspectJ风格实现spring AOP
- aop:aspectj-autoproxy, SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- AOP的annotation实现方式是基于AspectJ的实现
- Spring AOP(三)——通过@AspectJ注解切面
- AOP具体实现--AspectJ(2)
- 8.4.2: AspectJ 实现AOP
- Spring面向切面编程——Spring实现AOP方式——通过注解实现
- Spring-通过注解实现的AOP
- spring AspectJ的Execution表达式
- 整数快速幂取余
- AOP实现方式1——经典的基于代理的AOP实现
- 第一章.对象导论
- Anaconda+Tensorflow环境安装与配置
- AOP实现方式2——通过AspectJ提供的注解实现AOP
- CentOS7(linux)中ssh服务配置
- 【机器学习】决策树
- AOP实现方式3——通过<aop:config>来配置(纯POJO切面)
- 谈谈单例模式(一)
- CentOS 6 升级python2.66 到2.78 yum 无法使用解决方案
- C++标准库中bitset类的用法
- springmvc-异常的处理
- 第二章.一切都是对象