Spring配置例子

來源:互聯網
上載者:User

以下是詳解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 id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  <!-- 資料庫驅動,我這裡使用的是Mysql資料庫 -->   <property name="driverClassName">    <value>com.mysql.jdbc.Driver</value>   </property>  <!-- 資料庫地址,這裡也要注意一下編碼,不然亂碼可是很鬱悶的哦! -->   <property name="url">    <value>       jdbc:mysql://localhost:3306/tie?useUnicode=true&characterEncoding=utf-8   </value>   </property>  <!-- 資料庫的使用者名稱 -->   <property name="username">    <value>root</value>   </property>  <!-- 資料庫的密碼 -->   <property name="password">    <value>123</value>   </property></bean><!-- 把資料來源注入給Session工廠 --><bean id="sessionFactory"   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   <property name="dataSource">    <ref bean="dataSource" />   </property>  <!-- 配置對應檔 -->   <property name="mappingResources">    <list>     <value>com/alonely/vo/User.hbm.xml</value>    </list>   </property></bean><!-- 把Session工廠注入給hibernateTemplate --><!-- 解釋一下hibernateTemplate:hibernateTemplate提供了很多方便的方法,在執行時自動建立 HibernateCallback 對象,例如:load()、get()、save、delete()等方法。 --><bean id="hibernateTemplate"   class="org.springframework.orm.hibernate3.HibernateTemplate">   <constructor-arg>    <ref local="sessionFactory" />   </constructor-arg></bean><!-- 把DAO注入給Session工廠 --><bean id="userDAO" class="com.alonely.dao.UserDAO">   <property name="sessionFactory">    <ref bean="sessionFactory" />   </property></bean><!-- 把Service注入給DAO --><bean id="userService" class="com.alonely.service.UserService">   <property name="userDAO">    <ref local="userDAO" />   </property></bean><!-- 把Action注入給Service --><bean name="/user" class="com.alonely.struts.action.UserAction">   <property name="userService">    <ref bean="userService" />   </property></bean></beans>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.