java - Spring MVC 从带有 @Scheduled 注释的 Cron 方法访问服务类

标签 java spring spring-mvc cron

我有一个有效的 cron 方法,但它在我的服务类上给了我一个 null java.lang.NullPointerException:

public class CronTask {



    private JpassatemposService jpassatemposService;
    @Autowired(required=true)
    @Qualifier(value="jpassatemposService")
    public void setJpassatemposService(JpassatemposService concs){
        this.jpassatemposService = concs;
    }




    @Autowired
    private ServletContext sCtx;





  //    @Scheduled(cron ="46 11 * * * ?")
    @Scheduled(fixedDelay=30000)
    public void sendCodPendentes(){

        System.out.println("Start :: "+ new Date());
        try {
            System.out.println("TRY "+ new Date());

            List<Jpassatempos> aTerminar = this.jpassatemposService.listJpassatempos24H();
            System.out.println("24 "+ new Date());
            List<Jpassatempos> enviados = this.jenviocodpendentesService.listJenviocodpendentes24H("CodPendentes24");
            System.out.println("Enviados "+ new Date());
            List<Jpassatempos> resultado = new ArrayList<Jpassatempos>();

            .....

感谢任何帮助

定时任务 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 />
    <bean id="cronTask" class="com.setelog.spring.CronTask"></bean>
</beans>

我的完整配置 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.setelog.spring" />
    <context:component-scan base-package="com.setelog.spring.cron" />

    <context:property-placeholder location="classpath:config.properties"/>

    <mvc:annotation-driven>

        <mvc:argument-resolvers>
                <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
        </mvc:argument-resolvers>

    </mvc:annotation-driven>

    <mvc:interceptors>
           <bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
    </mvc:interceptors>

    <mvc:interceptors>
        <bean class="com.setelog.spring.businessrules.Interceptor" />
    </mvc:interceptors>


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

    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="com.setelog.spring.dao.UserDetailsDaoImpl.setEmails_Blocked"/>
        <property name="arguments">
            <list>
                <value>${emails_blocked}</value>
            </list>
       </property>
    </bean>

    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="com.setelog.spring.handler.LimitLoginAuthenticationProvider.setEmails_Help"/>
        <property name="arguments">
            <list>
                <value>${emails_help}</value>
            </list>
       </property>
    </bean>


    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>


 <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

         <!-- setting maximum upload size -->
        <property name="maxUploadSize" value="10000000" />

    </bean>




    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="pt" />
        <property name="cookieName" value="myAppLocaleCookie"></property>
        <property name="cookieMaxAge" value="3600"></property>
    </bean>

    <mvc:interceptors>
        <bean
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="locale" />
        </bean>
    </mvc:interceptors>

</beans>

我得到的输出如下:

开始::2015 年 12 月 1 日星期二 12:51:19 GMT 尝试 2015 年 12 月 1 日星期二 12:51:19 GMT java.lang.NullPointerException

最佳答案

在与@Kunal 的聊天中进行讨论后,我们确定问题出在以下 XML 配置上:

<bean id="cronTask" class="com.setelog.spring.CronTask"></bean>

代码中引用的两个服务是在 XML 配置中创建的,因此需要直接在此 <bean> 中设置定义,因此以下解决方案有效:

<bean id="cronTask" class="com.setelog.spring.CronTask"> 
  <property name="jpassatemposService" ref="jpassatemposService"/> 
  <property name="jenviocodpendentesService" ref="jenviocodpendentesService"/> 
</bean>

在此发帖是为了将来可能发现此问题的其他人。

关于java - Spring MVC 从带有 @Scheduled 注释的 Cron 方法访问服务类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34019749/

相关文章:

Android Textview 中的 Java 字符串排序

java - 为什么这个数组有 14 个元素而不是 13 个?

java - 为什么我在使用 Spring Data JPA 的 Spring Boot 项目中获取此 "org.hibernate.exception.SQLGrammarException"?

java - 隐藏敏感信息作为回应

spring-mvc - 用于 'dashboard'风格UI(涉及多个域)的 Controller

java - 如何将动态列数据导出到excel

java - 如何向标准 thymeleaf 方言添加自定义属性 'th'

Java快速检查网络连接

java - java与google-app-engine通信时报错302

java - spring-integration 中的 Windows key 身份验证