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

I am upgrading my application from spring 3.x to spring 4.3. And instead of xml config, I want to customize java (abstract). I am unable to customize with 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 set up the configuration using annotation. And I want to apply the above xml config to the following MyClassName.java

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

      

I have tried the following configuration using annotation, but I get an 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 the annotation for the above xml configuration?

+3


source to share


1 answer


Use @EnableScheduling

and @EnableAsync

appropriately to replace the scheduler and executor <task:annotation-driven>

in the config class, which will look something like this:

@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;
    }
}

      

See the docs here for details .



EDIT (error message as reported by the OP)

Several things regarding MyClassName

  • Replace @Configuration' &

    @ComponentScan with

    @ Component` as it should be spring bean and not config.
  • The field is userName

    not required @Autowired

    as its value is provided via@Value

  • The field authorities

    does not require either @Autowired

    . However, the syntax should be corrected as @Value("#{'${ROLE_SYSTEM}'.split(',')}")

    if ROLE_SYSTEM

    defined in the properties file asROLE_SYSTEM=foo,bar,alpha,delta

  • All occurrences @Required

    should be removed as by default all fields @Autowired

    are required by default, unless otherwise specified via@Autowired(required = false)

+3


source







All Articles