我正在将我的应用程序从Spring 3.x升级到Spring 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" />
Run Code Online (Sandbox Code Playgroud)
使用注释在何处以及如何配置以上配置。我想将上述xml配置应用于以下MyClassName.java
<bean id="mcn" class="com.jobs.MyClassName">
<property name="username" value="...."/>
<property name="authorities">
<list>
<value>....</value>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
我试过像使用注解的以下配置,但出现异常:
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Properties 'authorities' and 'username' are required for bean 'myClassName'
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
上面的xml配置的注释是什么?
使用@EnableScheduling和@EnableAsync分别替换<task:annotation-driven>配置类中的Scheduler和executor,如下所示
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅此处的文档。
编辑(由OP报告错误)
关于的几件事 MyClassName
@Configuration' &@ComponentScan with@ Component`,因为它应该是Spring bean,而不是配置。userName不需要,@Autowired因为其值通过@Valueauthorities也不需要@Autowired。然而作为语法应当纠正@Value("#{'${ROLE_SYSTEM}'.split(',')}")如果ROLE_SYSTEM在属性文件中被定义为ROLE_SYSTEM=foo,bar,alpha,delta@Required应该删除所有出现的内容,因为@Autowired默认情况下,基本上所有字段都是必填字段,除非通过另行指定@Autowired(required = false)| 归档时间: |
|
| 查看次数: |
6793 次 |
| 最近记录: |