Java 如何将@Scheduled与配置文件组合

Java 如何将@Scheduled与配置文件组合,java,spring,Java,Spring,我有一个Spring通过组件扫描找到的类,它有一个用@Scheduled注释的方法: @Component public class Foo { ... @Scheduled(fixedDelay = 60000) public void update() { ... 值60000可以生产,但在我的测试中,我希望它是1000 我怎样才能做到这一点?例如,我能否以某种方式将@Scheduled与概要文件结合起来?创建两个bean,一个用于生产,一个用于测试

我有一个Spring通过组件扫描找到的类,它有一个用
@Scheduled
注释的方法:

@Component
public class Foo {
    ...
    @Scheduled(fixedDelay = 60000)
    public void update() {
        ...
60000
可以生产,但在我的测试中,我希望它是
1000


我怎样才能做到这一点?例如,我能否以某种方式将
@Scheduled
与概要文件结合起来?

创建两个bean,一个用于生产,一个用于测试,并相应地用@Profile注释这两个bean,如下所示

@Bean
@Scheduled(fixedDelay = 1000)
@Profile("test")
    public void update() {
}

@Bean
@Scheduled(fixedDelay = 60000)
@Profile("dev")
    public void update() {
}
@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from "classpath:/app-config.xml"
@ContextConfiguration("/app-config.xml")
@ActiveProfiles("dev") //or switch to @ActiveProfiles("test") when testing
public class TransferServiceTest {

    @Autowired
    private TransferService transferService;

    @Test
    public void testTransferService() {
        // test the transferService
    }
}
在单元测试类中,您可以通过如下激活相关配置文件在它们之间切换

@Bean
@Scheduled(fixedDelay = 1000)
@Profile("test")
    public void update() {
}

@Bean
@Scheduled(fixedDelay = 60000)
@Profile("dev")
    public void update() {
}
@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from "classpath:/app-config.xml"
@ContextConfiguration("/app-config.xml")
@ActiveProfiles("dev") //or switch to @ActiveProfiles("test") when testing
public class TransferServiceTest {

    @Autowired
    private TransferService transferService;

    @Test
    public void testTransferService() {
        // test the transferService
    }
}

如果@ActiveProfiles(“dev”)被激活,则只会创建dev@scheduled bean,否则,如果测试配置文件被激活,则进行测试。

将延迟设置为属性:

@Component
    public class Foo {
        ...
        @Scheduled(fixedDelay = ${delay})
        public void update() {
您可以保留2个属性文件。例如dev.properties和prod.properties 弹簧将加载其中一个

<context:property-placeholder
location="classpath:${spring.profiles.active}.properties" />

我这样解决了这个问题:

<?xml version="1.0" encoding="UTF-8"?>
<beans ... xmlns:task="http://www.springframework.org/schema/task"
... xsi:schemaLocation="... http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd ...>

<!-- Everything for "default" profile, including the bean with "@Scheduled(fixedDelay = 60000)" on UpdaterTracker.update() and the "taskScheduler" bean -->
...

<!-- Activate this profile in Arquillian tests -->
<beans profile="arquillian">
    <!-- Update more frequently -->
    <bean id="updaterTracker" class="com.foo.UpdaterTracker"/>
    <task:scheduled-tasks scheduler="taskScheduler">
        <task:scheduled ref="updaterTracker" method="update" fixed-delay="1000"/>
    </task:scheduled-tasks>
</beans>

</beans>


这是个好主意,但是,它对我不起作用,因为我使用多个配置文件。因此,在生产环境中,我只使用了“default”,对于一些测试,我添加了“arquillian”配置文件。这使得解析一个名为“default,arquillian.properties”的文件失败。我认为这是可行的。然而,我不是在编写两个不同的bean,而是在寻找一个更优雅的解决方案。此外,在我的案例中,我在测试时有两个活动概要(“default”和“arquillian”),这可能会导致实例化两个bean。(可能使用多个配置文件是我的根本问题,但目前我仍无法解决。)