Spring AspectJ简化AOP配置

1.通过ProxyFactoryBean配置AOP存在的问题

   a)这种配置方式是一个过渡方式, 为了让大家理解AOP就是对动态代理的具体应用.
   b)这种配置方式需要一个切点一个切点的配置, 不灵活, 配置信息越写越多.
   c)使用AspectJ方式进行配置, 可以解决上述问题.

2.AspectJ方式配置AOP

   a)AspectJ是一个插件, 需要额外导包: aspectjweaver.jar
   b)特点是可以支持通配符配置, *作为通配符, 可以代表包, 类, 方法…
   c)只需要配置一次, 后续所有匹配的位置都将变成切点.
   d)需要遵循特定的规范.

3.AspectJ配置步骤

   a)导包: aspectjweaver.jar
   b)在spring配置文件中引入aop命名空间
   c)通过指定的标签进行aop配置

<?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">
    <!--管理service对象-->
    <bean id="userService" class="com.bjsxt.service.impl.UserServiceImpl" />
    <!--管理通知对象-->
    <bean id="txAdvice" class="com.bjsxt.advice.TxAdvice" />
    <!--配置aop-->
    <aop:config>
        <!--配置切点-->
        <!--
            id: 唯一标识切点
            expression: 切点表达式, 让spring通过这个表达式定位切点
                execution(), 固定语法
                * 任意类型的方法返回值
                包名.*.* 指定包下的任意类的任意方法
                (..) 任意数量,任意类型的参数列表
        -->
        <aop:pointcut id="pc" expression="execution(* com.bjsxt.service.*.*(..))"/>
        <!--配置通知-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
    </aop:config>
</beans>
发布了320 篇原创文章 · 获赞 152 · 访问量 64万+

猜你喜欢

转载自blog.csdn.net/hello_word2/article/details/104833165