Struts,Spring,Hibernate面试题总结(6)

2019-08-20 19:27

15.2 Spring配置数据库驱动

使用”org.springframework.jdbc.datasource.DriverManagerDataSource”数据源来配置数据库驱动。示例如下:

org.hsqldb.jdbcDriver

jdbc:hsqldb:db/appfuse

sa

15.3 Spring里面applicationContext.xml文件改名

ContextLoaderListener是一个ServletContextListener, 它在你的web应用启动的时候初始化。缺省情况下, 它会在WEB-INF/applicationContext.xml文件找Spring的配置。

你可以通过定义一个元素名字为”contextConfigLocation”来改变Spring配置文件的位置。示例如下:

org.springframework.web.context.ContextLoaderListener

contextConfigLocation /WEB-INF/xyz.xml

在web环境中配置applicationContext.xml文件:

org.springframework.web.context.ContextLoaderListener 或:

context

org.springframework.web.context.ContextLoaderServlet

1

通过如下方法取出applicationContext实例: ApplicationContext

ac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext

15.4 web应用里面配置spring

在web.xml中加入如下同容,在启动web服务器时加载/WEB-INF/applicationContext.xml中的内容。

context

org.springframework.web.context.ContextLoaderServlet

1

通过如下类得到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文件里面。示例如下:

org/appfuse/model/User.hbm.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=\


Struts,Spring,Hibernate面试题总结(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:骨科手术同意书

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: