简体   繁体   English

在春季获取工厂类的servletcontext

[英]Get servletcontext for factory class in spring

I want to create Factory Class. 我想创建工厂类。 for example is FooFactory. 例如FooFactory。 before Foo instanced, FooFacoty must be injected ServletContext to the constructor. 在Foo实例化之前,必须将FooFacoty注入ServletContext到构造函数。 I have snippet as follows: 我有如下片段:

public class FooFactory() {
    public static Foo getFoo() {
        ctx = //getservlet context
        Foo foo = new Foo(ctx);

            return foo;
    }
}

I came across this post on my search for how to do this very thing. 我在寻找如何执行此操作的过程中遇到了该帖子。 It helped me as a starting point, but since ServletContextFactoryBean is deprecated in Spring 3, I had to try something different. 它以帮助我为起点,但是由于ServletContextFactoryBean在Spring 3中已弃用,因此我不得不尝试一些不同的方法。

I found two options: 我发现了两个选择:

@Autowired
public AnAutowiredConstructor(WebApplicationContext webApplicationContext)
{
    servletContext = webApplicationContext.getServletContext();
}

Or you can implement org.springframework.web.context.ServletContextAware . 或者,您可以实现org.springframework.web.context.ServletContextAware

public class SomeClass implements ServletContextAware
{
    public void setServletContext(ServletContext servletContext)
    {
    }
}

EDIT: You can use ServletContextFactoryBean . 编辑:您可以使用ServletContextFactoryBean You can then pass a reference to this into your factory (eg as a method argument.). 然后,您可以将对此的引用传递到工厂中(例如,作为方法参数。)。 Like this 像这样

   <bean id="servletContext" class="org.springframework.web.context.support.ServletContextFactoryBean"/>

   <bean id="foo" class="FooFactory" factory-method="getFoo">
      <constructor-arg index="0" ref="servletContext"/>
   </bean>

You then change FooFactory.getFoo to 然后,将FooFactory.getFoo更改为

   public static Foo getFoo(ServletContext ctx) {
        Foo foo = new Foo(ctx);
        return foo;
   }

There is no direct way that I know of, but you can do it indirectly by implementing ServletContextAware or ApplicationContextAware. 我没有直接了解的方法,但是可以通过实现ServletContextAware或ApplicationContextAware间接实现。

\n

This article describes the details. 本文介绍了详细信息。

You can directly inject the instance of ServletContext which is kept by the Spring WebApplicationContext into you bean using xml: 您可以使用xml将Spring WebApplicationContext保留的ServletContext实例直接注入到bean中:

<bean id="myBean" class="foo.bar.SomeClass">
    <constructor-arg ref="servletContext"/>

Indeed the servlet context is registered in the application context as "servletContext". 实际上,servlet上下文在应用程序上下文中注册为“ servletContext”。 See http://docs.spring.io/spring/docs/4.0.x/javadoc-api/org/springframework/web/context/WebApplicationContext.html#SERVLET_CONTEXT_BEAN_NAME . 参见http://docs.spring.io/spring/docs/4.0.x/javadoc-api/org/springframework/web/context/WebApplicationContext.html#SERVLET_CONTEXT_BEAN_NAME Also works with Spring 3.x 也适用于Spring 3.x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Struts 2 工厂类中获取 ServletContext - How to get the ServletContext in Struts 2 factory classes 在Spring中使用工厂类 - Using factory class in Spring Spring-如何将ServletContext转换为@Service(+如何获取WebApps清单) - Spring - how to get ServletContext into a @Service (+ how to get WebApps Manifest) 如何使用 spring 和 hibernate 在我的 class 中获取 session 工厂 - How can get the session factory in my class using spring with hibernate Spring org.springframework.beans.factory.BeanDefinitionStoreException:IOException从ServletContext资源解析XML文档 - Spring org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource org.springframework.beans.factory.BeanDefinitionStoreException:在spring app上从ServletContext资源解析XML文档时出现意外异常 - org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource on spring app 无法分配ServletContext spring 3 - Can not assign ServletContext spring 3 Spring ServletContext 返回 null - Spring ServletContext returns null 如何从Java类(而非Servlet)获取ServletContext属性 - How to get ServletContext attribute from a java class (not servlet) 如何在简单的类文件中获取ServletContext对象? - How do i get ServletContext object in a simple class file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM