无法注册我的自定义AbstractAnnotationConfigDispatcherServletInitializer

时间:2018-03-04 18:13:03

标签: java spring spring-mvc spring-boot

我正在尝试使用嵌入式tomcat创建一个非常简单的spring-mvc应用程序。我认为AbstractAnnotationConfigDispatcherServletInitializer是自动注册的,但我读过一些相反的事情。所以,我尝试将Main类标记为@Configuration并添加一个函数来检索该类型的Bean。

我希望从MEB-INF / views和" .jsp"获得意见。后缀而不是模板目录和" .html"后缀。我还想改变应用程序上下文。

@SpringBootApplication
@ComponentScan("com.boraji.tutorial.spring")
public class Main {
    public static void main(String[] args){
        SpringApplication.run(Main.class, args);
    }
}


//second Main option I mentioned
@SpringBootApplication
@ComponentScan("com.boraji.tutorial.spring")
@Configuration
public class Main {
    public static void main(String[] args){
        SpringApplication.run(Main.class, args);
    }

    @Bean
    AbstractAnnotationConfigDispatcherServletInitializer webAppInitializer(){
        return new MyWebAppInitializer();
    }
}

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[0];
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

@Configuration
public class RootConfig {
}

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter{
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

@Controller
public class HelloWorldController {
    @RequestMapping(path={"/"},method= RequestMethod.GET)
    public String sayHello(Model model) {
        model.addAttribute("message","Hello Spring MVC!");

        //Java 8 LocalDate
        DateTimeFormatter formatter =    DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        LocalDate date = LocalDate.now();
        model.addAttribute("date", date.format(formatter));

        return "index";
    }
}

The view directory layout

application.properties为空。它就在那里,因为我正在尝试各种配置。

启动跟踪

 :: Spring Boot ::       (v1.5.10.RELEASE)
2018-03-04 19:43:47.211  INFO 1987 --- [           main] com.boraji.tutorial.spring.Main          : Starting Main v1.0-SNAPSHOT on javier-N56VZ with PID 1987 (/home/javier/test_src/springtest2/target/spring-test-2-1.0-SNAPSHOT.jar started by javier in /home/javier/test_src/springtest2)

2018-03-04 19:43:47.215  INFO 1987 --- [           main] com.boraji.tutorial.spring.Main          : No active profile set, falling back to default profiles: default

2018-03-04 19:43:47.549  INFO 1987 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@30946e09: startup date [Sun Mar 04 19:43:47 CET 2018]; root of context hierarchy

2018-03-04 19:43:48.880  INFO 1987 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)

2018-03-04 19:43:48.892  INFO 1987 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]

2018-03-04 19:43:48.893  INFO 1987 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.27

2018-03-04 19:43:48.974  INFO 1987 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext

2018-03-04 19:43:48.974  INFO 1987 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1429 ms

2018-03-04 19:43:49.055  INFO 1987 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]

2018-03-04 19:43:49.059  INFO 1987 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]

2018-03-04 19:43:49.179  INFO 1987 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String com.boraji.tutorial.spring.controllers.HelloWorldController.sayHello(org.springframework.ui.Model)

2018-03-04 19:43:49.180  INFO 1987 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/bye]}" onto public java.lang.String com.boraji.tutorial.spring.controllers.ByeController.bye()

2018-03-04 19:43:49.188  INFO 1987 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

2018-03-04 19:43:49.188  INFO 1987 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)

2018-03-04 19:43:49.218  INFO 1987 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]

2018-03-04 19:43:49.256  INFO 1987 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@30946e09: startup date [Sun Mar 04 19:43:47 CET 2018]; root of context hierarchy

2018-03-04 19:43:50.113  INFO 1987 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup

2018-03-04 19:43:50.180  INFO 1987 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)

2018-03-04 19:43:50.186  INFO 1987 --- [           main] com.boraji.tutorial.spring.Main          : Started Main in 3.308 seconds (JVM running for 3.697)

=== POM ===

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
</parent>

<dependencies>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.7.RELEASE</version>
</dependency>


<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.14.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!-- JSTL Dependency-->
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>javax.servlet.jsp.jstl-api</artifactId>
    <version>1.2.1</version>
</dependency>

<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

<!-- Servlet Dependency -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

<!-- JSP Dependency -->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
    <scope>provided</scope>
</dependency>

</dependencies>
<build>
<plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
</plugins>
</build>

2 个答案:

答案 0 :(得分:0)

尝试在资源下移动application.properties,但不在WEB-INF下移动。此外,有错误堆栈跟踪会很好。

答案 1 :(得分:0)

首先,我强烈建议将JSP用作视图层,但类似Thymeleaf。在Spring Boot中使用JSP时,必须考虑certain limitations

据说你正在使事情变得过于复杂。使它工作。

  1. 删除RootConfig
  2. 删除WebConfig
  3. 删除MyWebAppInitializer
  4. @ComponentScan
  5. 中删除@ConfigurationMain
  6. 将您的Main课程移至com.boraji.tutorial
  7. org.springframework中删除普通pom.xml依赖项,然后使用spring-boot-starter-web代替
  8. 删除maven-compiler-plugin
  9. spring-boot-starter-tomcat依赖项添加为provided
  10. spring.mvc.view.prefixspring.mvc.view.suffix添加到application.properties
  11. 基本上,这会将您的Main课程缩减为类似的内容。

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

    你的依赖关系是这样的

    <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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    
        <!-- JSTL Dependency-->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.1</version>
        </dependency>
    
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
    
        <!-- JSP Dependency -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
    
    </dependencies>
    

    还要确保使用<packaging>war</packaging>作为pom的类型,而不是maven-compiler-plugin使用<properties>部分来设置java版本。

    <properties>
        <java.version>1.8</java.version>
    </properties>
    

    最后将suffixprefix添加到您的application.properties

    spring.mvc.view.prefix=/WEB-INF/views/
    spring.mvc.view.suffix=.jsp
    

    现在,这也只会使您的应用程序成为JSP(尽管您的依赖项列表中仍然存在Thymeleaf依赖项)。

    要获得更多灵感(或缺少依赖项),您可能需要查看Spring Boot中的JSP Sample