依赖查找安全性对比
依赖查找类型 | 代表实现 | 是否安全 |
---|---|---|
单一类型 | BeanFactory#getBean | 否 |
ObjectFactory#getObject | 否 | |
ObjectProvider#getIfAvailable | 是 | |
集合类型 | ListableBeanFactory#getBeansOfType | 是 |
ObjectProvider#stream | 是 |
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; } }
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
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
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
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
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