Is possible to get web application full url from Spring service that is invoked by task?

偶尔善良 提交于 2021-01-29 07:29:16

问题


I have service which is not invoked by user request, but by task. Now I need to send email from this task, and email contains link to my web application, how can I get full url of my application so that my email can link back to my web application?

Example of service:

@Service
public class MyService {

    @Scheduled(cron="0 */10 * * * *")
    @Transactional
    public void myTask() throws IOException, ParseException
    {
        // Now I have to send email with my app url, but how can I get it here ?
    }

}

回答1:


You cannot do it, since web application actually doesn't know its own domain name. It can only extract a domain name used to send a request from that request.

Therefore domain name of your application should be a part of configuration, and your task should obtain it from there.



来源:https://stackoverflow.com/questions/5040209/is-possible-to-get-web-application-full-url-from-spring-service-that-is-invoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!