
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextxml");//获取配置文件信息
Test1 test1=(Test1) contextgetBean("t1");//根据bean的id获得bean
test1show(test1getName(), test1getSex());
也可以用注解获取
@Resource //先按属性名查找 如果没有 再按属性类型查找
//以下两种注解 需要有setter 方法
@Autowired//按类型查找
@Qualifier("beanName")//按名字查找
private BizCheckResultDao bizCheckResultDao;
GamePackService gamePackService;
@Override
public void init() throws ServletException
{
superinit();
WebApplicationContext wac = WebApplicationContextUtils
getRequiredWebApplicationContext(getServletContext());
gamePackService = (GamePackService) wacgetBean("gamePackService");
}、
在servlet init 方法中加入
1、@Bean只能存在于配置类中,当配置类中的方法存在这个注解时,这个注解会将方法的返回值放入ioc容器中去。
2、当@Bean标注的方法中有参数的时候,会去ioc容器中寻找该类型的bean 作为参数注入进该方法中。当找不到同类型的bean的的时候会报错。
以上就是关于如何取得Spring管理的bean全部的内容,包括:如何取得Spring管理的bean、怎么在servlet里面获取spring通过注解方式注入的service bean、@Bean注解 放到方法上的时候这两个特性你知道吗等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)