spring 直接获取bean

org.springframework.web.context.ContextLoader

通过ContextLoader

获取当前的ApplicationContext

然后取得对应的bean

ContextLoader.getCurrentWebApplicationContext().getBean(name)

例子

配置文件

<bean id="myService " class="com.zw.MyServiceImpl"></bean>

调用

MyService myService = (MyService )ContextLoader.getCurrentWebApplicationContext().getBean("myService ");

注意需要强制转化类型

bean名称小写

猜你喜欢

转载自zwxiaole.iteye.com/blog/1853061
今日推荐