Spring泛形注入,CGLIB动态代理

41 篇文章 0 订阅

1.两组泛型继承类,和一个实体类
第一组:

package com.test.daselin.generate.di;

public class BaseRespostory<T> {

}

}

package com.test.daselin.generate.di;

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository extends BaseRespostory<User> {

}

//第二组 父类作为泛型注入的链接类

package com.test.daselin.generate.di;

import org.springframework.beans.factory.annotation.Autowired;

public class BaseService<T> {
    @Autowired
    protected BaseRespostory<T> repository;

    public  void add(){
        System.out.println("add");
        System.out.println(repository);
    }

}
package com.test.daselin.generate.di;

import org.springframework.stereotype.Service;

@Service
public class UserService extends BaseService<User> {

}

实体类

package com.test.daselin.generate.di;

public class User {

}

测试结果

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.daselin.generate.di.UserService;


public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-generate.xml");

        UserService userService = (UserService) ctx.getBean("userService");

        userService.add();

    }

}

spring 的bean配置

<?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"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:util="http://www.springframework.org/schema/util"
    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-4.0.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-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/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

        <context:component-scan base-package="com.test.daselin.generate.di"></context:component-scan>

</beans>

结果:

add
com.test.daselin.generate.di.UserRepository@547e045

二、动态代理
1.接口

package com.test.dasenlin.generate.aop;

public interface ArithmeticCalculator {

    Integer getPlus(Integer i,Integer j);

    Integer getMinus(Integer i,Integer j);

    Integer getMultiply(Integer i,Integer j);

    Integer getExcept(Integer i,Integer j);

}

2.实现类

package com.test.dasenlin.generate.aop;

public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

    @Override
    public Integer getPlus(Integer i, Integer j) {
        return i+j;
    }

    @Override
    public Integer getMinus(Integer i, Integer j) {
        return i-j;
    }

    @Override
    public Integer getMultiply(Integer i, Integer j) {
        return i*j;
    }

    @Override
    public Integer getExcept(Integer i, Integer j) {
        if(j!=0){
            return i/j;
        }else{
            return -1;
        }
    }

}

动态代理

package com.test.dasenlin.generate.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;

public class ArithmeticCalculatorLoggingCglibProxy {

    private ArithmeticCalculator target;

    public ArithmeticCalculatorLoggingCglibProxy(ArithmeticCalculator target){
        this.target = target;
    }

    public ArithmeticCalculator getLoggingProxy(){
        ArithmeticCalculator proxy = null;
        //代理对象由哪一个类加载器负责加载
        ClassLoader loader = target.getClass().getClassLoader();
        //代理对象的类型,其中有哪些加载方法
        Class [] interfaces = new Class [] {ArithmeticCalculator.class};
        //当调用代理对象其中的方法时,该执行的代码
        InvocationHandler h = new InvocationHandler() {
            /**
             * proxy 正在返回的那个代理对象,一般情况下,在invoke方法中都不使用该对象
             * method 正在被调用的方法
             * args 调用方法时,传入的参数
             */
            @Override
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
                    System.out.println("The method" + method.getName()+"begin with" +Arrays.asList(args));
                    Object result = method.invoke(target, args);
                    System.out.println("The method" + method.getName()+"end with "+result);
                return result;
            }
        };
        proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);


        return proxy;
    }
}

测试

import com.test.dasenlin.generate.aop.ArithmeticCalculator;
import com.test.dasenlin.generate.aop.ArithmeticCalculatorImpl;
import com.test.dasenlin.generate.aop.ArithmeticCalculatorLoggingCglibProxy;


public class Main2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ArithmeticCalculator target = new ArithmeticCalculatorImpl();
        ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingCglibProxy(target).getLoggingProxy();

        int result = proxy.getPlus(1, 3);
        System.out.println("--->"+result);

        result = proxy.getMultiply(1, 5);
        System.out.println("--->"+result);
    }

}

结果

The methodgetPlusbegin with[1, 3]
The methodgetPlusend with 4
--->4
The methodgetMultiplybegin with[1, 5]
The methodgetMultiplyend with 5
--->5

aop相关术语
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值