相关疑难解决方法(0)

在servlet中自动装配

我想在servlet中使用spring autowiring,所以这是我的代码:

@Configurable
public class ImageServlet extends HttpServlet {

   @Autowired
   private SystemPropertyDao systemPropertyDao;

   @Override
   public void init() throws ServletException {


   String imagePath = systemPropertyDao.findByID(StaticParam.CONTENT_FOLDER);

}
Run Code Online (Sandbox Code Playgroud)

SystemPropertyDao注释时@Repository

和我的applicationContext.xml:

<context:component-scan base-package="com.basepackage" />
<mvc:annotation-driven />
<context:annotation-config />
<context:spring-configured/>
Run Code Online (Sandbox Code Playgroud)

web.xml:

  <servlet>
    <servlet-name>imageServlet</servlet-name>
    <servlet-class>com.xeno.basepackage.ImageServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>imageServlet</servlet-name>
    <url-pattern>/myimages/*</url-pattern>
  </servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

有时自动装配工作,有时它不工作(对spring bean systemPropertyDao的引用为null),任何人都可以告诉我,如果我错过了什么?

spring servlets dependency-injection autowired java-ee

35
推荐指数
2
解决办法
3万
查看次数

Spring注入Servlet

所以我看到了这个问题:

Spring依赖注入到其他实例

并想知道我的方法是否会成功.

1)在Spring应用程序上下文中声明bean

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialSize" value="${jdbc.initialSize}" />
        <property name="validationQuery" value="${jdbc.validationQuery}" /> 
        <property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
    </bean>

    <bean id="apiData" class="com.mydomain.api.data.ApiData">
        <property name="dataSource" ref="dataSource" />
        <property name="apiLogger" ref="apiLogger" />
    </bean>

    <bean id="apiLogging" class="com.mydomain.api.data.ApiLogger">
        <property name="dataSource" ref="dataSource" />
    </bean>
Run Code Online (Sandbox Code Playgroud)

2)覆盖我的servlet的init方法,如下所示:

    @Override
    public void init(ServletConfig config) throws ServletException {
       super.init(config);

       ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

       this.apiData = (ApiData)ac.getBean("apiData");
       this.apiLogger = (ApiLogger)ac.getBean("apiLogger");
    }
Run Code Online (Sandbox Code Playgroud)

这个工作还是Spring还没准备好在Web应用程序部署中将bean交付给我的servlet?我是否必须做一些更传统的事情,比如把豆子放进去web.xml …

java spring servlets dependency-injection

23
推荐指数
3
解决办法
3万
查看次数

确保从非弹簧环境加载春豆

我有弹簧应用程序(球衣2.6类和servlet).

我需要从泽西/非弹簧环境中获取Spring bean(s),

类似的问题建议在上下文的静态包装中获取上下文

public static ApplicationContext getContext() {
    return context;
}
Run Code Online (Sandbox Code Playgroud)

如何确定上下文已加载或不为null?

如果我不能,我应该等待/检查,直到它加载弹簧上下文?

在从泽西上下文调用或从简单的HttpServlet代码调用bean的情况下

编辑

Jersey使用jersey-spring3依赖jar 工作正常,所以我的问题只是关于Servlets的Spring控件

编辑2

该应用程序正在加载不同于@entpnerd建议文章的弹簧

它注册了一个实现的Servlet WebApplicationInitializer

public class MyWebAppInitializer implements WebApplicationInitializer {
Run Code Online (Sandbox Code Playgroud)

但是也在DispatcherServletweb.xml中配置了

如何才能DispatcherServlet在Spring加载后加载?

因为我们在其init方法上添加了自动装配功能:

WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext())
                    .getAutowireCapableBeanFactory().autowireBean(this);
Run Code Online (Sandbox Code Playgroud)

在提供请求之前添加超时是最喜欢的解决方案还是在类加载中有一个可以处理它的调整?

编辑3

我找到了注入的答案答案,但不是为什么在Servlet之前加载Spring.

java spring servlets classloader autowired

8
推荐指数
1
解决办法
1231
查看次数