I want to create Factory Class. for example is FooFactory. before Foo instanced, FooFacoty must be injected ServletContext to the constructor. I have snippet as follows:
public class FooFactory() {
public static Foo getFoo() {
ctx = //getservlet context
Foo foo = new Foo(ctx);
return foo;
}
}
EDIT: You can use
ServletContextFactoryBean. You can then pass a reference to this into your factory (e.g. as a method argument.). Like thisYou then change
FooFactory.getFootoThere is no direct way that I know of, but you can do it indirectly by implementing ServletContextAware or ApplicationContextAware.This article describes the details.