那些年、一起追过的Spring--(5)----AOP
来源:互联网 发布:解析域名 编辑:程序博客网 时间:2024/05/21 10:36
了解: (在概念上和原理上充分理解)
参考视频:http://www.jikexueyuan.com/course/665_3.html?ss=1
1. 什么是AOP
2. AOP的存在价值
3. AOP的原理剖析
4. AOP的关键概念
5. AOP的通俗理解
– AOP存在的价值
AOP专门用于处理系统中分布于各个模块中的交叉关注点的问,在JavaEE应用中,常常通过AOP来处理一些具有横切性质的系统级服务,如事务管理,安全检查,缓存,对象池管理等,AOP已经成为了一种非常常用的解决方案;
–AOP的原理剖析
AOP代理其实是有AOP框架动态生成的一个对象,该对象可作为目标对象使用,
– AOP的关键概念
切面:–Aspect
连接点:–Join Point
通知:–Advice(before,after)
切入点:– Point Cut
引入: – Introduction
目标对象: – Target Object
AOP代理: – AOP Proxy
织入: – Weaving
–AOP通俗理解
一个组件A,不关心其他常用的服务组件B,但是这个组件A使用组件B的时候,不是组件A自身去调用,而是通过配置等其他方式,比如Spring中可以通过xml配置文件。这样就使的A压根就不需要知道服务组件B是怎样的,爱存在不存在,爱怎么存在都与A无关。A只关心自己的业务逻辑,具体A使用B的时候,配置文件去做,与具体的A组件无关。
核心概念:
切面,Aspect:其实就是我们需要定义的AOP类,它对功能类似的代码进行封装。
具体实现–XML方式:
导包:
实现代码:
package com.spring.aop;import java.util.Date;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class LogAop { @Before("execution(public void com.spring.dao.impl.StudentDaoImpl.*(..))") public void logBefore(){ Date date=new Date(); System.out.println("在方法执行之前执行日志------"); }}
package com.spring.dao;public interface StudentDao { public void insert(); public void update();}
package com.spring.dao.impl;import com.spring.dao.StudentDao;public class StudentDaoImpl implements StudentDao { @Override public void insert() { System.out.println("method insert"); }@Overridepublic void update() { System.out.println("update method!");}}
package com.spring.services;import com.spring.dao.StudentDao;public class StudentServices { private StudentDao dao; public void setDao(StudentDao dao) { this.dao = dao; } public void insert() { dao.insert(); } public void update(){ dao.update(); }}
package com.spring.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.services.StudentServices;public class test01 { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("bean-aop-annotation.xml"); StudentServices ss=(StudentServices) context.getBean("studentServices"); ss.insert(); ss.update(); }}
XML配置:
<?xml version="1.0" encoding="utf-8" ?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"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></aop:aspectj-autoproxy> <bean id="studentServices" class="com.spring.services.StudentServices"> <property name="dao" ref="studentDao"></property></bean> <bean id="studentDao" class="com.spring.dao.impl.StudentDaoImpl"></bean> <bean id="logAop" class="com.spring.aop.LogAop"></bean></beans>
运行结果:
在方法执行之前执行日志------method insert在方法执行之前执行日志------update method!
阅读全文
0 0
- 那些年、一起追过的Spring--(5)----AOP
- 那些年、一起追过的Spring--(4)----注解
- 那些年、一起追过的Spring--(1)----初识Spring
- 那些年、一起追过的Spring--(7)----如何让Servlet被Spring框架管理
- 那些年、一起追过的Spring--(2)----IOC、两种依赖注入方式
- 那些年、一起追过的Spring--(6)----Spring JDBC框架操作数据库(JdbcTemplate的使用)
- 《那些年,我们一起追过的女孩》观后感
- 那些年,我们一起追过的女孩~~
- 那些年,我们一起追过的VC++ 6.0/MFC
- 那些年,我们一起追过的球队
- VAVA:那些年,我们一起追过的ZERO
- 那些年,我们一起追过的Code......
- 那些年,我们一起追过的Code......
- 那些年,我们一起追过的C++
- 那些年,我们一起追过的Linux
- 那些年,我们一起追过的seo培训!
- 那些年我们一起追过的c++
- 那些年,我们一起追过的女孩-会声会…
- 图片加载神器之Fresco,基于各种使用场景的讲解
- Struts2 Part6
- Xcode中的 workspace, project, target, scheme
- xgboost使用入门
- SSM框架搭建
- 那些年、一起追过的Spring--(5)----AOP
- maven 解决架包冲突
- 欢迎使用CSDN-markdown编辑器
- CentOS中MySQL安装
- 【学习摘记】马士兵Servlet&JSP_课时4_配置第一个servlet文件
- HDU 4635:Strongly connected(强连通入门)
- 多线程编程学习一(Java多线程的基础)
- VB.NET版机房收费系统——三层登录
- Android Error-AndroidStudio解决Minimum supported Gradle version is 3.3. Current version is 2.14.1问题