Servlet, Spring, hibernate基本配置

  本文主要描述个人对于servlet,spring,及hibernate初始化过程的理解。
     1,一切皆因web.xml而起。web.xml,也叫deployment descriptor。从这个名字就知道它包含了web程序运行的所有信息。当servlet容器初始化的时候就会读取该文件的内容,来决定如何初始化程序上下文,如何定义servlet的参数,如何定义Request Dispatch,如何挂载filter,如何运行Listener等信息。来看一个简单的web.xml的例子:

web.xml
 
   
1 <? xml version="1.0" encoding="UTF-8" ?>
2   < web-app xmlns ="http://java.sun.com/xml/ns/javaee" ; xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" ;
3 xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee
4 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" ;
5 version ="3.0" >
6 < display-name > chronicles </ display-name >
7
8 < context-param >
9 < param-name > key </ param-name >
10 < param-value > value </ param-value >
11 </ context-param >
12
13 < listener >
14 < listener-class > org.springframework.web.context.ContextLoaderListener
15 </ listener-class >
16 </ listener >
17
18 < filter >
19 < filter-name > LoggingFilter </ filter-name >
20 < filter-class > org.springframework.web.filter.Log4jNestedDiagnosticContextFilter </ filter-class >
21 </ filter >
22
23 < filter-mapping >
24 < filter-name > LoggingFilter </ filter-name >
25 < url-pattern > /* </ url-pattern >
26 </ filter-mapping >
27
28 < servlet >
29 < servlet-name > chronicles </ servlet-name >
30 < servlet-class > org.springframework.web.servlet.DispatcherServlet
31 </ servlet-class >
32 < load-on-startup > 1 </ load-on-startup >
33 </ servlet >
34
35 < servlet-mapping >
36 < servlet-name > chronicles </ servlet-name >
37 < url-pattern > / </ url-pattern >
38 </ servlet-mapping >
39
40 < welcome-file-list >
41 < welcome-file > index.jsp </ welcome-file >
42 </ welcome-file-list >
43 </ web-app >
44

  该例子包含了常用的一些标签。第8行定义了一个context param。可以在Servlet类中使用ServletContext.getInitParameter()或者 ServletContext.getInitParameterNames()方法来获取该指。第13行定义了一个listener。listener有以下几种:ServletContextListener,ServletContextAttributeListener,HttpSessionListener,HttpSessionAttributeListener。他们分别定义了一些接口方法,在特定情况下触发相应的事件。具体的事件可以参看这里。本文下面要讲spring的加载过程,就是通过ServletContextListener实现的。第18行定义了一个filter名字和它对应的类。然后在23行定义了什么样的url-pattern调用该filter。下面的servlet和servlet-mapping标签也是与filter类似的功能。最后40行的welcom-file-list给出了网站默认显示页面。

  2,好了,基本的servlet配置就是这样。下面讲下spring的配置。其实说起来就很简单了。就是上面web.xml文件的第13行所定义的。然后在web/WEB-INF/文件下下添加ApplicationContext.xml文件如下即可。servlet容器启动的时候会去调用org.springframework.web.context.ContextLoaderListener的contextInitialized方法,该方法读取ApplicationContext.xml文件的内容对其中的bean进行初始化。

  这里简单说一下spring的IOC。IOC的就是说不在类代码中出现new关键字,所有需要的其他类的实例都是通过在配置文件中进行定义bean,然后使用spring框架运行时动态的注入。注入分为constructor和setter注入两种,也就是说如果你希望spring帮你注入某些实例,就要在该类中准备好相应的constructor或者setter。但是最近在用springmvc,发现直接加上@Autowired的annotation不需要任何显示的注入途径就能够被注入,真是太神奇的。不过其中究竟还没搞太明白,以后再研究。下面看一下ApplicationContext.xml:

ApplicationContext.xml
 
   
1 <? xml version="1.0" encoding="UTF-8" ?>
2   < beans xmlns ="http://www.springframework.org/schema/beans"
3 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop ="http://www.springframework.org/schema/aop"
4 xmlns:context ="http://www.springframework.org/schema/context" xmlns:tx ="http://www.springframework.org/schema/tx"
5 xmlns:mvc ="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation ="
7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
11 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" >
12
13 < tx:annotation-driven transaction-manager ="transactionManager" />
14
15 < bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource"
16 lazy-init ="true" autowire-candidate ="true" >
17 < property name ="driverClassName" value ="com.mysql.jdbc.Driver" />
18 < property name ="maxActive" value ="100" />
19 < property name ="url" value ="jdbc:mysql://localhost:3306/chronicles?sessionVariables=storage_engine=InnoDB" />
20 < property name ="username" value ="root" />
21 < property name ="password" value ="root" />
22 </ bean >
23
24 < bean id ="namingStrategy" class ="org.hibernate.cfg.ImprovedNamingStrategy" />
25
26 < bean id ="sessionFactory"
27 class ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
28 < property name ="dataSource" ref ="dataSource" />
29 < property name ="namingStrategy" ref ="namingStrategy" />
30 < property name ="packagesToScan" value ="chronicles.models" />
31 </ bean >
32
33 < bean id ="transactionManager"
34 class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
35 < property name ="sessionFactory" >
36 < ref bean ="sessionFactory" />
37 </ property >
38 </ bean >
39 < bean id ="chroniclesDao" class ="chronicles.repository.ChroniclesHibernateDAOSupport" >
40 < constructor-arg ref ="sessionFactory" />
41 </ bean >
42
43   </ beans >

  这里只有简单的几个bean的定义。一是因为用做示例,二是因为强大的springmvc大量使用了annotation来定义和注入。从上面的例子中我们可以清晰的看到我们都定义了那些bean,各个bean之间的依赖关系。

  3,Hibernate的配置。在上面的文件中可以看到第26行就是利用spring对hibernate SessionFactory的一个拼装,也就是对其的配置。Hibernate本身是用来简化数据库操作的。通过上面的配置使其成为一个bean,然后就可以在其他类中通过annotation方便的实例化。然后就可以使用SessionFactory的API去做你想做的各种数据库的操作了。这里就不详细讲Hibernate的使用了。只通过下面一个简单的例子来对spring的IOC和Hibernate的API有个大概的认识:

 

Hibernate使用示例
 
   
1 public class ChroniclesHibernateDAOSupport < Model >
2 private SessionFactory sessionFactory;
3
4 @Autowired
5 public ChroniclesHibernateDAOSupport(SessionFactory sessionFactory) {
6 this .sessionFactory = sessionFactory;
7 }
8
9 public boolean update(Model model) {
10 try {
11 getHibernateSession().merge(model);
12 return true ;
13 } catch (PropertyValueException e) {
14 return false ;
15 }
16 }
17
18 public boolean save(Model model) {
19 try {
20 return getHibernateSession().save(model) != null ;
21 } catch (PropertyValueException e) {
22 return false ;
23 } catch (ConstraintViolationException e) {
24 return false ;
25 }
26 }
27
28 public List < Model > findAll(String modelName) {
29 return getHibernateSession().createQuery(String.format( " from %s " , modelName)).list();
30 }
31
32 public boolean delete(Model model) {
33 getHibernateSession().delete(model);
34 return true ;
35 }
36
37 public void flush() {
38 getHibernateSession().flush();
39 }
40
41 public Session getHibernateSession() {
42 return sessionFactory.getCurrentSession();
43 }
44 }

 

  第4行通过@Autowired annotation将配置文件中的SessionFactory自动注入。然后使用其封装一些通用操作,如save,delete,select,update。在此之上可以继续封装不同的层次,封装你想要的操作。

 

转载于:https://www.cnblogs.com/cuiliqiang/archive/2010/11/01/1865878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值