【发布时间】:2016-10-21 12:52:12
【问题描述】:
我有一个包含嵌入式 Tomcat 的有效 Spring 应用程序。作为一个可执行的战争,它运行良好。由于我将使用 AngularJ 作为前端,因此将 Spring 项目放在可执行 jar 中并不是很实用,因为我将在浏览器中进行调试并希望能够快速编辑 js 源文件。如果将它们保存在存档中,这很烦人。
这就是我想让它可部署的原因,所以它可以在 tomcat 上解压。这似乎很容易,但我无法让它工作。
显然上下文总是被加载两次。因为首先我得到了我使用(已经被阻止)的存储库的异常(@Repository)和 spring 的另一个异常“无法初始化上下文,因为已经存在根应用程序上下文”。
正如我已经读过的那样,我感到困惑,Spring 创建 2 个上下文是很正常的,尤其是使用 MVC。但是为什么会有例外呢?
我的 SpringApp 类看起来像这样
@EnableWebMvc
@ComponentScan
@EnableAutoConfiguration
public class LAuthServerApplication extends WebMvcConfigurerAdapter {
@Autowired
Environment env;
public static void main(String[] args) {
System.out.println("#################### Startup parameters ##############");
for (String s : args) {
System.out.println("Parameter: " + s);
}
System.out.println("######################################################");
SpringApplication.run(LAuthServerApplication.class, args);
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
为了使其可部署,我将这个类添加到我的项目中:
@Configuration
public class AppServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.comlab.comweb</groupId>
<artifactId>LAuth</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>ch.comlab.comweb</groupId>
<artifactId>Infrastructure</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources/</directory>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<name>LAuth</name>
</project>
【问题讨论】:
-
你见过Spring Boot Devtools吗?允许您在 IDE 中调试应用程序时动态调试和编辑 js 文件等文件(当未打包在 jar 中时)。无需部署在Tomcat中进行调试。
-
@Herr 你能显示你的 pom 文件吗?我已经做了一个带有战争配置的项目,要部署到 tomcat 中。我可以和你分享
-
@Mudassar 我确实试过了。它没有用,这就是我在这里问的原因
-
@Jesper 这也适用于可执行的战争吗?
标签: java spring spring-mvc tomcat