配置实例 – 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. MySQL 查看表结构简单命令。

    一.简单描述表结构,字段类型 desc tabl_name; 显示表结构,字段类型,主键,是否为空等属性,但不显示外键. 二.查询表中列的注释信息 select * from information_ ...

  2. 理解C#值类型和引用类型

    网上偶尔浏览到这一篇文章,还不错就修改了下分享给大家. 工作许久了,可是对C#值类型和C#引用类型却一直无法很好的理解.这两天花了不少时间查找资料,看文章,终于有所收获,在此将自己理解整理出来,方便日 ...

  3. 编译ffmpeg(iOS)

    一,x264库的编译 首先到http://www.videolan.org/developers/x264.html下载x264的库,然后解压,修改文件夹名称为x264     二,下载ffmpeg2 ...

  4. spring+jpg环境下,spring实现文件上传

    jsp: <form method="post" action="excel.do?method=inputExcel" enctype="mu ...

  5. Android 图标上面添加提醒使用开源UI类库 Viewbadger

    Viewbadger 1.BadgeView主要是继承了TextView,所以实际上就是一个TextView,底层放了一个label,可以自定义背景图,自定义背景颜色,是否显示,显示进入的动画效果以及 ...

  6. ?this&函数自身的引用

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Window.navigator

    定义和用法 userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值. 一般来讲,它是在 navigator.appCodeName 的值之后加上斜线和 navig ...

  8. java画图输出到磁盘

    直奔主题,实战例子如下 package com.yuanmeng.jase; import java.awt.Color; import java.awt.Font; import java.awt. ...

  9. 安装Exchange2010

    1.exadmin加入到 Schema admins,enterprise admins组中 CAS,HUB,MB安装.Net Framework CAS,HUB:2.Run 'ServerManag ...

  10. Drupal 7.31 SQL注入漏洞利用具体解释及EXP

     有意迟几天放出来这篇文章以及程序,只是看样子Drupal的这个洞没有引起多少重视,所以我也没有必要按着不发了,只是说实话这个洞威力挺大的.当然.这也是Drupal本身没有意料到的. 0x00 首 ...