加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

applicationContext.xml文件的具体讲解

发布时间:2020-12-16 08:59:18 所属栏目:百科 来源:网络整理
导读:以我做的一个项目为例,对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-

以我做的一个项目为例,对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="username" value="sa"></property>
<property name="password" value="123"></property>
</bean>

<!-- 定义Hibernate的SessionFactory -->
<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!--定义SessionFactory必须注入DataSource -->

<property name="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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读