我正在将我的应用从spring 3.x升级到4.3。而不是xml配置我想要java配置(注释)。我无法使用注释进行配置。
<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配置的注释是什么?
答案 0 :(得分:4)
分别使用@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而不是配置。userName
不需要@Autowired
,因为其值是通过@Value
authorities
也不需要@Autowired
。但是,如果@Value("#{'${ROLE_SYSTEM}'.split(',')}")
在属性文件中定义为ROLE_SYSTEM
ROLE_SYSTEM=foo,bar,alpha,delta
@Required
,因为除非通过@Autowired
@Autowired(required = false)
字段都是强制性的