If you use a ServletContainer >= 2.5 you can use the following code to get the ContextPath:
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component
@Component
public class SpringBean {
@Autowired
private ServletContext servletContext;
@PostConstruct
public void showIt() {
System.out.println(servletContext.getContextPath());
}
}