Unitils 使用指南[翻译]-和Spring的集成

本文详细介绍了如何在Unitils中与Spring框架进行集成,包括ApplicationContext配置、Springbean注入、配置测试数据库等方面,旨在提高测试性能。

 

1.1.1           Spring的集成

 

Unitils 也提供了对Spring框架的支持。Sping的一个基本远侧就是你的对象应该被设计成容易测试的,即使没有Spring或者其他容器。There are times however, when it can be useful to work with object structures that are wired by the Spring container.

Unitils支持Spring的特性如下:

  • 管理ApplicationContext配置
    注入Spring beans到单元测试
    使用Hibernate SessionFactory 配置
    Spring配置中引用Unitls 数据源
1.1.1.1     ApplicationContext配置

你只需要在类,方法或者属性上用注解@SpringApplicationContext,并且指明需要的Spring配置文件,既可以容易的装载application context 。例如:


public class UserServiceTest extends UnitilsJUnit4 {
 
    @SpringApplicationContext({"spring-config.xml", "spring-test-config.xml"})
    private ApplicationContext applicationContext;
    
}
 

这样就可以装载Spring-config.xml spring-test-config.xml文件并建立新的ApplicationContext,并将会注入对应的属性。在setter方法上也可以使用注解,而不仅仅是属性上。

父类也会被扫描,检查是否有@SpringApplicationContect注解存在。如果发现的话,这些配置文件会首先被加载。
这样就可都能通过扩展的特殊配置类覆盖父类的配置。例如:


public class UserServiceTest extends UnitilsJUnit4 {
 
    @SpringApplicationContext({"spring-config.xml", "spring-test-config.xml"})
    private ApplicationContext applicationContext;
    
}
 

在上面的例子,将即哪里一个新的ApplicationContext,首先加载spring-beans.xml,然后是extra-spring-beans.xml配置。然后application context被注入到注解的属性。

注意在这个例子里,一个新的application context被建立了。这是必须的因为有扩展的配置文件被指明了。Unitils会尽量重用application context 。在下面的例子里,没有扩展的文件被加载,因为相同的appliction context实例被重用了。


@SpringApplicationContext("spring-beans.xml")
public class BaseServiceTest extends UnitilsJUnit4 {
}
 
public class UserServiceTest extends BaseServiceTest {
 
    @SpringApplicationContext
    private ApplicationContext applicationContext;     
}
 
public class UserGroupServiceTest extends BaseServiceTest {
 
    @SpringApplicationContext
    private ApplicationContext applicationContext;     
}
 

在公共的父类BaseServiceTest中指明了配置文件,ApplicationContext会被建立一次,然后被子类重用。

既然加载一次application context的操作是比较麻烦的,那么尽量重用他对你的测试性能是有很大好处的。

1.1.1.1.1    Programmatic configuration

编程式配置也是可能的,你可以注解一个方法,这个方法没有参数或者有一个List<String>作为参数,方法会返回一个ConfigrableApplicationContext的实例。If a List<String> parameter is specified, all locations of the @SpringApplicationContext annotations that would otherwise be used to create a new instance will be passed to the method.

The result of this method should be an instance of an application context for which the refresh() method was not yet invoked. This important, since it allows Unitils to perform extra configuration such as adding some BeanPostProcessors, which is no longer possible once refresh is invoked. In case of aClassPathXmlApplicationContext this can easily be achieved by passing false as value for the refresh parameter of the constructor:


@SpringApplicationContext
public ConfigurableApplicationContext createApplicationContext(List<String> locations) {
    return new ClassPathXmlApplicationContext(Arrays.asList(locations), false);
}
 
@SpringApplicationContext
public ConfigurableApplicationContext createApplicationContext() {
    return new ClassPathXmlApplicationContext(Arrays.asList("spring-beans.xml","extra-spring-beans.xml"), false);
}
 
1.1.1.2   Injection of Spring beans

