这个是我的WEB-INF/applicationContext.xml文件:
配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName"
   value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
  <property name="url"
   value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs" />
  <property name="username" value="sa" />
  <property name="password" value="" />
 </bean>
 
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource">
  <ref bean="dataSource"/>
  </property>
 </bean>
</beans>现在我要在程序里获得jdbcTemplate对象应该怎么写?
谢谢了 给点代码

解决方案 »

  1.   

    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;....ServletContext servletContext = actionServlet.getServletContext();
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    Object obj = wac.getBean("jdbcTemplate");
      

  2.   

    我用的是普通的类不是servlet如何获得现在用的
    String[] ctxs = new String[]{"D:/eclipse_3.2/workspace/sp/WebContent/WEB-INF/applicationContext.xml"};
    ApplicationContext ctx = new FileSystemXmlApplicationContext(ctxs);相对路径不知道怎么定义
      

  3.   

    确实没做过spring还没入门呢请楼上的高人指点