Spring 4 WebApplicationInitializer未被调用

时间:2017-01-05 01:25:35

标签: spring-mvc spring-4 spring-restcontroller spring-web

这是我的代码:

public class HelloWorldInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>> ONSTARTUP <<<<<<<<<<<<<<<<<<<<<<<<");
        WebApplicationContext context = getConText();
        servletContext.addListener(new ContextLoaderListener(context));
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>> ONSTARTUP END <<<<<<<<<<<<<<<<<<<<<<<<");
    }



    private AnnotationConfigWebApplicationContext getConText() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("com.websystique.springmvc.configuration.HelloWorldConfiguration");
        return context;
    }

配置:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.websystique.springmvc")
public class HelloWorldConfiguration {


}

其他控制人员:

@RestController
public class HelloWorldRestController {

    @Autowired
    UserService userService;  //Service which will do all data retrieval/manipulation work


    //-------------------Retrieve All Users--------------------------------------------------------

    @RequestMapping(value = "/user/", method = RequestMethod.GET)
    public ResponseEntity<List<User>> listAllUsers() {
        List<User> users = userService.findAllUsers();
        if(users.isEmpty()){
            return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
        }
        return new ResponseEntity<List<User>>(users, HttpStatus.OK);
    }

JAR FILES:

spring-expression-4.2.0.RELEASE.jar"
spring-tx-4.2.0.RELEASE.jar"
spring-web-4.2.0.RELEASE.jar"
spring-webmvc-4.2.0.RELEASE.jar"
aopalliance-1.0.jar"
jackson-databind-2.5.3.jar"
spring-beans-4.2.0.RELEASE.jar"
spring-context-4.2.0.RELEASE.jar"
spring-context-support-4.2.0.RELEASE.jar"
spring-core-4.2.0.RELEASE.jar"

当我运行http://localhost:9080/MyApp/user时,它说未找到。

我有什么事忘了吗?或者我做错了?

我无法看到我在onStartUp方法中创建的sysout日志,这意味着无法看到HelloWorldInitializer,这反过来又导致我无法访问其余的Web服务方法。

1 个答案:

答案 0 :(得分:0)

使用AbstractAnnotationConfigDispatcherServletInitializer。这对我有用,所以你的代码将如下所示

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>> ONSTARTUP <<<<<<<<<<<<<<<<<<<<<<<<");
        WebApplicationContext context = getConText();
        servletContext.addListener(new ContextLoaderListener(context));
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>> ONSTARTUP END <<<<<<<<<<<<<<<<<<<<<<<<");
    }



    private AnnotationConfigWebApplicationContext getConText() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("com.websystique.springmvc.configuration.HelloWorldConfiguration");
        return context;
    }