您的位置:首页 > 移动开发

Spring配置文件(applicationContext.xml如何配置)

2015-12-14 09:45 821 查看
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www. springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- 定义数据源Bean,使用C3P0数据源实现 (使用C3P0数据库要导入3个Jar包))-->
<!-- 设置连接数据库的驱动、URL、用户名、密码 连接池最大连接数、最小连接数、初始连接数等参数 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://localhost:3306/blog1" p:user="root"
p:password="147258"
p:maxPoolSize="60" p:minPoolSize="10"
p:initialPoolSize="1" p:maxIdleTime="1800" p:acquireIncrement="5"
p:maxStatements="0" p:idleConnectionTestPeriod="120"
p:acquireRetryAttempts="30" p:checkoutTimeout="100" />

<!-- 定义Hibernate的SessionFactory -->
<!-- 依赖注入数据源,注入正是上面定义的dataSource -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource">

<!-- 添加hibernate配置文件 -->
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"
/> -->

<!-- mappingResouces属性用来列出全部映射文件 -->
<property name="mappingResources">
<list>
<!-- 以下用来列出Hibernate映射文件,这样的话Hibernate.cfg.xml文件中的映射就没用了 -->
<value>wys/zml/pojo/Comment.hbm.xml </value>
<value>wys/zml/pojo/User.hbm.xml</value>
<value>wys/zml/pojo/Articletype.hbm.xml </value>
<value>wys/zml/pojo/ArticleInfo.hbm.xml </value>
<value>wys/zml/pojo/Friends.hbm.xml </value>
<value>wys/zml/pojo/Sdtype.hbm.xml</value>
<value>wys/zml/pojo/Replycomment.hbm.xml</value>
<value>wys/zml/pojo/Message.hbm.xml </value>
<value>wys/zml/pojo/Photoalbum.hbm.xml</value>
<value>wys/zml/pojo/Tuku.hbm.xml</value>
</list>
</property>

<!-- 定义Hibernate的SessionFactory的属性 -->
<property name="hibernateProperties">
<!-- 指定数据库方言、是否自动建表 是否生成SQL语句等 -->
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!-- 用sessionFactory.getCurrentSession必须配置这一条 -->
<prop key="hibernate.current_session_context_class">thread</prop>

</props>

</property>
</bean>
<!-- Spring的拦截器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*.action" />
<mvc:mapping path="/*.jsp" />
<bean class="wys.zml.interceptor.LoginSpringInterceptor">
</bean>
</mvc:interceptor>

</mvc:interceptors>

<!-- 管理事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 注入所有的dao -->
<bean id="ArticleDao" class="wys.zml.dao.impl.ArticleDao"
abstract="false" lazy-init="false">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="CommentDao" class="wys.zml.dao.impl.CommentDao"
abstract="false" lazy-init="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="FriendsDao" class="wys.zml.dao.impl.FriendsDao"
abstract="false" lazy-init="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="LoginDao" class="wys.zml.dao.impl.LoginDao" abstract="false"
lazy-init="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="MessageDao" class="wys.zml.dao.impl.MessageDao"
abstract="false" lazy-init="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="PhotosDao" class="wys.zml.dao.impl.PhotosDao" abstract="false"
lazy-init="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="UserDao" class="wys.zml.dao.impl.UserDao" abstract="false"
lazy-init="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<!-- 注入所有的Action -->

<bean id="ArticleAction" class="wys.zml.action.ArticleAction"
abstract="false" lazy-init="default" scope="prototype" >
<property name="articleDao">
<ref bean="ArticleDao" />
</property>
<property name="commentDao">
<ref bean="CommentDao" />
</property>
</bean>

<bean id="CommentAction" class="wys.zml.action.CommentAction"
abstract="false" lazy-init="default" scope="prototype" >
<property name="commentDao">
<ref bean="CommentDao" />
</property>
</bean>

<bean id="FriendAction" class="wys.zml.action.FriendAction"
abstract="false" lazy-init="default" scope="prototype" >
<property name="friendsDao">
<ref bean="FriendsDao" />
</property>
</bean>

<bean id="LoginAction" class="wys.zml.action.LoginAction"
abstract="false" lazy-init="default" scope="prototype" >
<property name="loginDao">
<ref bean="LoginDao" />
</property>
</bean>

<bean id="MessageAction" class="wys.zml.action.MessageAction"
abstract="false" lazy-init="default" scope="prototype" >
<property name="messageDao">
<ref bean="MessageDao" />
</property>
<property name="userDao">
<ref bean="UserDao" />
</property>
</bean>

<bean id="PhotosAlbumAction" class="wys.zml.action.PhotosAlbumAction"
abstract="false" lazy-init="default" scope="prototype" >
<property name="photosDao">
<ref bean="PhotosDao" />
</property>
</bean>

<bean id="SDtypeAction" class="wys.zml.action.SDtypeAction"
abstract="false" lazy-init="default" scope="prototype" >
<property name="articleDao">
<ref bean="ArticleDao" />
</property>
</bean>

<bean id="UserAction" class="wys.zml.action.UserAction" abstract="false"
lazy-init="default" scope="prototype" >
<property name="userDao">
<ref bean="UserDao" />
</property>
<property name="loginDao">
<ref bean="LoginDao" />
</property>
</bean>

</beans>


使用C3P0数据源连接数据库的3个jar包:http://download.csdn.net/detail/zml_2015/9330959
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: