How to call @PostConstruct on scoped bean request before @HandleBeforeCreate handler?

In my Spring application, I have a bean with a request scope:

@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MyBean {

    @PostConstruct
    public void init() {
       ...
    }

I also have a MongoDB event handler:

@Component
@RepositoryEventHandler
public class MyEventHandler {

    @HandleBeforeCreate
    public void beforeCreateInstance(Object instance) {
        ...
    }
 }

When I call Spring the REST data endpoint to save my resource, it starts first @HandleBeforeCreateand then it @PostConstructis called.

How can I reorder these calls? I would like to call @PostConstructon MyBeanbefore MongoDB event handlers are removed?

+4
source share
1 answer

, beans , . , MyEventHandler MyBean, MyBean , PostConstruct.

, bean, . @DependsOn. MyEventHandler :

@Component
@RepositoryEventHandler
@DependsOn("myBean")
public class MyEventHandler {

    @HandleBeforeCreate
    public void beforeCreateInstance(Object instance) {
         ...
    }
}
+1

Source: https://habr.com/ru/post/1678707/


All Articles