[Spring+SpringMVC+Mybatis]框架学习笔记(五):SpringAOP_顾问

上一章:[Spring+SpringMVC+Mybatis]框架学习笔记(四):Spring实现AOP
下一章:[Spring+SpringMVC+Mybatis]框架学习笔记(六):Spring_AspectJ实现AOP

第5章 SpringAOP_顾问

对第4章Spring实现AOP的缺点的思考:

  • 一个代理只能代理一个bean,意味着在实际应用中要定义多个代理;(通过默认advisor自动代理生成器来解决)
  • 从容器中获取对象是通过代理的bean的id,而不是我们在容器中定义的目标对象的id;(通过默认advisor自动代理生成器来解决)
  • 通知只能切入到目标类的所有的方法,不能指定某些方法。(通过顾问对通知的封装实现)

5.1 顾问Advisor

它将通知进行了包装,根据通知的不同类型,在不同的时间点,将切面织入到指定的目标对象的某些连接点。

PointCutAdvisor 顾问的一种,它是一个接口,有两个实现类:

  • NameMatchMethodPointCutAdvisor 名称匹配方法切入点顾问
  • RegexpMethodPointCutAdvisor 正则表达式方法切入点顾问

复习正则表达式:
(*) 星号:匹配前面的子表达式任意次 例如:ao* 能匹配 a ao aoo aooooo
(+) 加号:匹配前面的子表达式一次或多次 例如:ao+ 能匹配 ao aoo aooooo
(.) 点 :匹配任意字符 除“\r\n”之外
.* 代表任意的一个字符串 .*add.* 代表包含add字符的任意字符串

实例:

利用实例4.5的目标类接口、目标类、通知,实现切入到目标类的指定的方法。

1)配置文件

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 注册服务类,并描述依赖关系 -->
    <bean id="studentService" class="com.steven.spring.sysmgr.service.impl.StudentService"></bean>
    
    <!-- 注册前置通知 -->
    <bean id="beforeAdvice" class="com.steven.spring.sysmgr.advice.MyBeforeAdvice"></bean>
    
    <!-- 注册后置通知 -->
    <bean id="afterAdvice" class="com.steven.spring.sysmgr.advice.MyAfterAdvice"></bean>
    
    <!-- 注册环绕通知 -->
    <bean id="aroundAdvice" class="com.steven.spring.sysmgr.advice.MyAroundAdvice"></bean>
    
    <!-- 注册异常通知 -->
    <bean id="throwingAdvice" class="com.steven.spring.sysmgr.advice.MyThrowingAdvice"></bean>
    
    <!-- 注册一个名称匹配方法切入点顾问 -->
    <!-- 顾问Advisor比通知Advice多了一个指定方法的步骤 -->
    <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"/>
        <!-- 限定单个目标方法 -->
        <property name="mappedName" value="addStudent"/>
        <!-- 限定多个目标方法 -->
        <!-- 方法1 -->
        <!-- <property name="mappedNames" value="addStudent,updateStudent"/> -->
        <!-- 方法2 -->
        <!-- <property name="mappedNames">
            <array>
                <value>addStudent</value>
                <value>updateStudent</value>
            </array>
        </property> -->
        <!-- 方法3 -->
        <!-- <property name="mappedNames" value="*Student"/> -->
        
    </bean>

    <!-- 注册前置顾问代理生成器 -->
    <bean id="myBeforeAdvisorProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="studentService"/>
        <property name="interceptorNames" value="beforeAdvisor"/>
    </bean>
    
    <!-- 注册一个正则表达式方法切入点顾问 -->
    <bean id="afterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="afterAdvice"/>
        <!--限定单种方法-->
        <property name="pattern" value=".*add.*"/>
        <!-- 限定多种 -->
        <property name="patterns" value=".*add.*,.*del.*"/>
    </bean>
    
    <!-- 注册后置顾问代理生成器 -->
    <bean id="myAfterAdvisorProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="studentService"/>
        <property name="interceptorNames" value="afterAdvisor"/>
    </bean>
    
</beans>

2)测试

package com.steven.spring.sysmgr.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.steven.spring.sysmgr.entity.Student;
import com.steven.spring.sysmgr.service.IStudentService;

/**
 * 测试顾问
 * @author chenyang
 *
 */
public class AdvisorTest {
    private ApplicationContext ac = null;
    
