简体   繁体   中英

Spring MVC Access Service class from a Cron Method annotated with @Scheduled

I have a working cron method but it gives me a null java.lang.NullPointerException on my service class:

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>();

            .....

Any Help is appreciated

Cron 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>

My full config 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>

The output i get is as follows:

Start :: Tue Dec 01 12:51:19 GMT 2015 TRY Tue Dec 01 12:51:19 GMT 2015 java.lang.NullPointerException

After a discussion in the chat with @Kunal, we established that the problem was with the following XML configuration:

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

The two services referred to in the code were being created in the XML config, and so needed to be set directly in this <bean> definition, so the following solution worked:

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

Posting here for others who might find this with the same issue in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM