AOP的七种实现方式之三

他们都假装颓废,你别上当!

说明;

方式一 同样的是service和serviceimpl,但是其他的就有所不同了,之前的都是直接写功能类就好,现在要添加一个配置文件了,具体如下:

先去继承一个MethodInterceptor ,实现其方法
package com.qianfeng.aop03;

import org.aopalliance.intercept.Invocation;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * Created by Administrator on 2020/3/1.
 */
public class MyAscept implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        System.out.println("------before--------");

        Object o = methodInvocation.proceed();

        System.out.println("------after--------");
        return null;
    }
}

添加配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="us" class="com.qianfeng.aop03.UserServiceImpl"></bean>
    <bean id="my" class="com.qianfeng.aop03.MyAscept"></bean>

    <!--
        ProxyFactoryBean代理的factorybean对象,代理目标us
        包含的四个属性注入
            1、interfaces:接口对象们
                <list>
                    <value>com.qianfeng.aop03.IUserService</value>
                    <value>com.qianfeng.aop03.IUserService</value>
                    <value>com.qianfeng.aop03.IUserService</value>
                </list>
           2、 target:目标对象
           3、interceptorNames:拦截对象的名称,自定义的 MethodInterceptor 对象,注意区分包名,并且此处使用value
           4、optimize:boolean类型的值
                true:强制使用cglib的动态代理方式
                false:使用jdk自带的动态代理

                cglib:code generation library,代码生成库,性能更高
    -->

    <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean" >
        <property name="interfaces" value="com.qianfeng.aop03.IUserService"></property>
        <property name="target " ref="us"></property>
        <property name="interceptorNames " value="my"></property>
        <property name="optimize" value="true"> </property>
    </bean>
</beans>

测试类:

package com.qianfeng.aop03;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.beans.Beans;

/**
 * Created by Administrator on 2020/3/1.
 */
public class Test03 {
    @Test
    public void test() {

        FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext("F:\\testmaven\\src\\main\\java\\com\\qianfeng\\aop03\\beans.xml");
        //避免强转
        IUserService proxy = fileSystemXmlApplicationContext .getBean("proxy", IUserService.class);

        proxy.getAllUser();
        proxy.saveUser(new Object());
        proxy.updateUser(new Object());
        proxy.deleteUser(1);
    }
}

~完

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值