    @Before
    public void init(){
        ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
    
    //测试前置顾问,指定某些方法,使用名称匹配切入点方法顾问来实现
    @Test
    public void testBeforeAdvisor(){
        IStudentService studentService = (IStudentService) ac.getBean("myBeforeAdvisorProxy");
        studentService.addStudent(new Student());
        studentService.updateStudent(new Student());//修改功能的前置通知不能被打印
    }
    
    //测试后置顾问,指定某些方法,使用正则表达式切入点方法顾问来实现
    @Test
    public void testAfterAdvisor(){
        IStudentService studentService = (IStudentService)ac.getBean("myAfterAdvisorProxy");
        studentService.addStudent(new Student());
        studentService.delStudent(1);
        studentService.updateStudent(new Student());
    }
    
}

5.2 自动代理生成器

Spring提供了自动代理生成器来解决要定义多个代理生成器的问题,有两种方式:

  • 默认advisor自动代理生成器(目标对象为配置文件中配置的所有的目标对象bean,而且为配置文件里面所有的advisor自动生成代理); ---> 笼统
  • bean名称自动代理生成器(可以指定某些目标对象(bean),而且可以指定某些切面的实现(advise/advisor)) 。 ---> 精确

总结:

第4-5章中各种技术的运用,无非是为了一个目标:将我们编写的切面的实现(通知/顾问)织入到某些类的某些方法中。

实例:

利用实例4.5的目标类接口、目标类、通知,实现切入到目标类的指定的方法。

1)配置文件

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 注册服务类,并描述依赖关系 -->
    <bean id="studentService" class="com.steven.spring.sysmgr.service.impl.StudentService"></bean>
    
    <!-- 注册前置通知 -->
    <bean id="beforeAdvice" class="com.steven.spring.sysmgr.advice.MyBeforeAdvice"></bean>
    
    <!-- 注册后置通知 -->
    <bean id="afterAdvice" class="com.steven.spring.sysmgr.advice.MyAfterAdvice"></bean>
    
    <!-- 注册环绕通知 -->
    <bean id="aroundAdvice" class="com.steven.spring.sysmgr.advice.MyAroundAdvice"></bean>
    
    <!-- 注册异常通知 -->
    <bean id="throwingAdvice" class="com.steven.spring.sysmgr.advice.MyThrowingAdvice"></bean>
    
    <!-- 注册一个名称匹配方法切入点顾问 -->
    <!-- 顾问Advisor比通知Advice多了一个指定方法的步骤 -->
    <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"/>
        <property name="mappedName" value="addStudent"/>
    </bean>

    <!-- 注册一个正则表达式方法切入点顾问 -->
    <bean id="afterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="afterAdvice"/>
        <property name="pattern" value=".*add.*"/>
    </bean>
    
    <!-- 默认Advisor自动代理生成器,目标对象为配置文件中注册的所有的目标对象,advisor为配置文件中所有的advisor -->
    <!-- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean> -->
    
    <!-- bean名称自动代理生成器,不仅可以指定目标对象,还可以指定advise/advisor(注意:这里都可以!) -->
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames" value="studentService"/>
        <property name="interceptorNames" value="beforeAdvisor"/>
    </bean>
    
</beans>

2)测试默认Advisor自动代理生成器

package com.steven.spring.sysmgr.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.steven.spring.sysmgr.entity.Student;
import com.steven.spring.sysmgr.service.IStudentService;

public class DefaultAdvisorAutoProxyCreator {
    private ApplicationContext ac = null;
    
    @Before
    public void init(){
        ac = new ClassPathXmlApplicationContext("applicationContextForAutoProxy.xml");
    }
    
    //测试默认顾问自动代理生成器
    @Test
    public void testDefaultAdvisorAutoProxyCreator(){
        // 注意:这里是直接取容器中定义的目标对象的id,不再使用代理生成器的id
        IStudentService studentService = (IStudentService) ac.getBean("studentService");
        studentService.addStudent(new Student());
        studentService.updateStudent(new Student());//修改功能的前置通知不能被打印
    }
}

3)测试bean名称自动代理生成器

package com.steven.spring.sysmgr.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.steven.spring.sysmgr.entity.Student;
import com.steven.spring.sysmgr.service.IStudentService;

public class BeanNameAutoProxyCreator {
    private ApplicationContext ac = null;
    
    @Before
    public void init(){
        ac = new ClassPathXmlApplicationContext("applicationContextForAutoProxy.xml");
    }
    
    //测试bean名称自动代理生成器
    @Test
    public void testBeanNameAutoProxyCreator(){
        IStudentService studentService = (IStudentService) ac.getBean("studentService");
        studentService.addStudent(new Student());
        studentService.updateStudent(new Student());
    }
}

上一章:[Spring+SpringMVC+Mybatis]框架学习笔记(四):Spring实现AOP
下一章:[Spring+SpringMVC+Mybatis]框架学习笔记(六):Spring_AspectJ实现AOP

转载于:https://www.cnblogs.com/steven0325/p/11151609.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值