配置实例 – 1

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd "> <!--
这是itsalon项目spring框架的核心配置
任何spring配置文件都会导入这个配置文件
内容主要包括:
连接MS SQL Server 2005的数据源(jdbcMSSQLServerDataSource)
使用c3p0连接MS SQL Server 2005的数据源(c3p0MSSQLServerDataSource)
会话工厂(sessionFactory)
-->
<!--
使用jdbc连接MS SQL Server 2005的数据源
<bean id="jdbcMSSQLServerDataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=itsalon">
</property>
<property name="username" value="sa"></property>
<property name="password" value="sa"></property>
</bean>
-->
<!--
使用c3p0连接MS SQL Server 2005的数据源
-->
<bean id="c3p0MSSQLServerDataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="jdbcUrl"
value="jdbc:sqlserver://localhost:1433;databaseName=itsalon">
</property>
<property name="user" value="sa"></property>
<property name="password" value="abc"></property>
<property name="maxPoolSize" value="40"></property>
<property name="minPoolSize" value="1"></property>
<property name="initialPoolSize" value="1"></property>
<property name="maxIdleTime" value="20"></property>
</bean>
<!--
Hibernate数据访问会话工厂
-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="c3p0MSSQLServerDataSource" />
</property>
<property name="hibernateProperties">
<props>
<!--
以下为使用proxool数据库连接池的配置
有异常,未调试完毕
-->
<!--
<prop key="hibernate.connection.provider_class">
org.hibernate.connection.ProxoolConnectionProvider
</prop>
<prop key="hibernate.proxool.pool_alias">
dbProxool
</prop>
<prop key="hibernate.proxool.xml">
proxool-config.xml
</prop>
-->
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list> <value>net/itsalon/entity/Users.hbm.xml</value>
<value>net/itsalon/entity/City.hbm.xml</value>
<value>net/itsalon/entity/Province.hbm.xml</value>
<value>net/itsalon/entity/ManagerPower.hbm.xml</value>
<value>net/itsalon/entity/Manager.hbm.xml</value>
<value>net/itsalon/entity/WebSite.hbm.xml</value>
<value>net/itsalon/entity/BbsTopicOperation.hbm.xml</value>
<value>net/itsalon/entity/BbsComment.hbm.xml</value>
<value>net/itsalon/entity/BbsSessionType.hbm.xml</value>
<value>net/itsalon/entity/BbsUsers.hbm.xml</value>
<value>net/itsalon/entity/BbsSession.hbm.xml</value>
<value>net/itsalon/entity/BbsSessionMaster.hbm.xml</value>
<value>net/itsalon/entity/BbsCollection.hbm.xml</value>
<value>net/itsalon/entity/BbsUsersType.hbm.xml</value>
<value>net/itsalon/entity/BbsTopicType.hbm.xml</value>
<value>net/itsalon/entity/BbsTopic.hbm.xml</value>
<value>net/itsalon/entity/BbsInfo.hbm.xml</value>
<value>net/itsalon/entity/BbsGrade.hbm.xml</value>
</list>
</property>
</bean> <!-- 事务管理 -->
<bean id="hibTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="tranAdvice"
transaction-manager="hibTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethods"
expression="execution(* net.itsalon.*.service.*.*(..))" />
<aop:advisor advice-ref="tranAdvice"
pointcut-ref="serviceMethods" />
</aop:config> <!-- 导入用户管理模块bean -->
<import resource="beans-manager.xml"/>
<!-- 导入论坛模块bean -->
<import resource="beans-bbs.xml"/>
</beans>

实例配置 - 2

