三种getBean的方法
作者:互联网
三种getBean的方法 (重载 参数不同)
public interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
Object getBean(String var1) throws BeansException;
<T> T getBean(String var1, Class<T> var2) throws BeansException;
Object getBean(String var1, Object... var2) throws BeansException;
<T> T getBean(Class<T> var1) throws BeansException;
<T> T getBean(Class<T> var1, Object... var2) throws BeansException;
<T> ObjectProvider<T> getBeanProvider(Class<T> var1);
<T> ObjectProvider<T> getBeanProvider(ResolvableType var1);
区别
1.根据id,xml配置文件可以有多个相同类型的bean 返回Object对象 2.根据类型,xml配置文件中不能有相同class的bean,返回bean对象
<bean id="Service" class="com.z.service.Impl.UserServiceImpl"/>
<bean id="Service" class="com.z.service.Impl.UserServiceImpl"/>
配置了多个同类型bean,此时使用类型的方法获得,会报错如下;
UserServiceImpl bean = applicationContext.getBean(UserServiceImpl.class);
No qualifying bean of type 'com.z.service.Impl.UserServiceImpl' available: expected single matching bean but found 2: Service2,Service
3.根据id和类型
UserServiceImpl bean = applicationContext.getBean("Service2",UserServiceImpl.class);
返回bean对象
标签:UserServiceImpl,var1,BeansException,Object,getBean,bean,三种,方法 来源: https://www.cnblogs.com/zzw2/p/16448802.html