首页 > 代码库 > [JavaEE] applicationContext.xml配置文件使用合集

[JavaEE] applicationContext.xml配置文件使用合集

配置实例 – 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="http://www.mamicode.com/com.microsoft.sqlserver.jdbc.SQLServerDriver">  </property>  <property name="url"  value="http://www.mamicode.com/jdbc:sqlserver://localhost:1433;databaseName=itsalon">  </property>  <property name="username" value="http://www.mamicode.com/sa"></property>  <property name="password" value="http://www.mamicode.com/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="http://www.mamicode.com/${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配置文件使用合集