<?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.5.xsd"> <description>Spring公共配置文件</description> <!-- 导入bean文件 -->
<import resource="classpath*:config/applicationContext-*.xml" /> <!-- 定义受环境影响易变的变量 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName"
value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<!-- 本地开发环境配置 -->
<value>classpath*:config/datasource.properties</value>
</list>
</property>
</bean> <!-- 数据源配置 -->
<bean id="maindataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 连接池启动时的初始值 -->
<property name="initialPoolSize"
value="${jdbc.connectionPool.initialPoolSize}" />
<!-- 连接池的最大值 -->
<property name="maxPoolSize"
value="${jdbc.connectionPool.maxPoolSize}" />
<!-- 连接池的最小值 -->
<property name="minPoolSize"
value="${jdbc.connectionPool.minPoolSize}" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdleTime"
value="${jdbc.connectionPool.maxIdleTime}" />
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请
<property name="minIdleTime"
value="${jdbc.connectionPool.minIdleTime}" />-->
</bean> <bean id="dataSource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource">
<ref local="maindataSource" />
</property>
</bean> <!-- sessionFactory配置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- hibernate默认的策略,其中包含了把列名的大写自动变成小写并加上下划线
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>-->
<!-- 实体映射文件 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/test/demo/model/maps</value>
</list>
</property>
<!-- 数据库属性配置 -->
<property name="hibernateProperties">
<props>
<!-- SQL方言,这边设定的是MySQL -->
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!-- 为true表示将Hibernate发送给数据库的sql显示出来 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 格式化输出sql语句 -->
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<!-- 一次读的数据库记录数 -->
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<!-- 开启二级缓存 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
</props>
</property> </bean> <!-- 事务管理器配置 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>

实例配置 - 3

<!-- 头文件,主要注意一下编码 -->
<?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:context ="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > <!-- 建立数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库驱动,我这里使用的是Mysql数据库 -->
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url">
<value>
<!-- 数据库名称,注意编码 -->
jdbc:mysql://localhost:3306/test0920?useUnicode=true&amp;characterEncoding=utf8
</value>
</property>
<!-- 数据库登录用户名&密码 -->
<property name="username" value="root"></property>
<property name="password" value=""></property> </bean> <!-- 把数据源注入给Session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property> <!-- 把Session工厂注入给hibernateTemplate -->
<!-- hibernateTemplate提供很多方便的方法,执行时自动建立 HibernateCallback 对象,如:load()、save等。 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property> <!-- 配置映射文件 -->
<property name="mappingResources">
<list>
<!-- *.hbm.xml 在这里配置 -->
<value>com/blank/pojo/User.hbm.xml</value>
</list>
</property>
</bean> <!-- 声明式事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 配置事务规则 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 事务要处理的类的路径 -->
<aop:pointcut id="interceptorPointCuts"
expression="execution(* com.blank.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config> <!-- DAO -->
<!-- class记得要写正确 -->
<bean id="IBaseDAO" class="com.blank.dao.impl.IBaseDAO" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="UserDAO" class="com.blank.dao.impl.UserDAOImpl"
parent="IBaseDAO">
</bean> <!-- Service -->
<bean id="UserServiceImpl" class="com.blank.service.impl.UserServiceImpl">
<property name="userDAO">
<ref bean="UserDAO" />
</property>
</bean> <!-- Action -->
<!-- struts使用这里的id做为class -->
<bean id="UserAction" class="com.blank.action.UserAction" scope="prototype">
<property name="userService">
<ref bean="UserServiceImpl"/>
</property>
</bean> </beans>

web.xml中classpath:和classpath*:  有什么区别?  
 
classpath:只会到你的class路径中查找找文件;  
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找. 
 
  
 
存放位置: 
1:src下面 
需要在web.xml中定义如下:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
< /context-param>

2:WEB-INF下面 
需要在web.xml中定义如下:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext*.xml</param-value>
< /context-param>

web.xml 通过contextConfigLocation配置spring 的方式  
SSI框架配置文件路径问题:  
 
struts2的 1个+N个 路径:src+src(可配置) 名称: struts.xml + N  
spring 的 1个 路径: src 名称: applicationContext.xml 
ibatis 的 1个+N个 路径: src+src(可配置) 名称: SqlMapConfig.xml + N  
 
 
部署到tomcat后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下  
 
spring的 配置文件在启动时,加载的是web-info目录下的applicationContext.xml,  
运行时使用的是web-info/classes目录下的applicationContext.xml。  
 
配置web.xml使这2个路径一致:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
< /context-param>

