2017-02-01 9 views
0

Я хочу выполнить метод каждый час, который будет выполнять мою работу.Spring 3.0 загрузить файл и установить файл @Scheduled cron

Я использую SPRING 3.0 (пожалуйста, имейте в виду) расписание cron ниже - мой код. Но он дает следующую ошибку. Ошибка: "Инициализация боба не удалось, вложенным исключением является java.lang.IllegalArgumentException: Не удалось разрешить заполнитель 'scheduling.job.cron'"

applicationContext.xml

<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:task="http://www.springframework.org/schema/task" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.0.xsd 
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util.xsd "> 

<bean 

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
<property name="locations"> 
    <list> 
     <value>classpath:test.properties</value>    
    </list> 
</property> 
<property name="ignoreUnresolvablePlaceholders" value="true"/> 

Java Класс

@Scheduled(cron = "${scheduling.job.cron}") 
public void testScheule() 
{ 
    logger.info("Schedule Call" + new Date()); 
} 

Файл свойств (который присутствует в src/main/resour CE/test.properties) содержат ниже линии

scheduling.job.cron=0 0/1 * * * ? 

Может кто-нибудь, пожалуйста, помогите мне выйти из этой ошибки и работать успешно. Спасибо заранее.

ответ

0

Убедитесь, что вы корр ectly настроен ваш application.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:task="http://www.springframework.org/schema/task" 
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd 
     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 

    <task:annotation-driven /> 
    <util:properties id="testProps" location="test.properties" /> 
    <context:property-placeholder properties-ref="testProps" /> 
    <bean id="yourBeanClassHere" class="com.howtodoinjava.service.YourBeanClassImplHere"></bean> 
</beans> 

Посмотрите на эту ссылку для лучшего понимания http://howtodoinjava.com/spring/spring-core/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/

+0

Поскольку мой test.properties находится в папке ресурсов в проекте maven, мне нужно предоставить ресурсы/тест.недвижимость в месте? –

+0

технически весна должна проверять все файлы, расположенные по папке ресурсов по умолчанию – cralfaro

+0

Я пробовал это, но он не читает файл в обоих направлениях, как resources/test.properties или просто test.properties :(какая может быть другая причина? –

0

То, чего вы пытаетесь достичь, невозможно в Spring 3.0.
Эта функция доступна, начиная с весны 3.0.1.

Даже если вы в состоянии решить свойство scheduling.job.cron из test.properties файла, вы получите ниже исключением: -

java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 1 in ${scheduling.job.cron}) 

Весной 3.0, единственно возможный способ сделать это: -

@Service (value="testService") 
public class TestService { 
// .. 

    public void testScheule() { .. } 

} 

Теперь вы можете определить в XML, как показано ниже: -

<context:component-scan base-package="com.some.package" /> 
    <task:annotation-driven /> 
    <util:properties id="applicationProps" location="test.properties" /> 

<task:scheduled-tasks> 
    <task:scheduled ref="testService " method="testScheule" cron="#{applicationProps['scheduling.job.cron']}" /> 
</task:scheduled-tasks> 
+0

Спасибо за ответ, но я дал свойство в test.properties ниже scheduling.job.cron = 0 0/1 * * *? –

+0

Обновленный ответ, пожалуйста, проверьте и дайте мне знать. –

+0

Не удалось ... получить org.springframework.beans.factory.BeanInitializationException: Не удалось загрузить свойства; Вложенное исключение - это java.io.FileNotFoundException: ресурс класса path [resource/test.properties] не может быть открыт, потому что он не существует –

Смежные вопросы