
getBean是用来获取applicationContextxml文件里bean的,()写的是bean的id。
一种是singleton,一种是prototype,默认的是singleton,这种定义的bean实例的作用是与spring的容器一致的,只有spring容器初始化,调用getBean得到的singleton实例始终是同一个bean的实例spring定义的bean有两种作用范围,是每当调用getBean得到实例的时候spring都会new一个实例来给你,而prototype的实例。
通过bring程序获取。
最近项目中遇到一个业务场景,就是在Spring容器启动后获取所有的Bean中实现了一个特定接口的对象,第一个想到的ApplicationContextAware,在setApplicationContext中去通过ctx获取所有的bean。
后来发现好像逻辑不对,这个方法不是在所有bean初始化完成后实现的,后来试了一下看看有没有什么Listener之类的,发现了好东西ApplicationListener,然后百度一下ApplicationListener用法。
不用配置xml,直接java代码实现,参考代码如下:
public class GetApplicationContext {private static class ApplicationContextHolder {
// 单例变量
private static ApplicationContext AC = new FileSystemXmlApplicationContext(
"classpath:applicationContextxml");
}
// 私有化的构造方法,保证外部的类不能通过构造器来实例化。
private GetApplicationContext() {
}
// 获取单例对象实例
public static ApplicationContext getInstance() {
if (ApplicationContextHolderAC == null) {
ApplicationContextHolderAC = new FileSystemXmlApplicationContext(
"classpath:applicationContextxml");
}
return ApplicationContextHolderAC;
}
}
获取所有spring自动装配的bean:
<span style="font-size:18px;">//获取spring装配的bean个数GetApplicationContextgetInstance()getBeanDefinitionNames()length;
//逐个打印出spring自动装配的bean。根据我的测试,类名第一个字母小写即bean的名字
for(int i=0;i<33;i++){
Systemoutprintln( GetApplicationContextgetInstance()getBeanDefinitionNames()[i]);
}</span>
然后通过下面的代码获取到spring注解装配的bean供自己使用:
<span style="font-size:18px;">StorageReturnService ossService = (StorageReturnService) GetApplicationContextgetInstance()getBean("storageReturnServiceImpl");</span>只要搜一下“获取 spring bean”,获取spring bean的N中方法都出来了。线程中获取和普通类中获取方法是一样的。
下面是一种方法。这个类需要配置在Spring中。
使用的时候直接:Bean bean = SpringUtilgetBean("bean的id", Beanclass);//Beanclass是bean的类对象,比如获取 UserService的bean,就是:
1
UserService us = SpringUtilgetBean("userService", UserServiceclass);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import orgspringframeworkbeansBeansException;
import orgspringframeworkcontextApplicationContext;
import orgspringframeworkcontextApplicationContextAware;
/
Spring IOC上下文工具类
@ClassName: SpringUtil
@Description:
@author
@date 2014-6-21 下午02:06:48
/
public class SpringUtil implements ApplicationContextAware {
/
当前IOC
/
private static ApplicationContext applicationContext;
/
设置当前上下文环境,此方法由spring自动装配
/
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
applicationContext = arg0;
}
/
从当前IOC获取bean
@param id
bean的id
@return
/
public static <T> T getBean(String id, Class<T> requiredType) {
T t = applicationContextgetBean(id, requiredType);
return t;
}
}
1
1实例化bean的三种方法:
(1) 构造器
<!-- 体验1 -->
<bean id="personService" class="compersiaPersonServiceBean">
<!-- index 代表方法的参数序号,由0开始,基本的类型Type可以不声明-->
<constructor-arg index="0" value="构造注入的name" />
<constructor-arg index="1" type="compersiaIDaoBean" ref="personDao"/>
</bean>
对应类
public PersonServiceBean(String name, IDaoBean personDao) {
thisname = name;
thispersonDao = personDao;
}
<!-- 体现2-->
<bean id="personDao" class="cnitcastdaoimplPersonDaoBean" />
<bean id="personServiceBean" class="cnitcastserviceimplPersonServiceBean"
lazy-init="true" init-method="init" destroy-method="destory">
<!-- ref属性对应id personDao值 name属性对应接口的getter方法名称-->
<property name="personDao" ref="personDao" />
<!-- 体验3 -->
<!-- 注入属性值 -->
<property name="name" value="123"/>
<!-- Set的注入 -->
<property name="sets">
<set>
<value>sets:第一个值</value>
<value>sets:第二个值</value>
<value>sets:第三个值</value>
</set>
</property>
<!-- List的注入 -->
createInstance(){
return new ();
}
程序如下:
1 ServletContext context = configgetServletContext();
2 ApplicationContext ac = WebApplicationContextUtils
3 getWebApplicationContext(context);
4 TestBean testBean = (TestBean) acgetBean("testBean");
//
public ModelAndView toPage(>
以上就是关于关于spring的getBean方法全部的内容,包括:关于spring的getBean方法、除了autoware其他获取注解bean的方式、如何获取spring 注解的bean等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)