Spring安全依赖查找

39次阅读

共计 5079 个字符,预计需要花费 13 分钟才能阅读完成。

依赖查找安全性比照

依赖查找类型 代表实现 是否平安
繁多类型 BeanFactory#getBean
ObjectFactory#getObject
ObjectProvider#getIfAvailable
汇合类型 ListableBeanFactory#getBeansOfType
ObjectProvider#stream

实体类 Rumenz.java

package com.rumenz;
public class Rumenz{
    private Integer id;
    private String name;
    @Override
    public String toString() {
        return "Rumenz{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {return id;}

    public void setId(Integer id) {this.id = id;}

    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
}

BeanFactory#getBean 繁多类型查找

package com.rumenz;



import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;


public class DemoApplication {public static void main(String[] args) {AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
         ac.register(DemoApplication.class); // 没有注册 Rumenz.class
         ac.refresh();
         // 通过 BeanFactory#getBean
         byBeanFactory(ac);
         ac.close();}

    private static void byBeanFactory(AnnotationConfigApplicationContext ac) {printBeanException("byBeanFactory",()->ac.getBean(Rumenz.class));
    }

    private static void printBeanException(String msg,  Runnable runnable) {System.err.println("from---->>>"+msg);
        try{runnable.run();
        }catch (Exception e){e.printStackTrace();
        }

    }


}

输入

因为没有注册 Rumenz.class, 所以查找报错, 不平安

from---->>>byBeanFactory
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rumenz.Rumenz' available

ObjectFactory#getObject 繁多类型查找

package com.rumenz;



import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;


public class DemoApplication {public static void main(String[] args) {AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
         ac.register(DemoApplication.class);
         ac.refresh();

         // 通过 ObjectFactory#getObject
         byObjectFactory(ac);
         ac.close();}

    private static void byObjectFactory(AnnotationConfigApplicationContext ac) {ObjectProvider<Rumenz> beanProvider = ac.getBeanProvider(Rumenz.class);
        printBeanException("byObjectFactory",()->beanProvider.getObject());
    }
    private static void printBeanException(String msg,  Runnable runnable) {System.err.println("from---->>>"+msg);
        try{runnable.run();
        }catch (Exception e){e.printStackTrace();
        }

    }
}

输入

因为没有注册 Rumenz.class, 所以查找报错, 不平安

from---->>>byObjectFactory
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rumenz.Rumenz' available

ObjectProvider#getIfAvailable 繁多类型查找

package com.rumenz;



import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;


public class DemoApplication {public static void main(String[] args) {AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
         ac.register(DemoApplication.class);
         ac.refresh();
         // 通过 ObjectProvider#getIfAvailable
         byObjectProvider(ac);
         ac.close();}

    private static void byObjectProvider(AnnotationConfigApplicationContext ac) {ObjectProvider<Rumenz> beanProvider = ac.getBeanProvider(Rumenz.class);
        printBeanException("byObjectProvider",()->beanProvider.getIfAvailable());
    }

    private static void printBeanException(String msg,  Runnable runnable) {System.err.println("from---->>>"+msg);
        try{runnable.run();
        }catch (Exception e){e.printStackTrace();
        }

    }


}

输入

不存在 Rumenz.class, 也不会报错, 平安

from---->>>byObjectProvider

ListableBeanFactory#getBeansOfType 汇合查找

package com.rumenz;



import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;


public class DemoApplication {public static void main(String[] args) {AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
         ac.register(DemoApplication.class);
         ac.refresh();
         // 通过 ListableBeanFactory#getBeansOfType 汇合类型查找
         byListableBeanFactory(ac);


         ac.close();}

    private static void byListableBeanFactory(AnnotationConfigApplicationContext ac) {printBeanException("byListableBeanFactory",()->ac.getBeansOfType(Rumenz.class));

    }

    private static void printBeanException(String msg,  Runnable runnable) {System.err.println("from---->>>"+msg);
        try{runnable.run();
        }catch (Exception e){e.printStackTrace();
        }

    }


}

输入

不存在 Rumenz.class, 也不会报错, 平安

from---->>>byListableBeanFactory

ObjectProvider#Stream 汇合类型查找

package com.rumenz;



import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;


public class DemoApplication {public static void main(String[] args) {AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
         ac.register(DemoApplication.class);
         ac.refresh();
        

         // 通过 ObjectProvider#stream
         byObjectProviderStream(ac);


         ac.close();}

    private static void byObjectProviderStream(AnnotationConfigApplicationContext ac) {ObjectProvider<Rumenz> beanProvider = ac.getBeanProvider(Rumenz.class);
        printBeanException("byObjectProviderStream",()->beanProvider.stream().forEach(System.out::println));
    }

    private static void printBeanException(String msg,  Runnable runnable) {System.err.println("from---->>>"+msg);
        try{runnable.run();
        }catch (Exception e){e.printStackTrace();
        }

    }

}

输入

不存在 Rumenz.class, 也不会报错, 平安

from---->>>byObjectProviderStream

源码:https://github.com/mifunc/Spr…

原文: https://rumenz.com/rumenbiji/.html

正文完
 0