以我做的一个项目为例,对applicationContext.xml文件做简单的介绍:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.sqlServerDriver">
</property>
(MysqL的驱动只需改变value的值为:“com.MysqL.jdbc.Driver”)
<!-- 数据库配置 -->
<property name="url"
value="jdbc:sqlserver://localhost:1433;databasename=our">
</property>
(MysqL的配置只需改变value的值为:“jdbc:MysqL://localhost:3306/our”,其中也可对编码进行设置。
如:“jdbc:MysqL://localhost:3306/our?useUnicode=true&characterEncoding=utf-8”)
<property name="password" value="123"></property>
</bean>
<!-- 定义Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionfactorybean">
<!--定义SessionFactory必须注入DataSource -->
<ref bean="dataSource" />
</property>
<!-- 定义Hibernate的SessionFactory。例如:方言和是否显示sql -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.sqlServerDialect
</prop>
<prop key="show_sql">true</prop>
</props>
</property>
<!-- 配置映射文件 -->
<property name="mappingResources">
<list>
<value>org/bookstore/model/Book.hbm.xml</value>
<value>org/bookstore/model/User.hbm.xml</value>
<value>org/bookstore/model/Orders.hbm.xml</value>
<value>org/bookstore/model/Orderitem.hbm.xml</value>
<value>org/bookstore/model/Catalog.hbm.xml</value>
<value>org/bookstore/model/Files.hbm.xml</value>
</list>
</property>
</bean>
<!-- 所有DAO全部注入 -->
<bean id="userDAO" class="org.bookstore.dao.impl.UserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="catalogDAO" class="org.bookstore.dao.impl.CatalogDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="bookDAO" class="org.bookstore.dao.impl.BookDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="orderDAO" class="org.bookstore.dao.impl.OrderDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- <bean id="imageDao" class="org.bookstore.dao.impl.ImageDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> -->
<!-- 注入Service-->
<bean id="userService"
class="org.bookstore.service.impl.UserService">
<property name="userDAO">
<ref bean="userDAO" />
</property>
</bean>
<bean id="catalogService"
class="org.bookstore.service.impl.CatalogService">
<property name="catalogDAO">
<ref bean="catalogDAO" />
</property>
</bean>
<bean id="bookService"
class="org.bookstore.service.impl.BookService">
<property name="bookDAO">
<ref bean="bookDAO" />
</property>
</bean>
<bean id="orderService" class="org.bookstore.service.impl.OrderService">
<property name="orderDAO">
<ref bean="orderDAO" />
</property>
</bean>
<!-- 注入Action -->
<bean id="userAction" class="org.bookstore.action.UserAction">
<property name="userService">
<ref bean="userService" />
</property>
</bean>
<bean id="bookAction"
class="org.bookstore.action.BookAction">
<property name="catalogService">
<ref bean="catalogService" />
</property>
<property name="bookService">
<ref bean="bookService" />
</property>
</bean>
<bean id="shoppingAction"
class="org.bookstore.action.ShoppingAction">
<property name="bookService">
<ref bean="bookService" />
</property>
<property name="orderService">
<ref bean="orderService"/>
</property>
</bean>
</beans>