Once you've configured an ApplicationContext, Spring beans are injected into fields / setters annotated with @SpringBean@SpringBeanByType or@SpringBeanByName. Following example shows 3 ways to get a UserService bean instance from the application context:

Spring bean的注入

一旦你配置了application context,Spring bean 会注入到那些使用了@springBean@SpringBeanByType或者@SpringBeanByName的方法属性中,下面例子展现了3中从application context中去的UserService Bean的方式。


@SpringBean("userService")
private UserService userService;
 
@SpringBeanByName
private UserService userService;
 
@SpringBeanByType
private UserService userService;
 

使用@SpringBean,你会从Application Context中得到一个名字严格匹配的Bean@SpringBeanByName有相同的效果,但是现在字段的名字被用作唯一的标识。

如果使用@SpringBeanByType,则会依据字段的类型查找application context 。这种情况下,这可都能是UserService类或者他的子类。如果没有查到,或者查到多个结果,系统会抛出异常。

The same annotations can be used on setter methods. For example:


@SpringBeanByType
public void setUserService(UserService userService) {
    this.userService = userService;
}
 
1.1.1.3     配置测试数据库

在数据库测试单元我们展示怎么为你的测试获得并安装一个数据源。使用hibernate测试单元描述怎么为你的测试配置hibernate.spring中,这些通常被放在spring的配置文件。加入你的application容易使用了如下类似的文件application-config.xml:


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:hsql://localhost/sample"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>
 
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">   
    <property name="dataSource" ref="dataSource"/>
    <property name="annotatedClasses">
        <list>
            <value>org.unitils.sample.User</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.HSQLDialect
            hibernate.show_sql=true
        </value>
    </property>
</bean>
 

第一个bean定义了链接应用数据库的数据源,第二个bean配置了hibernate SessionFactory 并使用上面的数据源。

如果我们的测试里需要连接数据库,通常一个测试用的spring配置文件会像下面的内容一样:


<bean id="dataSource" class="org.unitils.database.UnitilsDataSourceFactoryBean" />
 

With this configuration, Spring will make use of the DataSource configured in Unitils. Refer to the configuration chapter for more information on how to configure a test DataSource.

Unitils automatically detects the SessionFactory configured in Spring: all integration features Unitils offers for Hibernate also work when using Spring. Thehibernate mapping test will therefore also work as follows:

对于这个配置,Srping会只用Unitils中配置的数据源。可以参考 the configuration chapter查找更多信息来查看怎么配置一个测试数据源。

Unitils自动探测Spring中配置的SessionFactoryUnitils提供的所有集成hibernate的特性也可以使用在Springhibernate mapping test 因此也可以如下配置:


@SpringApplicationContext({"application-config.xml", "test-ds-config.xml"})
public class HibernateMappingTest extends UnitilsJUnit4 {
    
    @Test
    public void testMappingToDatabase() {
        HibernateUnitils.assertMappingWithDatabaseConsistent();
    }
}
 

 