多个配置文件的加载

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:conf/spring/applicationContext_core*.xml,
classpath*:conf/spring/applicationContext_dict*.xml,
classpath*:conf/spring/applicationContext_hibernate.xml,
classpath*:conf/spring/applicationContext_staff*.xml,
classpath*:conf/spring/applicationContext_security.xml
classpath*:conf/spring/applicationContext_modules*.xml
classpath*:conf/spring/applicationContext_cti*.xml
classpath*:conf/spring/applicationContext_apm*.xml
</param-value>
</context-param>

contextConfigLocation 参数定义了要装入的 Spring 配置文件。  
 
首先与Spring相关的配置文件必须要以"applicationContext-"开头,要符合约定优于配置的思想,这样在效率上和出错率上都要好很多。  
还有最好把所有Spring配置文件都放在一个统一的目录下,如果项目大了还可以在该目录下分模块建目录。这样程序看起来不会很乱。  
在web.xml中的配置如下:  
Xml代码

<context-param>
< param-name>contextConfigLocation</param-name>
< param-value>classpath*:**/applicationContext-*.xml</param-value>
< /context-param>

"**/"表示的是任意目录;  
"**/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。  
你自己可以根据需要修改。最好把所有Spring配置文件都放在一个统一的目录下,如:  
 
<!-- Spring 的配置 -->

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/applicationContext-*.xml</param-value>
< /context-param>

[JavaEE] applicationContext.xml配置文件使用合集的更多相关文章

  1. Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

    一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...

  2. 在web.xml注册applicationContext.xml配置文件

    概要: Spring配置文件是集成了Spring框架的项目的核心,引擎的开始是:容器先是加载web.xml,接着是applicationContext.xml在web.xml里的注册.以下我们将介绍a ...

  3. applicationContext.xml 配置文件的存放位置

    eb.xml中classpath:和classpath*:  有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件中 ...

  4. 模拟Spring中applicationContext.xml配置文件初始化bean的过程

    package com.xiaohao.action; import java.io.File; import java.lang.reflect.Method; import java.util.C ...

  5. spring applicationContext.xml 配置文件 详解

      <?xml version="1.0" encoding="UTF-8"?>   <beans xmlns="http://ww ...

  6. 关于spring的applicationContext.xml配置文件的ref和value之自我想法

    今天在做SSH的一个项目的时候,因为需要定时操作,所以就再sping里面加入了一个quartz的小定时框架,结果在运行时候,发生了一个小bug. Caused by: org.springframew ...

  7. STS中applicationContext.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  8. Spring 框架的 applicationContext.xml 配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. zookeeper中controller项目中资源配置文件applicationContext.xml配置文件编写

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

随机推荐

  1. Linux下的paste合并命令详解

    paste单词意思是粘贴.该命令主要用来将多个文件的内容合并,与cut命令完成的功能刚好相反. 粘贴两个不同来源的数据时,首先需将其分类,并确保两个文件行数相同.paste将按行将不同文件行信息放在一 ...

  2. Labview二进制文件的操作

  3. LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)

    http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...

  4. unigui TUniTreeView demo

    unit untTree; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  5. 移动端页面的head头部内容

  6. java提高数据库访问效率代码优化

    package com.jb.jubmis.comm; import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQL ...

  7. SQL Server中行列转换

    典型实例 一.行转列 1.建立表格 ifobject_id('tb')isnotnulldroptabletb go createtabletb(姓名varchar(10),课程varchar(10) ...

  8. ASP.NET基础之HttpModule 、HttpContext、 HttpHandler

    http://www.cnblogs.com/wujy/p/3261141.html http://www.cnblogs.com/wujy/p/3264475.html http://www.cnb ...

  9. yum update Transaction Check Error

    update系统时,发现其中一台server居然提示: Transaction Check Error:file /usr/lib/perl5/5.8.8/CGI.pm from install of ...

  10. 高性能的JavaScript -- 读书笔记

    高性能的JavaScript 一.      加载和运行 将脚本放在底部 脚本下载解析执行时,页面已经加载完成并显示在用户面前 成组脚本 减少外部脚本文件数量,整合成一个文件 延迟脚本 动态脚本元素 ...