基于xml的Spring配置二(Spring 的get方法)

时间:2022-12-07 14:10:15


方法定义

返回值和参数

Object getBean(String beanName)

根据beanName从容器中获取Bean实例,要求容器中Bean唯一,返回值为Object,需要强转

T getBean(Class type)

根据Class类型从容器中获取Bean实例,要求容器中Bean类型唯一,返回值为Class类型实例,无需强转

T getBean(String beanName, Class type)

根据beanName从容器中获得Bean实例,返回值为Class类型实例,无需强转

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

UserService userService2 = applicationContext.getBean(UserService.class);

UserService userService3 = applicationContext.getBean("userService", UserService.class);