s2s4h4框架整合之平台搭建(2)

本文介绍Spring4与Hibernate4集成后的优化配置方案,包括使用注解简化配置、二级缓存配置及Spring管理事务等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本次文章主要是讲Spring4和hibernate4集成优化后的配置,看起来比之前的流畅,hibernate用注解,优化后,spring的属性参数配置可以直接让hibernate.cfg.xml消失,这样的话,这个项目的参数看起来就更加简洁和易懂了。同时介绍了下hibernate二级缓存的配置方式。

为了把相关重点的标红,我就不用编码格式了。

参考相关书籍的实例,编码通过了检测的,直接拷贝修改可以使用。需要的jar包由于ssh整合很多,就不再贴图了。

Spring的配置文件:

(1)默认配置文件classpath下:applicationContext.xml(如下,分块非常明显,通俗,易懂)

<?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.xsd">

<!-- dao -->
<import resource="classpath:baobaotao-dao.xml"/>

<!-- service -->
<import resource="classpath:baobaotao-service.xml"/>

</beans>

(2)baobaotao-dao.xml 这个配置主要是封装数据来用的,所以主要是给持久层配置使用的(如hibernate等,注意开启二级缓存的配置)

<?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:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

<!-- 扫描dao组件中所有标注@Repository的dao组件 -->
<context:component-scan base-package="dao"></context:component-scan>

<!-- (1) 引入jdbc的属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- (2) 定义一个数据源 (本例采取的是c3p0数据源) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>

<!-- (3) 定义hibernate的sessionFactory(通过注解建立) -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource">
<property name="packagesToScan" value="domain" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.MySQL57InnoDBDialect</prop> -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">true</prop>

<!-- hibernate事务托管给spring来管理就不用线程本地化了处理了 -->
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->

<!-- ============================hibernate3或者是hibernate4二级缓存的配置======================================================================= -->
<!-- 开启二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>

<!-- hibernate4二级缓存的实现类 -->
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>

<!-- hibernate3二级缓存的实现类(本例是hibernate4所以注释掉,不然报错)-->
<!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> -->

<!-- 开启查询缓存 --> 
<prop key="hibernate.cache.use_query_cache">true</prop>

<prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>
<!-- ============================hibernate3或者是hibernate4二级缓存的配置======================================================================= -->
</props>
</property>
</bean>

<!-- (4) 定义hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"
p:sessionFactory-ref="sessionFactory"></bean>
</beans>

