private static ApplicationContext ctx= new ClassPathXmlApplicationContext(
"spring/ws-client.xml");
private static Cache cache = (Cache) ctx.getBean("cache");
当bean继承spring 的FactoryBean时,
使用applicationContext.getBean(beanName)
得到的是bean的getObject方法的返回值,而不是bean本身。
此时需要的话请用
applicationContext.getBean('&'+beanName)将得到bean本身。而且使用
applicationContext.getBean(beanName)的时候bean的getObject只会调用一次,此后都是直接返回原先的对象,
使用applicationContext.getBean('&'+beanName)后再调用bean.getObject方法就可以多次调用bean.getObject方法体,从而改变返回对象的值
分享到:
相关推荐
JdbcTemplate jdTemplate =(JdbcTemplate) applicationContext.getBean("jdbcTemplate"); jdTemplate.execute("create table account(" id int primary key auto_increment," + "username varchar(50)," + "balance...
今天小编就为大家分享一篇关于spring中通过ApplicationContext getBean获取注入对象的方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
接着上一讲的Java工程。 配置文件详解 alias:为bean设置别名;可以设置多个别名 id是bean的标识符,要...如果不配置id和name,可以根据applicationContext.getBean(Class)获取对象 团队协作可以通过import来完成
JdbcTemplateTool jtt = super.applicationContext.getBean("jdbcTemplateTool",JdbcTemplateTool.class); Employee e = new Employee(); e.setId(4); e.setName("billy"); Date now = new ...
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //输出spring容器 System.out.println(ctx); //打印加载的bean名称 System.out.println(java.util.Arrays....
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/368874
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO"); 如果是两个以上: ApplicationContext ac = new ...
以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. 在配置文件中注入<bean class="xxx.xxx.SpringContextHolder" lazy-init="false" /> 即可使用
Resource res = new ClassPathResource("applicationContext.xml"); XmlBeanFactory xbf = new XmlBeanFactory(res); //开启事物 StudentDao dao = (StudentDao) xbf.getBean("studentDao"); dao.save(stu...
ApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(this.getServletConfig().getServletContext()); Workflow wf = (Workflow)cxt.getBean("workflow"); 用osworkflow自带的designer把...
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-jms-producer.xml"); //获取生产者发送消息服务接口 MessageSender messageSender = (MessageSender)ac.getBean...
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml"); JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsQueueTemplate"); Destination ...
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); ESBasicService service = context.getBean(ESBasicService.class); /*service.createIndexAndMapping...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested ...
---------------------- applicationContext.xml ------------------------ <!-- 配置SessionFactory(整合Hibernate) --> <context:property-placeholder location="classpath:jdbc.properties" /> ...
ISysUserGroupService sysUserGroupService = (ISysUserGroupService) ctx.getBean(ISysUserGroupService.SERVICE_NAME); SysUserGroup sysUserGroup = new SysUserGroup(); sysUserGroup....
Spring3ProjectScopePrototype Spring 3 + IOC(核心容器)+ Scope(原型),示例由于作用域是原型,因此在这种情况下,如果您多次调用getBean方法,则容器仅将多个对象引用发送到JVM,并会创建多个对象使用...
但是有一些部分自己没有能完成,主要是如何从spring容器里取出ApplicationContext,这个我的实现比较low,看了看讲义,才OK的。 我的实现: [java] view plain copy WebApplicationContext acc = ...
MessageSender sender = (MessageSender)context.getBean("messageSender"); 10. sender.send("message"); 11. } 12. } 执行结果: Log before public abstract void ...
WEB-INF下的applicationContext.xml为Spring的配置文件,struts-config.xml为Struts的配置文件,file-upload.jsp为文件上传页面,file-list.jsp为文件列表页面。 本文后面的章节将从数据持久层->业务层->Web层的...