在Spring MVC启动期间如何找到基本URL? [英] How do I find base URL during Spring MVC startup?

查看:85
本文介绍了在Spring MVC启动期间如何找到基本URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

潜伏在Stack Overflow的资深程序员.

fellow programmers who lurks here at Stack Overflow.

今天的问题:如何从启动中获取Spring MVC Framework中的绝对baseUrl?

Today's question: How can I get the absolute baseUrl in Spring MVC Framework, from startup?

我正在为应用程序使用Spring MVC Framework,并且处于这种情况:假设我需要将Foo类的对象插入到列表中.每个对象都包含一个唯一的自链接(我在实际应用程序中使用的是org.springframework.hateoas.Link,但这并不重要).

I'm working with Spring MVC Framework for an application, and I'm in this situation: Let's say that I need to make objects of class Foo, inserted into a list. Every object contains an unique self-link (I'm using org.springframework.hateoas.Link for the real application, but that's beside the point).

示例代码:

class Foo{
    private int ID;
    private String absoluteUrl;

    public Foo(int ID, String baseUrl){
        this.ID = ID;
        this.absoluteUrl = baseUrl + ID; 
    }

    public void setAbsoluteUrl(String baseUrl){
        this.absoluteUrl = baseUrl + this.ID;
    }
}

如果我在工厂中运行它,它可能看起来像这样:

If I run it through a factory, it could look something like this:

public List<Foo> GenerateFooList(String baseUrlFetchedBySpring){
    List<Foo> list = new ArrayList();
    for (int i = 0; i<100; i++){
    list.add(new Foo(i, baseUrlFetchedBySpring);
    return list;
}

在测试阶段,我希望得到的基本地址是" http://localhost:8000 "(或者假设是,在生产中," http://www.fooExample.com ").

Resulting baseadresses I would expect during the test phase would be "http://localhost:8000" (or hypothetically, at production, "http://www.fooExample.com").

我的问题:我需要在启动时从Spring MVC Framework获取baseUrl.

My issue: I need to get the baseUrl from Spring MVC Framework, at startup.

我正在使用的Spring MVC应用程序仅由注释配置.我发现可以使用HttpServletRequest.getRequestURL().toString()来获取绝对URL,但是据我了解,应用程序在启动后会收到 ,而我从一开始就需要baseUrl.毕竟,Spring API将HttpServletRequest描述为:定义一个对象以向Servlet提供客户端请求信息",换句话说,是在启动后从客户端 发送的请求.

The Spring MVC application I'm working with is configured by annotations only. I have found out that one can get an absolute url by using HttpServletRequest.getRequestURL().toString(), but to my understanding the application receives these after startup, while I need the baseUrl from the beginning. After all, the Spring API describes HttpServletRequest as: "Defines an object to provide client request information to a servlet", in other words a request sent from a client after startup.

我当然可以通过在代码中写入私有的最终String来添加静态baseUrl:

I could of course add a static baseUrl by writing a private final String in the code:

private final String BASE_URL = "http://www.fooExample.com/"

但是如果应用程序的基本URL随时间变化,最好由Spring MVC自动设置基本URL.假设我有一个Cache-class,它使用依赖项注入:

But in case of changes on the application's base-url over time, it would be better if the base url could be set automaticly by Spring MVC. Let's say that I have a Cache-class, that uses dependency injection:

@Repository
class FooCache{

    List<Foo> list;
    SpringObject springObject; // = ???????????

    @Autowired
    public FooCache(SpringObject springObject){
        this.springObject = springObject; // = ???????????
        initCache();
    }

    public void initCache(){
        for (int i = 0; i<100; i++){
        list.add(new Foo(i, springObject.getAbsoluteBaseUrl()); // method = ???????????
    }
}

这是我正在寻找的更多内容:缓存一开始只使用一次来自Spring的包含我所需信息的对象设置了一次.最有可能的是,它是框架的一部分,是配置类,但是在Internet上搜索了一段时间之后,我最常发现的是与HttpServletRequest相关的解决方案.

This is more of what I am looking for: The cache is only set once, at the beginning, using an object from Spring that contains the information I need. Most likely, it's a config-class that is part of the framework, but after searching for a while on the Internet, what I mostly find is HttpServletRequest-related solutions.

我真正在寻找什么Spring类/对象和方法?还是您还需要从头开始获取base_url的其他建议?

What Spring class/object and method am I truly looking for? Or what other suggestions do you have to fetch the base_url from the beginning?

我需要这个的绝对基本URL,而不是相对的.

I need the absolute base url for this one, not something relative.

推荐答案

您的应用程序没有单个基本URL".考虑一下-您可以访问生产服务器:

There is no single "base URL" for your application. Think about it - you can access your production server:

  • 通过不同的域名
  • 通过IP
  • 通过SSH对其进行访问,并通过"localhost"进行访问
  • 通过HTTP和HTTPS

如果您不想或不能反映请求的基本URL",我的建议是为每个环境配置一个规范基本URL".在属性中.但这取决于您的情况.

If you don't want to or cannot reflect the "base URL" of the request, my suggestion would be to have one "canonical base URL" configured per environment eg. in the properties. But that's up to you if it makes sense in your case.

这篇关于在Spring MVC启动期间如何找到基本URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