(3)baobaotao-service.xml 这个配置主要是给业务逻辑使用的,比如要进行AOP的注入等,本例中使用的是aop注入事务管理(其实使用注解的方式方式更加加单,就是<tx:annation-driven transaction-managaer="...."/>,在相应的业务逻辑类上标注@Transaction采取默认配置,再在相应业务函数上标注@Transaction并配置特有个隔离机制等,用以覆盖并修改类标志的默认级别,萝卜白菜,各有所爱,看自己的喜好来配置

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">


<!-- 扫描service报下所有标注@service组件 -->
<context:component-scan base-package="service"></context:component-scan>


<!-- 事务管理器(采用xml配置方式) hibernate事务托管交给了spring,就不用再配置threadLocal啦 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"></bean>


<!-- 使用切面表达式配置 -->
<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* service.*Service.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
<!-- 事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>

</beans>

这里还有一点像强调的是,在以前的 《s2s4h4框架整合之平台搭建(1)》中,没有配置事务管理器托管给Spring,所以我们需要hibernate中需要配置threadLocal属性供事务使用,这里托管给Spring之后,就不用配置ThreadLocal属性了,即hibernate.current_session_context_class属性不用配置了。

(4)最后来看下SpringMVC:baobaotao-servlet.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<context:component-scan base-package="web"></context:component-scan>
<!-- 启动mvc的注解功能 -->
<mvc:annotation-driven></mvc:annotation-driven>


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"></bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8"></bean>

<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/message"/>

<!-- web异常解析处理 -->
<bean id="exceptionResolver" class="web.ForumHandlerExceptionResolver">
<property name="defaultErrorView">
<value>fail</value>
</property>
<property name="exceptionMappings">
<props>
<prop key="java.lang.RuntimeException">fail</prop>
</props>
</property>
</bean>
</beans>


最后的总结,分层结构的利用,让我们的程序加合理和容易,web.xml的配置基本一致,就不在贴出了,最后还有个旧市cache的配置文件,如下。

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as
  ~ indicated by the @author tags or express copyright attribution
  ~ statements applied by the authors. All third-party contributions are
  ~ distributed under license by Red Hat Middleware LLC.
  ~
  ~ This copyrighted material is made available to anyone wishing to use, modify,
  ~ copy, or redistribute it subject to the terms and conditions of the GNU
  ~ Lesser General Public License, as published by the Free Software Foundation.
  ~
  ~ This program is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
  ~ for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public License
  ~ along with this distribution; if not, write to:
  ~ Free Software Foundation, Inc.
  ~ 51 Franklin Street, Fifth Floor
  ~ Boston, MA  02110-1301  USA
  -->
<ehcache>

    <!-- Sets the path to the directory where cache .data files are created.

         If the path is a Java System Property it is replaced by
         its value in the running VM.

         The following properties are translated:
         user.home - User's home directory
         user.dir - User's current working directory
         java.io.tmpdir - Default temp file path -->
    <diskStore path="./target/tmp"/>


    <!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.

        The following attributes are required for defaultCache:

        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

    <!--Predefined caches.  Add your cache configuration settings here.
        If you do not have a configuration for your cache a WARNING will be issued when the
        CacheManager starts

        The following attributes are required for defaultCache:

        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.
        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->

    <!-- Sample cache named sampleCache1
        This cache contains a maximum in memory of 10000 elements, and will expire
        an element if it is idle for more than 5 minutes and lives for more than
        10 minutes.

        If there are more than 10000 elements it will overflow to the
        disk cache, which in this configuration will go to wherever java.io.tmp is
        defined on your system. On a standard Linux system this will be /tmp"
        -->
    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />

    <!-- Sample cache named sampleCache2
        This cache contains 1000 elements. Elements will always be held in memory.
        They are not expired. -->
    <cache name="sampleCache2"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> 

    <!-- Place configuration for your caches following -->

</ehcache>


资源下载链接为: https://pan.quark.cn/s/9e7ef05254f8 在苹果的生态系统中,IAP(应用内购买)是苹果应用商店(App Store)中应用开发者常采用的一种盈利模式,允许用户在应用内直接购买虚拟商品或服务。苹果为开发者提供了一份详细的人民币(CNY)IAP定价表,这份定价表具有以下特点: 价格分级:定价表由多个价格等级组成,开发者可根据虚拟商品的价值选择相应等级,等级越高,价格越高。例如,低等级可能对应基础功能解锁,高等级则对应高级服务或大量虚拟道具。 税收与分成:苹果会从应用内购买金额中抽取30%作为服务费或佣金,这是苹果生态的固定规则。不过,开发者实际到手的收入会因不同国家和地区的税收政策而有所变化,但定价表中的价格等级本身是固定的,便于开发者统一管理。 多级定价策略:通过设置不同价格等级,开发者可以根据商品或服务的类型与价值进行合理定价,以满足不同消费能力的用户需求,从而最大化应用的总收入。例如,一款游戏可以通过设置不同等级的虚拟货币包,吸引不同付费意愿的玩家。 特殊等级:除了标准等级外,定价表还包含备用等级和特殊等级(如备用等级A、备用等级B等),这些等级可能是为应对特殊情况或促销活动而设置的额外价格点,为开发者提供了更灵活的定价选择。 苹果IAP定价表是开发者设计应用内购机制的重要参考。它不仅为开发者提供了标准的收入分成模型,还允许开发者根据产品特性设定价格等级,以适应市场和满足不同用户需求。同时,开发者在使用定价表时,还需严格遵守苹果的《App Store审查指南》,包括30%的分成政策、使用苹果支付接口、提供清晰的产品描述和定价信息等。苹果对应用内交易有严格规定,以确保交易的透明性和安全性。总之,苹果IAP定价表是开发者在应用内购设计中不可或缺的工具,但开发者也需密切关注苹果政策变化,以确保应用的合规运营和收益最大化。
资源下载链接为: https://pan.quark.cn/s/032795b7064d 重要更新:models 已更新为由主办方提供图片制作的图像识别板。若使用过旧版本 ucar_sim 包的仿真,需重新执行使用方法中的步骤 3。world 文件夹下的 arena-1、arena-2、arena-3 分别对应三套仿真比赛场景,图像识别板位置参考赛前会议抽取的三套摆放位置,但图像内容组合未参考比赛题库组合(详见抽取结果.pdf 及 img-folder)。 图像使用说明:仿真场地中,采用与终点地块相同图样的地块标记随机图像板和固定图像板位置,详细坐标区域信息需参考抽取结果.pdf 和 img-folder/map.png。 使用方法:将 ucar_sim 包复制到工作空间 src 目录;先执行 catkin_make 编译,再运行 source ~/.bashrc 或 devel/setup.bash;为防止启动时编码报错,需修改 python2 默认编码,解决方案:打开终端输入指令(使用 anaconda 需定位虚拟环境),找到 setencoding () 函数,将第一个 encoding 改为 "utf-8",重启电脑;将 ucar_sim 包中 models 文件夹内所有内容复制到.gazebo/models 下(.gazebo 为隐藏文件,若无 models 文件夹需自行创建),前提:未打开过 gazebo 的用户需先在终端输入 gazebo 运行一次。 运行比赛仿真时,若终端出现 “Gazebo [Err] [REST.cc:205] Error in REST request”,解决方法:打开终端,用 url: https://api.ignitionrobotics.org替换原 url: https://api.ignitionfuel.org 。 Pac
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值