`
dawuafang
  • 浏览: 1107669 次
文章分类
社区版块
存档分类
最新评论

applicationContext.getBean()

 
阅读更多

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方法体,从而改变返回对象的值

分享到:
评论

相关推荐

    基于java的企业级应用开发:Spring JdbcTemplate 的常用方法.ppt

    JdbcTemplate jdTemplate =(JdbcTemplate) applicationContext.getBean("jdbcTemplate"); jdTemplate.execute("create table account(" id int primary key auto_increment," + "username varchar(50)," + "balance...

    spring中通过ApplicationContext getBean获取注入对象的方法实例

    今天小编就为大家分享一篇关于spring中通过ApplicationContext getBean获取注入对象的方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    第五讲spring配置文件.rar

    接着上一讲的Java工程。 配置文件详解 alias:为bean设置别名;可以设置多个别名 id是bean的标识符,要...如果不配置id和name,可以根据applicationContext.getBean(Class)获取对象 团队协作可以通过import来完成

    JdbcTemplateTool.zip

     JdbcTemplateTool jtt = super.applicationContext.getBean("jdbcTemplateTool",JdbcTemplateTool.class);  Employee e = new Employee();  e.setId(4);  e.setName("billy");  Date now = new ...

    spring技术入门相关源码

    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //输出spring容器 System.out.println(ctx); //打印加载的bean名称 System.out.println(java.util.Arrays....

    利用spring的ApplicationContext在程序中唤醒quartz的job

    NULL 博文链接:https://dolphin-ygj.iteye.com/blog/368874

    maven相关资料

    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO"); 如果是两个以上: ApplicationContext ac = new ...

    获取spring管理的bean

    以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. 在配置文件中注入<bean class="xxx.xxx.SpringContextHolder" lazy-init="false" /> 即可使用

    spring AOP

    Resource res = new ClassPathResource("applicationContext.xml"); XmlBeanFactory xbf = new XmlBeanFactory(res); //开启事物 StudentDao dao = (StudentDao) xbf.getBean("studentDao"); dao.save(stu...

    spring+hibernate+osworkflow

    ApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(this.getServletConfig().getServletContext()); Workflow wf = (Workflow)cxt.getBean("workflow"); 用osworkflow自带的designer把...

    ActiveMQ-demo

    ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-jms-producer.xml"); //获取生产者发送消息服务接口 MessageSender messageSender = (MessageSender)ac.getBean...

    spring-jms:Spring JmsTemplate演示

    ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml"); JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsQueueTemplate"); Destination ...

    基于elasticsearch的CRUD简单封装java版

    ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); ESBasicService service = context.getBean(ESBasicService.class); /*service.createIndexAndMapping...

    struts2驱动包

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested ...

    OA项目SSH整合框架

    ---------------------- 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

    Spring3ProjectScopePrototype Spring 3 + IOC(核心容器)+ Scope(原型),示例由于作用域是原型,因此在这种情况下,如果您多次调用getBean方法,则容器仅将多个对象引用发送到JVM,并会创建多个对象使用...

    spring+springmvc+mybatis的整合

    但是有一些部分自己没有能完成,主要是如何从spring容器里取出ApplicationContext,这个我的实现比较low,看了看讲义,才OK的。 我的实现: [java] view plain copy WebApplicationContext acc = ...

    spring aop 实现源代码--xml and annotation(带lib包)

    MessageSender sender = (MessageSender)context.getBean("messageSender"); 10. sender.send("message"); 11. } 12. } 执行结果: Log before public abstract void ...

    ssh(structs,spring,hibernate)框架中的上传下载

    WEB-INF下的applicationContext.xml为Spring的配置文件,struts-config.xml为Struts的配置文件,file-upload.jsp为文件上传页面,file-list.jsp为文件列表页面。  本文后面的章节将从数据持久层->业务层->Web层的...

Global site tag (gtag.js) - Google Analytics