views:

36

answers:

1

I need to get ServletContextResource in class that implements InitializingBean, how is that possible? I tried with following, code but it get expcetion for that code.

Code:

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

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

}

dispatcher-servlet.xml:

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

Exception:

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)
+1  A: 

I think you are mis-using or mis-understanding @Autowired.

@Autowired means that you are telling Spring to find a bean of type ServletContextResource in your context (the dispatcher-servlet.xml file or any other context files you are importing), and wire it into the InitBean instance.

From the error message, you do not have any ServletContextResource beans in the context, thus this fails.

Are you sure you want to be autowiring a type like this? Instead, you can simply inject a Resource into the class, and have the Spring context resolve things like where the file is located on disk, etc.

It sounds like you are on this path, but I do not understand why you would both attempt to configure the bean with a constructor-arg of the file path and then use @Autowired.

matt b
how can I define ServletContextResource bean in dispatcher-servlet.xml? spring documentation has no mention of this.
newbie
well looking at the [javadoc](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/context/support/ServletContextResource.html), it would not be so simple to construct. My real question to you, why are you bothering to try to autowire this? Simply declare your constructor to take in a Resource and then Spring will handle constructing a ServletContextResource for your path, and will pass it to your constructor. It doesn't seem like you need autowiring here at all. In the code posted above, you're mixing autowiring with injecting a file path. You only need one.
matt b
How can I take resource in constructor, can you give an example?
newbie
`public InitBean(Resource resource) { this.resource = resource; }`
matt b
how can I set resource in xml? and how can I include many resurces ?
newbie