当前位置: 首页 > 知识库问答 >
问题:

@在使用传统tomcat war部署的Spring Boot应用程序中未获取PropertySource

况经纬
2023-03-14

我在Tomcat8上部署了一个传统的SpringBoot应用程序。即使我定义了@PropertySources注释,在@PropertySources中指定的目录中给定的属性文件中定义的属性似乎找不到。我修改了一个SpringBootWeb示例项目,可以复制这个问题。在下面的示例中,调用web控制器的结果总是“Hello Default,来自Spring Boot的问候语!”。它从来没有提到过“名字”。来自外部应用程序的属性。属性(如果我从类属性文件中完全删除该属性,则部署将失败,因为该属性从未解析)。我错过了什么,以至于外部属性似乎没有被拾取?

我的应用类:

@SpringBootApplication
@Profile("dev")
@PropertySource(value = "file:/usr/local/tomcat/config/")
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder builder) {
        return builder.profiles("dev").sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

网页控制器:

@RestController
public class HelloController {

    @Value("${name}")
    private String name;

    @RequestMapping("/")
    public String index() {
        return "Hello " +name+ ", Greetings from Spring Boot!";
    }
}

src/main/资源/application.properties:

debug=true
logging.level.org.springframework.boot.context=DEBUG
logging.level.org.springframework.boot.env=DEBUG
logging.level.org.springframework.web=DEBUG
name=Default

外部应用。/usr/local/tomcat/config中的属性

name=Default

卡塔琳娜。显示属性源的out部分:

2015-06-14 22:02:11.685 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:11.688 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:11.717 DEBUG 5010 --- [ost-startStop-1] o.s.boot.SpringApplication               : Running with Spring Boot v1.2.2.RELEASE, Spring v4.1.5.RELEASE
2015-06-14 22:02:11.718 DEBUG 5010 --- [ost-startStop-1] o.s.boot.SpringApplication               : Loading source class hello.Application,class org.springframework.boot.context.web.ErrorPageFilter
2015-06-14 22:02:11.737 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application.xml' resource not found
[typical scanning for properties files]
2015-06-14 22:02:11.738 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'classpath:/application.properties' 
[continue and finish default scanning for properties files]

2015-06-14 22:02:11.738 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/application.yaml' for profile devresource not found
2015-06-14 22:02:11.742  INFO 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@31c0c0c3: startup date [Sun Jun 14 22:02:11 EDT 2015]; root of context hierarchy
2015-06-14 22:02:11.745 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Replacing [servletContextInitParams] PropertySource with [servletContextInitParams]
2015-06-14 22:02:11.745 DEBUG 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Bean factory for org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@31c0c0c3: org.springframework.beans.factory.support.DefaultListableBeanFactory@1319bea3: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,application,errorPageFilter]; root of factory hierarchy
2015-06-14 22:02:11.786 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [URL [file:/usr/local/apache-tomcat-8.0.23/config/]] PropertySource with lowest search precedence
2015-06-14 22:02:12.269  INFO 5010 --- [ost-startStop-1] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-06-14 22:02:12.500 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [applicationConfig: [classpath:/application.properties]] PropertySource with search precedence immediately lower than [applicationConfigurationProperties]
2015-06-14 22:02:12.500 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Removing [applicationConfigurationProperties] PropertySource
2015-06-14 22:02:12.500 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Removing [defaultProperties] PropertySource
2015-06-14 22:02:12.641 DEBUG 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@3fd3a17b]
2015-06-14 22:02:12.642 DEBUG 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Using ApplicationEventMulticaster [org.springframework.context.event.SimpleApplicationEventMulticaster@2c2545cb]
2015-06-14 22:02:12.644 DEBUG 5010 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2015-06-14 22:02:12.644  INFO 5010 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 902 ms
2015-06-14 22:02:12.943 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.e.ServletContextInitializerBeans : Added existing Servlet initializer bean 'dispatcherServletRegistration'; order=2147483647, resource=class path resource [org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration$DispatcherServletConfiguration.class]
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:12.984  INFO 5010 --- [ost-startStop-1] b.a.w.TomcatWebSocketContainerCustomizer : NonEmbeddedServletContainerFactory detected. Websockets support should be native so this normally is not a problem.
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.075 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.100 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]

共有1个答案

高运诚
2023-03-14

当使用传统的tomcat war部署时,我建议使用相对于类路径的文件夹路径,而不是绝对路径。

您需要定义变量spring。配置。配置Spring Boot应用程序开始时的位置

public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
        return springApplicationBuilder
                .sources(Application.class)
                .properties(getProperties());
    }

    public static void main(String[] args) {

        SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class)
                .sources(Application.class)
                .properties(getProperties())
                .run(args);
    }

   static Properties getProperties() {
      Properties props = new Properties();
      props.put("spring.config.location", "classpath:myapp1/");
      return props;
   }

我遇到了同样的问题,并在这里找到了一个解决方案:如何将Spring Boot应用程序外部化。tomcat/lib文件夹的属性

 类似资料:
  • 我试图在tomcat 6中部署一个Spring引导应用程序(战争)(当在tomcat 7和更旧的版本中部署时,我没有问题),事实上在tomcat 6(servlet 2.5)中部署是不可能使用新的方式(我会把描述新方式的链接放在底部),因为Spring Boot使用Servet 3.0 API初始化ServletContext(注册Servlet等),所以您不能在Servlet 2.5容器中开箱即

  • 完成干净的构建后,我将war文件复制到Tomcat的文件夹中。但是部署会发生两次,并且在上下文已经存在的情况下以异常结束。我错过了什么? 非常感谢您的帮助。

  • 在CRA使用网络字体有问题,在开发模式下它是这样加载的 字体已加载 但是当部署时,在生产模式下,它似乎根本没有加载。伙计们,有什么想法吗?这是我的css。

  • 要获取请求URL,可以在堆栈溢出中找到以下方法。 第一种方法: 第二种方法: 第三种方法: 我不知道在spring boot应用程序中使用哪一个来获取请求URL。 如果我使用第三种方法,那么我是否需要在配置类中创建RequestContextListener的bean,如下所示?

  • 尝试在Heroku云中部署Spring Boot应用程序,但编译java应用程序时出现错误,但在我的本地计算机中运行良好。

  • 我必须在数据库中存储的文档中搜索,这些文档中有图像,所以我使用tess4j来读取这些图像。在使用eclipse的windows中,项目可以与tess4j一起工作,如果我在windows7中的Tomcat6.35中部署应用程序,项目也可以工作,我可以正确地使用tess4j进行搜索。sous windows,1)我在tomcat lib/shared中添加了jar文件,2)tessdata和dll文件