问题
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