spring 4.3中的注释是什么

2023-12-19

我正在将我的应用程序从 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'

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


Use @EnableScheduling and @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 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-enable-annotation-support更多细节。

EDIT (OP报告的帖子错误)

关于以下几点MyClassName

  • Replace @Configuration' &@ComponentScanwith@Component` 因为它应该是一个 spring bean 而不是配置。
  • Field userName不需要@Autowired因为它的值是通过提供的@Value
  • Field authorities也不需要@Autowired。但是语法应该更正为@Value("#{'${ROLE_SYSTEM}'.split(',')}") if ROLE_SYSTEM在属性文件中定义为ROLE_SYSTEM=foo,bar,alpha,delta
  • 所有发生的@Required应该被删除,因为基本上所有的@Autowired除非通过其他方式指定,否则默认情况下字段是必填的@Autowired(required = false)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

spring 4.3中的注释是什么 的相关文章

随机推荐