在 spring 中创建不自动装配的 servlet 上下文
Create servlet context without autowiring in spring
我有一个单例 class,它的一个私有成员是 ServletContext 对象。
我将它设为单例以删除 spring 依赖项,因为我试图用纯 java 代码编写。
public class Utils {
private static Utils utils = null;
public Utils() {
// Exists only to defeat instantiation.
}
public synchronized static Utils getInstance() {
if (utils == null) {
utils = new Utils();
}
return utils;
}
@Autowired
private ServletContext servletContext;
public void makeUtils() {
// output csv path
String outputFile = servletContext.getRealPath("/util");
}
}
但是这里 servletContext
将为空,因为我在需要的地方手动创建 Utils
class 的对象。不是通过使用@Autowired。所以 spring 没有注入依赖。我该如何解决这个问题。
@Autowired
是一个 spring 注释。
如果您想要响应该注释而发生某些事情,那么您需要使用 spring。如果你想去掉对spring的依赖,那么就不能使用Autowired
。您需要选择一个 - 您是否依赖 spring 进行布线?
您可以遵循的一些解决方案:
使用Spring
制作 Utils
一个 spring bean 并注入它
将 Utils
放入您的 spring 上下文(bean 工厂),然后将其注入(使用 @Autowired
或其他连接策略)到每个其他需要的 class使用它。
制作 Utils
一个 spring bean 并查找它。
确实没有太多理由这样做,但如果你愿意,你可以访问你的 spring bean 工厂(可能通过 ApplicationContext
)并按类型查找 bean使用 BeanFactory.getBean(Class<T>)
使用 Spring 自动装配现有的 Utils
对象
同样,没有太多理由这样做,但如果你有一个 AutowireCapableBeanFactory
的实例(你可以通过 ApplicationContext
获得),那么你可以调用 autowireBean(Object existingBean)
来让 spring 连接你的 @Autowired
字段。
注意:我的 method/class 引用来自 Spring 3.2,因为它是我现在在 IDE 中打开的内容。如果您有不同的 spring 版本,您可能需要进行调整)
没有Spring
在 Servlet(或侦听器)
中实例化 Utils
在 Utils
上创建一个采用 ServletContext
.
的 configure
方法
在 Servlet
(或 ServletContextListener
)中调用 configure
方法来设置 Utils
上的 servletContext
字段。
存储静态全局ServletContext
创建一个像 ServletContextHolder
这样的对象,它有一个静态字段,您可以在其中存储 ServletContext
在 Servlet
(或 ServletContextListener
)中调用该持有者的 setContext
方法,设置 servletContext
字段。
在 Utils
内调用 ServletContextHolder.getContext()
我有一个单例 class,它的一个私有成员是 ServletContext 对象。 我将它设为单例以删除 spring 依赖项,因为我试图用纯 java 代码编写。
public class Utils {
private static Utils utils = null;
public Utils() {
// Exists only to defeat instantiation.
}
public synchronized static Utils getInstance() {
if (utils == null) {
utils = new Utils();
}
return utils;
}
@Autowired
private ServletContext servletContext;
public void makeUtils() {
// output csv path
String outputFile = servletContext.getRealPath("/util");
}
}
但是这里 servletContext
将为空,因为我在需要的地方手动创建 Utils
class 的对象。不是通过使用@Autowired。所以 spring 没有注入依赖。我该如何解决这个问题。
@Autowired
是一个 spring 注释。
如果您想要响应该注释而发生某些事情,那么您需要使用 spring。如果你想去掉对spring的依赖,那么就不能使用Autowired
。您需要选择一个 - 您是否依赖 spring 进行布线?
您可以遵循的一些解决方案:
使用Spring
制作 Utils
一个 spring bean 并注入它
将 Utils
放入您的 spring 上下文(bean 工厂),然后将其注入(使用 @Autowired
或其他连接策略)到每个其他需要的 class使用它。
制作 Utils
一个 spring bean 并查找它。
确实没有太多理由这样做,但如果你愿意,你可以访问你的 spring bean 工厂(可能通过 ApplicationContext
)并按类型查找 bean使用 BeanFactory.getBean(Class<T>)
使用 Spring 自动装配现有的 Utils
对象
同样,没有太多理由这样做,但如果你有一个 AutowireCapableBeanFactory
的实例(你可以通过 ApplicationContext
获得),那么你可以调用 autowireBean(Object existingBean)
来让 spring 连接你的 @Autowired
字段。
注意:我的 method/class 引用来自 Spring 3.2,因为它是我现在在 IDE 中打开的内容。如果您有不同的 spring 版本,您可能需要进行调整)
没有Spring
在 Servlet(或侦听器)
中实例化Utils
在 Utils
上创建一个采用 ServletContext
.
的 configure
方法
在 Servlet
(或 ServletContextListener
)中调用 configure
方法来设置 Utils
上的 servletContext
字段。
存储静态全局ServletContext
创建一个像 ServletContextHolder
这样的对象,它有一个静态字段,您可以在其中存储 ServletContext
在 Servlet
(或 ServletContextListener
)中调用该持有者的 setContext
方法,设置 servletContext
字段。
在 Utils
内调用 ServletContextHolder.getContext()