简体   繁体   English

组件方法只在开始时调用一次

[英]Components method only called once at start

I have s Spring Component.我有 s Spring 组件。 How can I call a method only one at start and never again?我怎样才能在开始时只调用一个方法而不再调用一个方法?

I use Scheduler but I am only aware of periodically calls.我使用调度程序,但我只知道定期调用。

Sure I can set the interval very high - but maybe there is a better solution for my problem.当然,我可以将间隔设置得非常高 - 但也许有更好的解决方案来解决我的问题。

@Component
public class Test
{
    @Scheduled (fixedDelay = 100000)
    public void foo ()
    {
    }
}

There are a few ways to handle this;有几种方法可以解决这个问题; PostConstruct is the most straightforward. PostConstruct 是最直接的。

You just add a PostConstruct annotation to your method, dropping the @Scheduled annotation altogether.您只需向您的方法添加一个 PostConstruct 注释,完全删除@Scheduled注释。 Spring will execute this method after it creates the bean and is done initializing it. Spring 将在创建 bean 并完成初始化后执行此方法。

@PostConstruct
public void foo ()
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 paintComponent方法仅被调用一次 - paintComponent method is called only once 摇动方法:活动只能开始一次 - Onshake method : activity should start only once toString方法似乎只运行一次,即使它被称为多个 - toString method seems to be only working once even tho it is called multiple 当我只调用一次时,我的方法运行两次 - My method is running twice when it is only called once Spring @initBinder方法在控制器内部只能被调用一次 - Spring @initBinder method to be called only once inside a controller ThreadFactory newThread()方法仅对ThreadPoolExectutor中的每个Submit()调用一次 - ThreadFactory newThread() method called only once for each submit() call in ThreadPoolExectutor 确保在x秒内每个输入仅调用一次方法 - Ensure method is only called once per input within x seconds 使 EJB @Schedule 中的方法只被调用一次 - Make a method inside EJB @Schedule to be called only once Java,制作一次只能调用一次的方法 - Java, Make a method that can only be called once at a time 如何确保仅在给定特定参数的情况下才调用方法 - How to make sure that a method is called only once given specific parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM