Java Spring WebプロジェクトでServletContextを取得し、 を使用してWebアプリケーションプロジェクトの絶対パスを取得します。私はまだJavaEEとSpringで 初心者ですので、多分いくつかの概念が間違っています。 ServletContextを使用したいJavaクラスでは、@Autowired ServletContextコンテキストを使用するときに が空のオブジェクトだけを取得します。 WebMvcConfigurerAdapterクラスを継承するRestConfiguartionクラスでServletContextを取得しましたが、 はServletContextの戻り値の型でJava Beanで使用することができます。しかし、私は は考えていない、どのように私は ServletContextを得るために別のクラスでBeanを使用することができます、これは可能ですか?SpringはServletContextを取得してBeanとして提供します
@Configuration
@EnableWebMvc
@Import({ ServiceConfiguration.class, SecurityConfiguration.class })
@ComponentScan(basePackages = { "de.rest", "de.security" })
public class RestConfiguration extends WebMvcConfigurerAdapter {
@Autowired
ServletContext context;
@Bean
public ServletContext getServletContext() {
System.out.println("*** Context path: *** " + context.getRealPath("/"));
return context;
}}