Spring使用篇(九)——经典Spring AOP与多切面开发

1、开发环境搭建

  在Spring_Demo项目中创建名为“ClassifyAOP”的模块module,并将如下jar包加入到lib文件夹中,并全部将其Add as Library。具体如下所示:
在这里插入图片描述

2、经典Spring AOP应用程序

  Sprig早期提供的AOP实现,在现在几乎已经被废弃了,不过具有一定的讨论价值,它需要通过XML的方式去配置。例如要完成printRole方法的切面前置通知的功能,这时可以把printRole称为AOP的连接点。首先在com.ccff.spring.classify.aop.aspect包下创建名为“ProxyFactoryBeanAspect”的前置通知类,并实现MethodBeforeAdvice接口的before方法,具体代码如下所示:

package com.ccff.spring.classify.aop.aspect;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class ProxyFactoryBeanAspect implements MethodBeforeAdvice {
   
    /**
     * 前置通知
     * @param method:被拦截的方法
     * @param objects:参数 数组[role]
     * @param o:被拦截的对象
     * @throws Throwable
     */
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
   
        System.out.println("前置通知!!!");
    }
}

  有了它还需要对Spring IoC容器描述对应的信息,这个时候需要一个XML文件去描述它,具体配置如下:

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

    <bean id="proxyFactoryBeanAspect" class="com.ccff.spring.classify.aop.aspect.ProxyFactoryBeanAspect" />

    <!--设定代理类-->
    <bean id="roleService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--这里代理的是接口-->
        <property name="proxyInterfaces">
            <value>com.ccff.spring.classify.aop.service.RoleService</value>
        </property>

        <!--是ProxyFactoryBean要代理的目标类-->
        <property name="target">
            <bean class="com.ccff.spring.classify.aop.service.RoleServiceImpl" />
        </property>

        <!--定义通知-->
        <property name="interceptorNames">
            <list>
                <!--引入定义好的spring bean-->
                <value>proxyFactoryBeanAspect</value>
            </list>
        </property>
    </bean>

</beans>

  然后在com.ccff.spring.classify.aop.test包下创建名为“Test”的测试方法,具体代码如下所示:

package com.ccff.spring.classify.aop.test;

import com.ccff.spring.classify.aop.pojo.Role;
import com.ccff.spring.classify.aop.service.RoleService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
   
    public static void main(String[] args) {
   
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        RoleService roleService = (RoleService) context.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值