加载applicationContext文件的七种方法

本文介绍了在Spring框架中初始化JdbcTemplate的多种方法,包括通过不同类型的ApplicationContext实例化以及从WebApplicationContext获取Bean等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  /** 1 把applicationContext.xml放到classpath目录下*/
//     Resource resource = new ClassPathResource("applicationContext.xml");
//     BeanFactory factory = new XmlBeanFactory(resource);
//     JdbcTemplate jt = (JdbcTemplate)factory.getBean("jdbcTemplate");
     /** 2*/
//  WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ctx);
//     JdbcTemplate jt = (JdbcTemplate) wac.getBean("jdbcTemplate");
     /** 3*/
//     ApplicationContext ac = new FileSystemXmlApplicationContext("D:\\apache-tomcat-6.0.29\\webapps\\jf_shop\\WEB-INF\\applicationContext.xml");
//     JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");
     /** 4*/
//     WebApplicationContext wac = (WebApplicationContext)ctx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
//     JdbcTemplate jt = (JdbcTemplate) wac.getBean("jdbcTemplate");
     /** 5把applicationContext.xml放到classpath目录下*/
//     ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_ty.xml");
     ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {"applicationContext_ty.xml","applicationContext.xml","applicationContext_zj.xml","applicationContext_mtb.xml","applicationContext_wll.xml"});
        BeanFactory factory = ac;
     JdbcTemplate jt = (JdbcTemplate)factory.getBean("jdbcTemplate");
//        dao =   (PingPaiDao)factory.getBean("dao");
  /** 6  必须继承MultiActionController类*/
//  JdbcTemplate jt = (JdbcTemplate) getApplicationContext().getBean("jdbcTemplate");
  /** 7*/
//     XmlWebApplicationContext xctx = new XmlWebApplicationContext();
//     xctx.setConfigLocations(new String[] {"/WEB-INF/applicationContext.xml"});
//     xctx.setServletContext(ctx);
//     xctx.refresh();
     /** 8*/
//     JdbcTemplate jt = (JdbcTemplate)xctx.getBean("jdbcTemplate");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值