Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring获取ServletContext并将其作为Bean提供_Java_Spring_Spring Mvc_Servlets_Javabeans - Fatal编程技术网

Java Spring获取ServletContext并将其作为Bean提供

Java Spring获取ServletContext并将其作为Bean提供,java,spring,spring-mvc,servlets,javabeans,Java,Spring,Spring Mvc,Servlets,Javabeans,我想在JavaSpringWebProject中获取ServletContext并使用 它需要获得web应用程序项目的绝对路径。我还是一个好孩子 JavaEE和Spring的初学者,所以可能我有些概念弄错了。 在我想要使用ServletContext的java类中,我得到了 使用@Autowired ServletContext时,仅为空对象; 但是在我的restconfiguration类中,它扩展了 WebMVCConfigureAdapter类,我得到了ServletContext,我能够

我想在JavaSpringWebProject中获取ServletContext并使用 它需要获得web应用程序项目的绝对路径。我还是一个好孩子 JavaEE和Spring的初学者,所以可能我有些概念弄错了。 在我想要使用ServletContext的java类中,我得到了 使用@Autowired ServletContext时,仅为空对象; 但是在我的restconfiguration类中,它扩展了 WebMVCConfigureAdapter类,我得到了ServletContext,我能够 在JavaBean中使用它,返回类型为ServletContext。但我 我不知道如何在另一个类中使用Bean来获取 ServletContext,这可能吗

你可以写

@Autowired
ServletContext context;
在其他bean注释类中也是如此。您将获得相同的上下文。 因此,您不需要指定:

@Bean
  public ServletContext getServletContext() {
    System.out.println("*** Context path: *** " + context.getRealPath("/"));
    return context;
  }}
例如(在注释
@ComponentScan
中指定的目录中的任何类):


感谢您的帮助,我已经解决了这个问题,将我的目标类的包添加到@ComponentScan,声明了我的目标类@Component,并插入了我以前使用的Bean。以下是生成的代码片段:

...
@ComponentScan(basePackages = { "de.rest", "de.security", "de.targetPackage" })
public class RestConfiguration extends WebMvcConfigurerAdapter {
...



@Component
public class targetClass {

  private static String absoluteServletContextPath;

  @Autowired
  ServletContext context;

  @Bean
  public ServletContext getServletContext() {
    absoluteServletContextPath = context.getRealPath("/");
    System.out.println(absoluteServletContextPath);
    return context;
  }

  @Override
  public void myMethod {

        absoluteServletContextPath = absoluteServletContextPath.replaceAll("webapp\\\\", "")
          .replaceAll("\\\\", "/");}}
@Bean
class X {
    @Autowired
    ServletContext context;

    ...
}
...
@ComponentScan(basePackages = { "de.rest", "de.security", "de.targetPackage" })
public class RestConfiguration extends WebMvcConfigurerAdapter {
...



@Component
public class targetClass {

  private static String absoluteServletContextPath;

  @Autowired
  ServletContext context;

  @Bean
  public ServletContext getServletContext() {
    absoluteServletContextPath = context.getRealPath("/");
    System.out.println(absoluteServletContextPath);
    return context;
  }

  @Override
  public void myMethod {

        absoluteServletContextPath = absoluteServletContextPath.replaceAll("webapp\\\\", "")
          .replaceAll("\\\\", "/");}}