15.2 Spring配置数据库驱动
使用”org.springframework.jdbc.datasource.DriverManagerDataSource”数据源来配置数据库驱动。示例如下:
15.3 Spring里面applicationContext.xml文件改名
ContextLoaderListener是一个ServletContextListener, 它在你的web应用启动的时候初始化。缺省情况下, 它会在WEB-INF/applicationContext.xml文件找Spring的配置。
你可以通过定义一个
在web环境中配置applicationContext.xml文件:
org.springframework.web.context.ContextLoaderListener 或:
org.springframework.web.context.ContextLoaderServlet
通过如下方法取出applicationContext实例: ApplicationContext
ac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext
15.4 web应用里面配置spring
在web.xml中加入如下同容,在启动web服务器时加载/WEB-INF/applicationContext.xml中的内容。
org.springframework.web.context.ContextLoaderServlet
通过如下类得到ApplicationContext实例
WebApplicationContextUtils.getWebApplicationContext 15.5 spring+hibernate配置文件
dataSource
sessionFactory:hibernate.cfg.xml transactionManager
userDao (extends HibernateDaoSupport) sessionFactory facade proxy
sessionFactory transactionManager facade
在myeclipse中先加入spring环境再加入hibernate环境。
如果spring与hibernate结合在一起可以不需要hibernate.cfg.xml文件
另一种说法:
在myeclipse创建一个web工程 加入spring环境
加入hibernate环境,会自动找到applicationContext.xml文件,加入dataSource及sessionFactory两个bean,
通过这两个文件可以不需要hibernate中的hibernate.cfg.xml文件。 加入一个hibernate事务管理的bean,其名是transactionManager 加入一个dao进行操作的bean,其有一个接口。
加入一个基于aop服务的代理,其包括transactionManager及dao为其提代服务 通过代理得到dao的对应的接口,作真正的处理
Spring里定义hibernate mapping
添加hibernate mapping 文件到web/WEB-INFapplicationContext.xml文件里面。示例如下:
目录下的
15.6 如何配置spring+struts?
在struts-config.xml加入一个插件,通过它加载applicationContext.xml
在struts-config.xml修改action-mapping标记,具体action交给了DelegateActionProxy 通过DelegateActionProxy进入一spring的环境。
在spring的applicationContext.xml加入 16. Spring设值注入简单实例(spring简单应用:面向接口编程) 1. 首先新建2个接口,Person(人),Axe(斧头)接口。 Person接口: package springtest; //定义Person接口 public interface Person { //Person接口里定义一个使用斧头的方法 public void useAxe(); } Axe接口: package springtest; //定义Axe接口 public interface Axe { //Axe接口有个砍柴的方法 public String chop(); } 2. 新建2个实现类,Chinese类,StoneAxe类。 Chinese类: package springtest; //Chinese实现Person接口 public class Chineseimplements Person { //面向Axe接口编程,而不是具体的实现累 private Axe axe; //默认的构造器 public Chinese(){} public void setAxe(Axe axe){ this.axe=axe; } //实现Person接口的useAxe方法 public void useAxe() { System.out.println(axe.chop()); } } StoneAxe类: package springtest; public class StoneAxeimplements Axe { //默认构造器 public StoneAxe(){} //实现Axe接口的chop方法 public String chop() { return \斧头砍柴好慢\ } } 3. 下面是spring配置: xmlns=\ xmlns:xsi=\ xmlns:p=\