【问题标题】:Sprint boot application could not be deployed in external vm. Throws HTTP 404 when I tried loading the application无法在外部虚拟机中部署 Sprint 启动应用程序。当我尝试加载应用程序时抛出 HTTP 404
【发布时间】:2016-07-10 05:19:10
【问题描述】:

我在 tomcat 8 的外部 vm 中部署了 spring boot 应用程序。但是,当我尝试加载它时。我抛出 HTTP 404 (resource not available) 。下面是 catalina.out 文件的内容

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.controller.Application]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/application.properties]

我能做些什么来解决这个问题。我是弹簧靴的新手。请帮忙。添加application.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;



@Configuration
@ComponentScan(basePackages="com.abc")
@EnableJpaRepositories("com.abc.repository")
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
@PropertySource("application.properties")
@EntityScan("com.abc.pojo")

public class Application extends SpringBootServletInitializer {
@Autowired
private LdapDAO ldapDao;

@Autowired
private HoursService hoursService;
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);         
    }   

}

谢谢, 普尔娜。

【问题讨论】:

  • 看起来您的 application.properties 文件未包含在构建中。请检查一下。
  • Application.properties 文件已经存在于 web-inf 的 classes 文件夹中。
  • 发布你的课程com.altair.dlc.controller.Application
  • 添加了代码作为问题本身的一部分。请看一下。
  • 尝试将@SpringBootApplication 注释添加到您的班级。

标签: java spring spring-boot spring-data-rest tomcat8


【解决方案1】:

波尔娜,

基于SpringBoot指定的加载顺序和位置:

Spring Boot 按以下顺序解析“application.properties”或“application.yml”等外部配置文件:

类路径根

当前目录

类路径 /config 包

/config 当前目录的子目录。

根据这些信息,您的 application.properties 文件应该位于类路径中的 /config 目录中。所以,如果你将它放在 WEB-INF/classes 中,这将是类路径的一部分,它应该被移动到:

/WEB-INF/classes/config/application.properties.

此外,如果这不能解决您的问题,请检查@PropertySource 标签。由于您希望通过类路径加载 application.properties,因此通常将 'classpath: 添加到该位置,如下所示:

@PropertySource("classpath:application.properties")

这应该可以,但如果它没有尝试在文件名中添加“配置”路径:

@PropertySource("classpath:config/application.properties")

这里似乎也有一些相关信息:Tomcat Not reading Spring-Boot Application Properties。除了我已经描述的之外,最相关的一点是 Tomcat 可以区分大小写。

确保您的属性文件被命名为:application.propertiesnotApplication.properties

【讨论】:

  • 我也试过这个。它不工作。但是,这次异常被改变了。它能够读取 application.properties 文件。我收到以下错误:未设置“hibernate.dialect”时对 DialectResolutionInfo 的访问不能为空
  • 嗨,谢谢。我通过在 vm 中创建 db 使其工作。但是,所有控制器映射都不起作用,我只是看到静态 html pagfes。有什么想法吗?
猜你喜欢
  • 2018-09-07
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
相关资源
最近更新 更多