配置实例 – 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. HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏

    Sum Problem Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  2. HD4505小Q系列故事——电梯里的爱情

    Problem Description 细心的同事发现,小Q最近喜欢乘电梯上上下下,究其原因,也许只有小Q自己知道:在电梯里经常可以遇到他心中的女神HR. 电梯其实是个很暧昧的地方,只有在电梯里,小Q ...

  3. <filter-mapping> 的 <dispatcher> 的作用

    The dispatcher has four legal values: FORWARD, REQUEST, INCLUDE, and ERROR. A value of FORWARD means ...

  4. Spring REST实践之Spring Boot

    Spring Boot基本描述 可以利用http://start.spring.io网站的进行Spring Boot的初始化构建.这个初始化构建器允许你输入工程基本信息.挑选工程支持的功能,最后会生成 ...

  5. Windows下使用NIF扩展Erlang方法

    在Erlang中,NIF(Native Implemented Function)被用来扩展erlang的某些功能,一般用来实现一些erlang很难实现的,或者一些erlang实现效率不高的功能. N ...

  6. WebService《JavaEE6权威指南 基础篇第4版》

    [Web服务] 为运行在不同平台和框架之上的软件提供了互操作的标准方式.良好的互操作性和可扩展性.消息采用自包含文档的形式. ——解决异构系统之间交互.解决异构系统通信问题:  1.通过XML,JSO ...

  7. InvocationHandler

    ====================================================================== 代理类生成之后再调用目标方法时就会调用invoke方法 p ...

  8. MVC的System.Web.Mvc.ViewPage小结

    Inherits="System.Web.Mvc.ViewPage<dynamic>这一句最好是自己手动修改,如果是维护用户数据,用户对象名是User,改成Inherits=&q ...

  9. js 原型的内存分析

    使用构造器的弊端:http://www.cnblogs.com/a757956132/p/5258897.html 示例 将行为设置为全局的行为,如果将所有的方法都设计为全局函数的时候, 这个函数就可 ...

  10. C++的优秀特性5:模版

    (转载请注明原创于潘多拉盒子) C++是强类型语言,而且恐怕是强类型语言里面类型最严格的.这意味着:1. C++变量的类型在定义时就确定了:2. 该类型在后续的生命期中不会改变.比如: int n = ...