想必用过Spring的程序员们都有这样的感觉,Spring把逻辑层封装的太完美了(个人感觉View层封装的不是很好)。以至于有的初学者都不知道Spring配置文件的意思,就拿来用了。所以今天我给大家详细解释一下Spring的applicationContext.xml文件。Ok,我还是通过代码加注释的方式为大家演示:
以下是详解Spring的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 ><!-- 数据库驱动,我这里使用的是Mysql数据库 --> <property > <value>com.mysql.jdbc.Driver</value> </property><!-- 数据库地址,这里也要注意一下编码,不然乱码可是很郁闷的哦! --> <property > <value> jdbc:mysql://localhost:3306/tie?useUnicode=true&characterEncoding=utf-8 </value> </property><!-- 数据库的用户名 --> <property > <value>root</value> </property><!-- 数据库的密码 --> <property > <value>123</value> </property> </bean><!-- 把数据源注入给Session工厂 --> <bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property > <ref bean="dataSource" /> </property><!-- 配置映射文件 --> <property > <list> <value>com/alonely/vo/User.hbm.xml</value> </list> </property> </bean><!-- 把Session工厂注入给hibernateTemplate --> <!-- 解释一下hibernateTemplate:hibernateTemplate提供了很多方便的方法,在执行时自动建立 HibernateCallback 对象,例如:load()、get()、save、delete()等方法。 --> <bean class="org.springframework.orm.hibernate3.HibernateTemplate"> <constructor-arg> <ref local="sessionFactory" /> </constructor-arg> </bean><!-- 把DAO注入给Session工厂 --> <bean > <property > <ref bean="sessionFactory" /> </property> </bean><!-- 把Service注入给DAO --> <bean > <property > <ref local="userDAO" /> </property> </bean><!-- 把Action注入给Service --> <bean > <property > <ref bean="userService" /> </property> </bean></beans>
以上Spring的applicationContext.xml文件我是用的SSH架构,如果您用Spring的MVC架构,其原理也是一样的。