问题
If I had an instance of HttpServletRequest, I could do request.getServerName() to get the server name. However, during the post initialization of the controller beans, I have no instance of HttpServletRequest.
@Controller
@RequestMapping(value = {"/data"})
public class DataController {
@PostConstruct
public void init() {
// how to get server name?
}
}
How do I get the server name in this case?
回答1:
There are no good ways to do it.
Generally speaking, server doesn't know the name that can be used to access it from the outside. Actually, HttpServletRequest.getServerName() isn't a solution either, because it returns a name used to send that particular request, not the canonical name you want your server to be known under.
The typical solution is to configure the canonical server name for a particular deployment in a property file, etc.
来源:https://stackoverflow.com/questions/8343634/get-server-name-from-postconstruct-method-in-a-spring-controller