Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么';将单个@Component实例转换为多个实例的最佳方法是什么?_Java_Spring - Fatal编程技术网

Java 什么';将单个@Component实例转换为多个实例的最佳方法是什么?

Java 什么';将单个@Component实例转换为多个实例的最佳方法是什么?,java,spring,Java,Spring,我有一个Spring应用程序,它创建了一个任务,该任务调度并运行一个可运行的: MAIN: @SpringBootApplication @Slf4j @Configuration @EnableEncryptableProperties @EnableJpaAuditing public class Main { @Autowired CoinListerTask coinListerTask; public static void main(String[] arg

我有一个Spring应用程序,它创建了一个任务,该任务调度并运行一个可运行的:

MAIN:

@SpringBootApplication
@Slf4j
@Configuration
@EnableEncryptableProperties
@EnableJpaAuditing
public class Main {
    @Autowired
    CoinListerTask coinListerTask;

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }


    @Component
    public class CommandLineAppStartupRunner implements CommandLineRunner {
        @Override
        public void run(String...args) throws Exception {
            ... 

            // start coin listing job           
            coinListerTask.startup();
@Component
public class CoinListerTask {
    private TimeUnit timeUnit = TimeUnit.SECONDS;
    private String threadName = "coin-lister";

    @Value("${coindatabase.coinlister.initialDelay}")
    private long initalDelay;

    @Value("${coindatabase.coinlister.period}")
    private long period;

    private String exchangeNameString = "Cryptopia";

    @Autowired
    private IDataService dataService;

    @Autowired
    CoinDbService dbService;

    public void startup() {
        ScheduledExecutorService executorService = new WrappedScheduledExecutor(threadName, 1, false);
        executorService.scheduleAtFixedRate(runnableTask, initalDelay, period, timeUnit);   

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                Shutdown.runner(executorService, threadName, 60L);
            }
        });
    }

    Runnable runnableTask = () -> {
            // do stuff...
    }
任务:

@SpringBootApplication
@Slf4j
@Configuration
@EnableEncryptableProperties
@EnableJpaAuditing
public class Main {
    @Autowired
    CoinListerTask coinListerTask;

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }


    @Component
    public class CommandLineAppStartupRunner implements CommandLineRunner {
        @Override
        public void run(String...args) throws Exception {
            ... 

            // start coin listing job           
            coinListerTask.startup();
@Component
public class CoinListerTask {
    private TimeUnit timeUnit = TimeUnit.SECONDS;
    private String threadName = "coin-lister";

    @Value("${coindatabase.coinlister.initialDelay}")
    private long initalDelay;

    @Value("${coindatabase.coinlister.period}")
    private long period;

    private String exchangeNameString = "Cryptopia";

    @Autowired
    private IDataService dataService;

    @Autowired
    CoinDbService dbService;

    public void startup() {
        ScheduledExecutorService executorService = new WrappedScheduledExecutor(threadName, 1, false);
        executorService.scheduleAtFixedRate(runnableTask, initalDelay, period, timeUnit);   

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                Shutdown.runner(executorService, threadName, 60L);
            }
        });
    }

    Runnable runnableTask = () -> {
            // do stuff...
    }
这可以正常工作,但我现在想对其进行推广,这样我就可以使用通过application.properties指定的不同参数集运行任务的多个实例,替换当前自动连接的变量

我应该删除@Component并将所有内容传递给使用new创建的实例,还是有更好、更适合spring的方法来实现这一点

解决方案

要扩展Lino和Visal的解决方案:

  • 添加到组件中: @范围(“原型”)

  • 将组件的@Autowire替换为: @自动连线 提供者coinListerTaskProvider

  • 将任务的调用更改为: coinListerTaskProvider.get().startup()


  • 如果您想在每次调用时创建新实例,可以通过添加javax/javaee api/6.0来使用提供程序。请使用范围注释和组件、服务、控制器、存储库等,如:-

    @Component
    @Scope("prototype")
    
    其中prototype定义了范围,即每次调用时总是提供一个新实例

    请参阅范围的说明

    ,也许使用
    @组件(scope=“prototype”)
    就可以解决这个问题。看见