Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is annotation for <task:annotation-driven> in spring 4.3

I'm upgrading my app from spring 3.x to spring 4.3. And instead of xml configuration i'm want to java configuration(annotation). I unable to do configuration using annotation.

<task:executor id="executor" pool-size="8-25" queue-capacity="100" />
<task:scheduler id="scheduler" pool-size="10" />
<context:component-scan annotation-config="true" base-package="com.jobs"/> 
<task:annotation-driven executor="executor" scheduler="scheduler" />

Where and how to configure above configuration using annotation. And I want to apply above xml configuration to following MyClassName.java

<bean id="mcn" class="com.jobs.MyClassName">
    <property name="username" value="...."/>
    <property name="authorities">
        <list>
            <value>....</value>
        </list>
    </property>
 </bean>

I tried like following configuration using annotation but getting Exception:

Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Properties 'authorities' and 'username' are required for bean 'myClassName'

MyClassName.java

@Component
public class MyClassName{

    @Value("CronUser")
    private String username;

    //@Value("#{'ROLE_SYSTEM'.split(',')}")
    @Value("#{'ROLE_SYSTEM'}")
    private List<String> authorities;

    @Required
    public
    void setUsername(final String aUsername)
    {
         username = aUsername;
    }

    @Required
    public
    void setAuthorities(final List<String> aAuthorities)
    {
        authorities = aAuthorities;
    }
  }

SprinQuartzJobConfig.java

package com.config;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
@EnableAsync
@EnableScheduling
@ComponentScan({"com.jobs"})
public class SpringQuartzJobConfig {

@Bean
public Executor taskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(100);
    executor.setMaxPoolSize(8-25);
    executor.setQueueCapacity(100);
    executor.initialize();
    return executor;
}

@Bean
public Executor taskScheduler() {
    // set properties if required 
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setPoolSize(10);
    return scheduler;
}   
}

What is annotation for above xml configuration?

like image 307
Anil Jagtap Avatar asked Feb 05 '23 16:02

Anil Jagtap


1 Answers

Use @EnableScheduling and @EnableAsync respectively to replace <task:annotation-driven> scheduler and executor in the configuration class which would look something like below

@Configuration
@EnableAsync
@EnableScheduling
@ComponentScan({"com.jobs","com.my.package.second"})
public class DemoApplication {

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(100);
        executor.setMaxPoolSize(75);
        executor.setQueueCapacity(50);
        executor.initialize();
        return executor;
    }

    @Bean
    public Executor taskScheduler() {
        // set properties if required 
        return new ThreadPoolTaskScheduler();
    }   

    @Bean
    public MyClassName myClass() {
       MyClassName className = new MyClassName();
       // set properties
       return className;
    }
}

Refer docs here for more details.

EDIT (post error reported by OP)

Couple of things regarding the MyClassName

  • Replace @Configuration' &@ComponentScanwith@Component` as it should be a spring bean and not configuration.
  • Field userName does not require @Autowired as its value is supplied via @Value
  • Field authorities too does not require @Autowired. However the syntax should be corrected as @Value("#{'${ROLE_SYSTEM}'.split(',')}") if ROLE_SYSTEM is defined in properties file as ROLE_SYSTEM=foo,bar,alpha,delta
  • All occurrence of @Required should be removed as essentially all the @Autowired fields are mandatory by default unless specified otherwise via @Autowired(required = false)
like image 196
Bond - Java Bond Avatar answered Mar 07 '23 15:03

Bond - Java Bond