質問

Is it possible to get the application URL in spring scheduled job (@Scheduled annotated)? I want to create a job which sends an email with URL to specific page on the application, let's say on http://localhost:8080/appName/some/specific/url. The problem is that the part http://localhost:8080/ will be different in each environment (local,dev,production etc).

Is there any simple way to get a base URL in server-side method which is executed by spring scheduler?

役に立ちましたか?

解決

I do that with a properties file. This tutorial tells you how you can do it.

The only complex part is you need a way to change the value the properties file is referencing for each of your different environments.

他のヒント

There is no direct way to get the base url within your scheduler. You may want to look at work arounds for this, like

  1. Use a properties file to store url for each environment

  2. Have a Configurartion bean which implements ServletContextAware. This bean would be automatically notified when a web context is initialised.

    public class AppConfig implements ServletContextAware{

        private String baseUrl;
    
        public String getBaseUrl(){ 
               return baseUrl; 
        }
    
        public void setServletContext(ServletContext servletContext){
            this.baseUrl=servletContext.getRealPath("/");
        }
    }
    
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top