Announcement Announcement Module
Collapse
No announcement yet.
Map Injection - Annotation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Map Injection - Annotation

    I am trying to autowire java.util.Map<String, String> with no success. I might be doing something silly, please help.

    JDK: 1.5
    Spring: 2.5.5
    Server: Tomcat 5.5

    Code:
    	<util:map id="mymap" value-type="java.lang.String">
    		<entry value="Upload" key="${UPLOADPATH}" />
    		<entry value="Preview Path" key="${RPTPREVIEWUPLOADPATH}" />
    	</util:map>
    Code:
    @Controller
    public class ExplorerController {
    
    	@Autowired
    	@Qualifier (value="mymap")
    	private Map<String, String> myMap;
    }
    Code:
    [2009-03-19 16:51:13,561 ERROR org.springframework.web.servlet.DispatcherServlet:290] Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'explorerController': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map com.citi.cate.gmrs.web.ExplorerController.dirMap; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency [map with value type java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=explorerDirs)}
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
    	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:402)
    	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:316)
    	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:282)
    	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:126)
    	at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
    	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:966)
    	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3956)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4230)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)

  • #2
    Try @Resource instead of @Autowired, it behaves slightly different and will allow the injection

    Comment


    • #3
      Originally posted by mirror303 View Post
      Try @Resource instead of @Autowired, it behaves slightly different and will allow the injection
      Infact @Resource didn't work first. I had to change the instance name to something else and it worked. @Autowired didn't work either way.

      Original
      Code:
      @Controller
      public class ExplorerController {
      
             @Resource
      	private Map<String, String> dirMap;
      }
      Modified
      Code:
      @Controller
      public class ExplorerController {
      
             @Resource
      	private Map<String, String> explorerDirs;
      }
      <util:map id="explorerDirs" key-type="java.lang.String"
      value-type="java.lang.String">
      <entry value="Upload" key="C:\logs" />
      </util:map>
      Is this how it is supposed to be or a bug?

      Comment


      • #4
        @Resource(name="my.resource.as.in.ctx")

        Comment


        • #5
          The map in the configuration is not qualified as "mymap". Try the following:

          Code:
          	<util:map id="mymap" value-type="java.lang.String">
          		<qualifier value="mymap"/>
          		<entry value="Upload" key="${UPLOADPATH}" />
          		<entry value="Preview Path" key="${RPTPREVIEWUPLOADPATH}" />
          	</util:map>

          Comment

          Working...
          X