ask*_*332 2 spring spring-mvc spring-boot
这是我正在处理的WebConfig代码:
package hello.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/greeting").setViewName("greeting");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Application.class
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎是一个Spring-boot问题,在某些系统中不会调用这些类方法.相应的问题报告在:https: //github.com/spring-projects/spring-boot/issues/2870
我的问题是,我们可以将此类中映射的资源映射到此类之外作为临时解决方法吗?
如果是,我们该怎么做?
更新:按照Andy Wilkinson的建议我删除@EnableWebMvc了,演示应用程序开始工作.然后我尝试逐个删除项目文件,以查看错误消失的点.我发现我在项目中有两个类,一个是扩展的WebMvcConfigurationSupport,第二个来自WebMvcConfigurerAdapter.从项目中删除前一个类修复了错误.
我想知道的是,为什么会发生这种情况?其次,为什么这个错误不会出现在所有系统上?
问题是WebConfig在config包中并且Application在hello包中.@SpringBootApplicationon Application启用对其声明的包和该包的子包进行组件扫描.在这种情况下这意味着hello是基本包组件扫描,因此,WebConfig在config封装件从来没有发现.
为了解决这个问题,我将WebConfig进入hello包或子包中hello.config.
您对GitHub的最新更新WebConfig从扩展更改WebMvcConfigurerAdapter为扩展WebMvcConfigurationSupport.WebMvcConfigurationSupport是通过@EnableWebMvc如此注释您的类导入的类,@EnableWebMvc并且扩展WebMvcConfigurationSupport将配置两次.你应该WebMvcConfigurerAdapter像往常一样回去扩展.
| 归档时间: |
|
| 查看次数: |
15150 次 |
| 最近记录: |