Spring: How do I call a method only when in web application?

kai :

I have a module with name "CacheService" which do some cache works, it has a method named "preheatCache" which build cache content when started. The "CacheService" referenced by both web application server and task application server. The web server and task serve started independently, when the task server started, I don't want "preheatCache" executed. How could I implement this?

the code in CacheService that call preheatCache automatically when start server:

@EventListener(ContextRefreshedEvent.class)
  public void afterStartup() {
    preheatCache();
  }

private preheatCache(){
    // build cache content
}

the event triggered both in web server and task server.

I trid @ConditionalOnWebApplication like this

@ConditionalOnWebApplication
private preheatCache(){
 // build cache content
}

but it seems not work

Is there some annotation or Spring's mechanism which act on a method, make this method only called in web server?

Antoniossss :

It must be a public method to allow AOP to kick in

@ConditionalOnWebApplication
public void preheatCache(){
 // build cache content
}

and must NOT be invoked from bean itself - so you must self inject that bean and use injection for invocation

@Autowired
private BeanContainingThatMethod self;

    @EventListener(ContextRefreshedEvent.class)
      public void afterStartup() {
        self.preheatCache();
      }

If I were you I would create dedicated bean for that (unless this is the one) and annotate whole bean. This way whole bean will be created and managed only if web context is present and will be skipped from creation otherwise.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I call a method when exiting?

When trying to reprogram an Applet to an Application in Java, how do I properly call the paintComponent method?

how to call the web method only when '/' is the last character from autocomplete?

How I call the ShootingSettings method from the Update only when there are changes?

Adyen: How do I verify the iDEAL payload in my callback method when an user is redirected back from iDEAL to my web application?

How can I call asynchronously a method only when other async method finishes working in Swift?

Spring @Transactional does not work when I call a method inside of it. How can I slove it?

How do I call a base class's own method from another method when it's overridden?

How do I call the procedure section from window application using asynchronous method on progress 4GL?

MVC How do I prevent calling Action Method by it's name while only allowing to call it via routing

how can I call a method only one time in a class? (first time when add "new")

Angular 2 How do I call a service method when an observable changes/when a services returns new results?

How do I call a method of an object when I have a string with the name of the object?

How do I call a method taking a union of string literals when I have a normal string?

How do I call a variable in another method

How do i call this method for debugging?

How do I call an object as method parameter?

How do I call a method by string variable?

How do I call upon the method in this program?

How do I call a method within a setString

How do I 'reset' a method on call?

How do I wrap a Web Api's async call and results as a synchronous method?

How can I call a method of the writer class when my Spring Batch Job complete?

How do i run a method on an MVC application?

I have an error when reading a method with web3 (call)

How do I call screenmanager in python file part to change screens only when all spinners are selected

How do I use a custom authentication mechanism for a Java web application with Spring Security?

How do I start a Spring Boot Web Application without using ComponentScan

How do I move my spring xml configuration outside of my web application?