springboot中,ContextLoader.getCurrentWebApplicationContext()获取的为Null

白云山中 发布于 2016/07/22 15:18
阅读 23K+
收藏 1

springboot版本:1.3.6.RELEASE

package com.pp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class App extends SpringBootServletInitializer {
	
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(App.class);
	}
	
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}



package com.pp;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.ContextLoader;

@RestController
public class HomeController {

	@RequestMapping("/home")
	public String home(){
		System.out.println(ContextLoader.getCurrentWebApplicationContext());
		return "home";
	}
}



启动main方法之后(内嵌tomcat8),

System.out.println(ContextLoader.getCurrentWebApplicationContext());

输出为null

请问,哪里有问题?

加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部