gpt4 book ai didi

spring - in spring 4. 3 的注释是什么

转载 作者:行者123 更新时间:2023-12-02 20:55:47 27 4
gpt4 key购买 nike

我正在将我的应用程序从 spring 3.x 升级到 spring 4.3。我想要 java 配置(注释),而不是 xml 配置。我无法使用注释进行配置。

<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 配置的注释是什么?

最佳答案

使用@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) 另有指定,否则字段默认为必填项

关于spring - <task :annotation-driven> in spring 4. 3 的注释是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43389047/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com