2017-04-13 47 views
2

我正在将我的应用从spring 3.x升级到spring 4.3。而不是xml配置我想要java配置(注释)。我无法使用注释进行配置。春季4.3什么是<task:annotation-driven>的注释4.3

<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" /> 

何处以及如何使用注释来配置上述配置。 我想上面的XML配置适用于以下MyClassName.java

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

我试着像下面的配置使用注释,但得到的异常:

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

以上xml配置的注释是什么?

回答

2

使用@EnableScheduling@EnableAsync分别替换配置类<task:annotation-driven>调度和执行这将类似于下面

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

参考文档here了解更多详情。

编辑(由OP报道后错误)

夫妇的关于MyClassName

  • 东西替换@Configuration' & @ComponentScan with @ Component`因为它应该是一个Spring bean,而不是配置。作为它的值是通过@Value
  • 领域authorities供给太不需要@Autowired
  • 领域userName不需要@Autowired。然而,语法应为@Value("#{'${ROLE_SYSTEM}'.split(',')}")如果ROLE_SYSTEM在性能定义进行修正文件作为ROLE_SYSTEM=foo,bar,alpha,delta
  • @Required所有的发生,应为本质,除非通过@Autowired(required = false)
+0

指定,否则所有的@Autowired字段默认强制移除感谢您响应。但是,我需要为

+0

看到编辑 - “@ ComponenetScan”需要添加配置类,它支持多包扫描以及 –

+0

意味着我需要创建单独的配置类作为DemoApplication类? –