zoukankan      html  css  js  c++  java
  • 接口动态代理IOC注入到spring容器中

    public interface StudentService {
    
        public void add(String studentName);
    }

    定义一个spring的FactoryBean,FactoryBean在通过spring实例化生成的不是自己本身,而是通过调用的getObject方法返回的对象,该FactoryBean为接口生成一个动态代理的实现。

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    import org.springframework.beans.factory.FactoryBean;
    
    public class StudentServiceFactoryBean implements FactoryBean<StudentService>{
        
        private String studentName;
    
        @Override
        public StudentService getObject() throws Exception {
            //生成数据库访问代理(相当于Mapper的代理)
            StudentService studentService = (StudentService)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), 
                            new Class<?>[]{StudentService.class}, new MapperInvocationHandler(studentName));
            return studentService;
        }
    
        @Override
        public Class<?> getObjectType() {
            return StudentService.class;
        }
        
        public String getStudentName() {
            return studentName;
        }
    
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
    
    
        public static class MapperInvocationHandler implements InvocationHandler{
            
            private String mapperName;
            
            public MapperInvocationHandler(String mapperName) {
                super();
                this.mapperName = mapperName;
            }
    
    
    
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String sql = method.getName();
                if (sql.equals("add")) {
                    System.out.println(mapperName+"execute add method"+args[0]);
                }
                return null;
            }
            
        }
    
    }

    把该FactoryBean注入到Spring的BeanDefinitionRegistry中:

    import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;

    public
    class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { try { BeanDefinition sbd = new RootBeanDefinition(StudentServiceFactoryBean.class); sbd.getPropertyValues().addPropertyValue("studentName", "zhangsan"); BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(sbd ,"StudentService"); BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry); }catch(Exception e) { } } }

    测试代码:

    @Component
    public class MyIncludeBean implements InitializingBean{
        
        @Autowired
        private StudentService studentService;
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("MyIncludeBean:init");
            studentService.add("student111");
        }
    
    }

    控制台成功打印:

    MyIncludeBean:init
    zhangsanexecute add methodstudent111

  • 相关阅读:
    php 面向对象编程实例 __construct 和 __destruct 区别
    php 数组 类对象 值传递 引用传递 区别
    WebHttpBinding.ReaderQuotas 无法设置或者无法点出来
    XP IE8 安装失败
    把XML保存为ANSI编码
    更新RDL文件中的数据集(DataSets)
    真实赛车3,FERRARI之魂不买FERRARI 599 GTO可以解锁顶点系列。
    [转]一大波“关于BUG的类型”的图片 + 一小波笑话
    sdk manager 代理,解决下载速度慢的问题
    错误 1 缺少编译器要求的成员“System.Runtime.CompilerServices.ExtensionAttrib
  • 原文地址:https://www.cnblogs.com/swave/p/14890169.html
Copyright © 2011-2022 走看看