`
零度弥合
  • 浏览: 19344 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

【spring学习笔记一】AOP简介

 
阅读更多

首先罗列一些AOP的概念

Aspect(切面):横切性关注点的抽象即为切面,它与类相似只是两者的关注点不一样,类是对物体特征的抽象,而切面是横切性关注的抽象。

joinpoint(连接点):所谓连接点是指那些被连接到的点,在spring中这些点指的是方法,因为spring只支持方法类型的连接点,实际上joinpoint还可以是field或类的构造器

pointcut(切入点):所谓切入点是指我们要对那些joinpoint进行拦截的定义

advice(通知):拦截到joinpoint之后要做的事情就是通知,通知分为前置通知,后置通知,异常通知,最终通知,环绕通知。

target(目标对象):代理的目标对象

weave(织入):将Aspect应用到target对象并导致proxy对象创建的过程称为织入

introduction(引入):在不修改代码的前提下,introduction可以在运行期为类动态的添加一些方法或field

 

 

基于代理的AOP实现(基于java反射机制)

1:创建实现类bean

<bean id="studentDaoImpl" class="dao.impl.StudentDaoImpl"/>

 

2:创建代理类

实现MethodBeforeAdvice AfterRunningAdivce这两个接口

 

3:定义切入点

<bean id="studentDaoPointCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">

<property name="pattern" value=".*Student"/>

</bean>

pattern指定正则表达式,匹配以Student结尾的方法 

 

4:定义通知

<bean id="studentDaoAdvice" class="org.springframework.aop.support.DefaultPointcutAdvisor">

<property name="advice" ref="studentDaoProxy"/>

<property name="pointcut" ref="studentDaoPointCut"/>

</bean>

这里面属性的名字是固定的advice:代理类,pointcut:切入点

 

 5:定义代理工厂 

<bean id="studentProxyFacory" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="studentDaoImpl"/>

<property name="interceptorNames" ref="studentDaoAdvice"/>

<property name="proxyInterfaces" value="com.mihe.dao.StudentDao"/>

</bean>

三个property的name也是固定的,分别是目标对象,拦截器,和实现了哪些接口

 

 自动代理的AOP实现(基于cglib)

只需要声明通知就可以,注意两个property都是固定的,第一个指向代理类,第二个使用正则表达式匹配目标对象

<bean id="studentDaoAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

<property name="advice" ref="studentDaoProxy" />

<property name="pattern" value=".*Student" />

</bean>

需声明支持自动代理

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

 

 AspectJ的AOP实现(基于注解)

1:增加xml支持

http://www.srpingframework.org/schema/aop

http://www.srpingframework.org/schema/aop/spring-aop-2.5.xsd

2:自动扫面注解和声明使用AspectJ自动代理

<context:annotation-config/>

<context:component-scan base-package="com.mihe"/>

<aop:aspectj-autoproxy/>

3:编写切面类

@Aspect//声明为切面

@Component//声明为组件

 在方法上加

@Before(value = "execution(public void com.mihe.dao.impl.StudentDaoImpl.*Student())")

表示StudentDaoImpl类中的所有以Student结尾的方法

 

分享到:
评论

相关推荐

    spring aop 学习笔记

    NULL 博文链接:https://microjava.iteye.com/blog/525796

    spring ioc aop mvc boot-学习笔记.docx

    自己学习spring课程的笔记。笔记都是根据尚硅谷的课程(spring ioc,spring aop,spring mvc,spring boot等)写的。 主要内容:spring ioc,spring aop,spring mvc,spring boot

    Spring Aop 学习笔记

    Spring Aop 学习笔记

    Spring AOP学习笔记

    NULL 博文链接:https://linres.iteye.com/blog/281221

    SpringAop学习笔记以及实现Demo

    SpringAOP学习笔记以及四个可运行的Demo,涵盖经典代理模式、基于注解、基于xml配置这3方面的Demo

    javaSpring学习笔记

    在“Java Spring学习笔记”中,你将找到对Spring框架的全面介绍,包括IoC(控制反转)和DI(依赖注入)原理、AOP(面向切面编程)、Spring MVC、Spring Boot等核心内容。每个主题都结合了理论知识和实际示例,帮助你...

    Spring学习笔记(16)----使用Spring配置文件实现AOP

    NULL 博文链接:https://coolszy.iteye.com/blog/541997

    Spring技术内幕学习笔记.docx

    《Spring技术内幕》学习笔记1——IoC容器体系结构 《Spring技术内幕》学习笔记2——IoC定位Bean定义资源 《Spring技术内幕》学习笔记3——IoC容器载入Bean定义资源文件 ...《Spring技术内幕》学习笔记7——AOP基础

    Spring学习笔记

    Spring学习笔记Spring spring的配置 IOC 依赖注入 基于Xml的注入 基于注释的注入 Spring的自动注入和属性自动注入 AOP 静态代理 动态代理 使用spring实现AOP 基于Annotation实现AOP 基于XML实现AOP ...

    spring学习笔记

    spring从HelloWorld到ioc,aop,对JDBC,hibernate,struts1,struts2的支持笔记

    Spring的学习笔记

    一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...

    Spring学习笔记总结

    Spring最新资料总结,包含SpringMVC,IOC,AOP面向切面编程,以及与Struts2,Hibernate的集成.

    spring的学习笔记

    spring很好的学习笔记,我想对于想学好或者面试好ioc和aop的同学有帮助的

    Spring-IOC-AOP.md

    Spring学习笔记,主要包括核心技术IOC和AOP

    spring学习笔记(有代码有注解解释)

    内容概要:学习Spring的一些学习笔记,主要学习Spring 框架两大核心机制(IoC、AOP) 笔记大纲:阅读笔记可以学习了解一下内容 如何使用 IoC ;配置文件;IoC 底层原理;通过运行时类获取 bean;通过有参构造创建 ...

    Spring入门学习笔记|Spring学习.pdf

    Spring入门学习笔记,内容包括Spring介绍,Spring配置文件,Spring配置数据源,Spring的注解开发,Spring集成Junit,Spring的AOP,jdbcTemplate介绍,Spring控制事务流程,Spring集成web。

    Spring学习笔记(15)----使用Spring的注解方式实现AOP

    NULL 博文链接:https://coolszy.iteye.com/blog/540465

    学习Spring笔记_AOP_Annotation实现和XML实现

    NULL 博文链接:https://ysj5125094.iteye.com/blog/2055563

Global site tag (gtag.js) - Google Analytics