基于51单片机,实现对直流电机的调速、测速以及正反转控制。项目包含完整的仿真文件、源程序、原理图PCB设计文件,适合学习实践51单片机在电机控制方面的应用。 功能特点 调速控制:通过按键调整PWM占空比,实现电机的速度调节。 测速功能:采用霍尔传感器非接触式测速,实时显示电机转速。 正反转控制:通过按键切换电机的正转反转状态。 LCD显示:使用LCD1602液晶显示屏,显示当前的转速PWM占空比。 硬件组成 主控制器:STC89C51/52单片机(与AT89S51/52、AT89C51/52通用)。 测速传感器:霍尔传感器,用于非接触式测速。 显示模块:LCD1602液晶显示屏,显示转速占空比。 电机驱动:采用双H桥电路,控制电机的正反转调速。 软件设计 编程语言:C语言。 开发环境:Keil uVision。 仿真工具:Proteus。 使用说明 液晶屏显示: 第一行显示电机转速(单位:转/分)。 第二行显示PWM占空比(0~100%)。 按键功能: 1键:加速键,短按占空比加1,长按连续加。 2键:减速键,短按占空比减1,长按连续减。 3键:反转切换键,按下后电机反转。 4键:正转切换键,按下后电机正转。 5键:开始暂停键,按一下开始,再按一下暂停。 注意事项 磁铁霍尔元件的距离应保持在2mm左右,过近可能会在电机转动时碰到霍尔元件,过远则可能导致霍尔元件无法检测到磁铁。 资源文件 仿真文件:Proteus仿真文件,用于模拟电机控制系统的运行。 源程序:Keil uVision项目文件,包含完整的C语言源代码。 原理图:电路设计原理图,详细展示了各模块的连接方式。 PCB设计:PCB布局文件,可用于实际电路板的制作。
【四旋翼无人机】具备螺旋桨倾斜机构的全驱动四旋翼无人机:建模与控制研究(Matlab代码、Simulink仿真实现)内容概要:本文围绕具备螺旋桨倾斜机构的全驱动四旋翼无人机展开研究,重点进行了系统建模与控制策略的设计与仿真验证。通过引入螺旋桨倾斜机构,该无人机能够实现全向力矢量控制,从而具备更强的姿态调节能力六自由度全驱动特性,克服传统四旋翼欠驱动限制。研究内容涵盖动力学建模、控制系统设计(如PID、MPC等)、Matlab/Simulink环境下的仿真验证,并可能涉及轨迹跟踪、抗干扰能力及稳定性分析,旨在提升无人机在复杂环境下的机动性与控制精度。; 适合人群:具备一定控制理论基础Matlab/Simulink仿真能力的研究生、科研人员及从事无人机系统开发的工程师,尤其适合研究先进无人机控制算法的技术人员。; 使用场景及目标:①深入理解全驱动四旋翼无人机的动力学建模方法;②掌握基于Matlab/Simulink的无人机控制系统设计与仿真流程;③复现硕士论文级别的研究成果,为科研项目或学术论文提供技术支持与参考。; 阅读建议:建议结合提供的Matlab代码与Simulink模型进行实践操作,重点关注建模推导过程与控制器参数调优,同时可扩展研究不同控制算法的性能对比,以深化对全驱动系统控制机制的理解。
标题中的"EthernetIP-master.zip"压缩文档涉及工业自动化领域的以太网通信协议EtherNet/IP。该协议由罗克韦尔自动化公司基于TCP/IP技术架构开发,已广泛应用于ControlLogix系列控制设备。该压缩包内可能封装了协议实现代码、技术文档或测试工具等核心组件。 根据描述信息判断,该资源主要用于验证EtherNet/IP通信功能,可能包含测试用例、参数配置模板及故障诊断方案。标签系统通过多种拼写形式强化了协议主题标识,其中"swimo6q"字段需结合具体应用场景才能准确定义其技术含义。 从文件结构分析,该压缩包采用主分支命名规范,符合开源项目管理的基本特征。解压后预期可获取以下技术资料: 1. 项目说明文档:阐述开发目标、环境配置要求及授权条款 2. 核心算法源码:采用工业级编程语言实现的通信协议栈 3. 参数配置文件:预设网络地址、通信端口等连接参数 4. 自动化测试套件:包含协议一致性验证性能基准测试 5. 技术参考手册:详细说明API接口规范与集成方法 6. 应用示范程序:展示设备数据交换的标准流程 7. 工程构建脚本:支持跨平台编译部署流程 8. 法律声明文件:明确知识产权归属及使用限制 该测试平台可用于构建协议仿真环境,验证工业控制器与现场设备间的数据交互可靠性。在正式部署前开展此类测试,能够有效识别系统兼容性问题,提升工程实施质量。建议用户在解压文件后优先查阅许可协议,严格遵循技术文档的操作指引,同时需具备EtherNet/IP协议栈的基础知识以深入理解通信机制。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值