0

我需要在实现 InitializingBean 的类中获取 ServletContextResource,这怎么可能?我尝试了以下代码,但它对该代码产生了异常。

代码:

public class InitBean implements InitializingBean {
    @Autowired private ServletContextResource context;

    @Override
    public void afterPropertiesSet() throws Exception {
        // load file from WEB-INF folder here
    }

}

调度程序-servlet.xml:

<!-- InitBean -->
<bean id="initBean" class="InitBean">
<constructor-arg>
    <value>/WEB-INF/init.properties</value> 
</constructor-arg>

例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'initBean': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.context.support.ServletContextResource InitBean.context; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.web.context.support.ServletContextResource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.context.support.AbstractApplicationContext.initMessageSource(AbstractApplicationContext.java:755)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:413)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:548)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:224)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at com.google.appengine.tools.development.JettyContainerService.startContainer(JettyContainerService.java:185)
    at com.google.appengine.tools.development.AbstractContainerService.startup(AbstractContainerService.java:149)
    at com.google.appengine.tools.development.DevAppServerImpl.start(DevAppServerImpl.java:219)
    at com.google.appengine.tools.development.DevAppServerMain$StartAction.apply(DevAppServerMain.java:164)
    at com.google.appengine.tools.util.Parser$ParseResult.applyArgs(Parser.java:48)
    at com.google.appengine.tools.development.DevAppServerMain.<init>(DevAppServerMain.java:113)
    at com.google.appengine.tools.development.DevAppServerMain.main(DevAppServerMain.java:89)
4

1 回答 1

1

我认为你是误用或误解@Autowired

@Autowired意味着您告诉 SpringServletContextResource在您的上下文(dispatcher-servlet.xml您正在导入的文件或任何其他上下文文件)中找到一个类型的 bean,并将其连接到InitBean实例中。

从错误消息来看,上下文中没有任何ServletContextResourcebean,因此失败。

您确定要自动装配这样的类型吗?相反,您可以简单地将 aResource注入到类中,并让 Spring 上下文解析文件在磁盘上的位置等问题。

听起来你在这条路径上,但我不明白为什么你会尝试constructor-arg使用文件路径配置 bean,然后使用@Autowired.

于 2010-10-21T19:01:32.447 回答