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";
}
}
System.out.println(ContextLoader.getCurrentWebApplicationContext());
输出为null
请问,哪里有问题?
Spring Boot
另外推荐使用ApplicationContextAware的方式获取ApplicationContext,这样对非web及web环境都有很好的支持,我的工程这样写的:
我也想知道答案