AOP浅谈
2017-05-30 17:45
176 查看
直接实践,概念查看相关资料
1.切面=切点(应用的地址)+通知(做什么)
2.AOP个人理解就是在某件事件之前或之后,应该做些什么?
举个例子:比如我们在跑步之前,你需要做热身运动,运动之后需要做拉伸运动。
package com.jack.begin.chatpter1.aop; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Run { Logger logger = LoggerFactory.getLogger(Run.class); public void run(){ logger.info("开始跑步了"); } }
3.接着你需要做热身运动和拉伸运动
package com.jack.begin.chatpter1.aop; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class WarmUp { Logger logger = LoggerFactory.getLogger(WarmUp.class); public void before(){ logger.info("开始热身了"); } public void after(){ logger.info("跑步做拉伸运动"); } }
4.关键切面的配置
<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"> <bean id="warmUp" class="com.jack.begin.chatpter1.aop.WarmUp"></bean> <!-- runs不能和切点名称一致 --> <bean id="runs" class="com.jack.begin.chatpter1.aop.Run"></bean> <aop:config> <aop:aspect ref="warmUp">
<!-- 这里是配置切点,做某件事情(这里是跑步)--> <aop:pointcut expression="execution(* *.run(..)))" id="run"/>
<!--这里是通知,也就是跑步之前需要做热身运动--> <aop:before method="before" pointcut-ref="run"/>
<!--这里是通知,也就是跑步之后需要做拉伸运动--><aop:after method="after"pointcut-ref="run"/></aop:aspect></aop:config></beans>
5.测试
package com.jack.begin.chatpter1;import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jack.begin.chatpter1.aop.Index;
import com.jack.begin.chatpter1.aop.Run;
public class ChapterTest {
@Test
public void RunAop(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring/chapter1/aop.xml");
Run run = (Run) context.getBean("runs");
run.run();
}
}
6.效果
5.当然这里是理解aop思想,大量用于日志和事务当中,可以这个理解日志在调用这个方法时之前或之后需要记录操作情况
相关文章推荐
- 从Design Pattern浅谈AOP
- 浅谈spring——AOP思想(五)
- 浅谈Spring(三)AOP原理
- 浅谈spring中的AOP
- 浅谈spring——AOP思想(五)
- 浅谈Spring AOP
- 浅谈Spring(四)AOP实例
- 浅谈AOP
- 浅谈springAOP和动态代理
- 浅谈Spring中的IOC和AOP概念
- 浅谈Spring中IOC和AOP是什么?
- 浅谈spring aop的五种通知类型
- 浅谈AOP
- 浅谈SpringBoot集成Redis实现缓存处理(Spring AOP实现)
- 浅谈Spring的AOP实现-代理机制
- 浅谈AOP
- 浅谈Android面向切面编程(AOP)
- 浅谈AOP
- 浅谈AOP(一)
- Spring浅谈理解AOP(Aspect Oritented Program)面向切面编程