Announcement Announcement Module
Collapse
No announcement yet.
Optional Autowiring of Lists Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Optional Autowiring of Lists

    I am trying to autowire a List but I want the Autowiring to be optional.

    It is known that @Autowired does not work for List beans created through the util namespace so I am using javax.annotation.Resource to autowire the list.

    However unlike @Autowired(required=false) I could not find an option to make the javax.annotation.Resource autowiring false.

    Does anyone know how to do this?

    Example:

    This will throw an exception:
    Code:
      
    
      package com.ex;
      public class B {
         @Autowired(required=false)
         @Qualifier("aList")
         List<String> aList;
    
         public List<String> getAll() {
            return aList; 
         }
      }
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"
    	xmlns:util="http://www.springframework.org/schema/util">
    
       <context:annotation-config/>
       <util:list id="aList" value-type="java.lang.String">
            <value>val1</value>   
            <value>val2</value>  
       </util:list>   
    </beans>

    This will work but the list can't be optional:
    Code:
      
    
      package com.ex;
      public class B {
    
         @javax.annotation.Resource(name="aList")
         List<String> aList;
    
         public List<String> getAll() {
            return aList; 
         }
      }
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"
    	xmlns:util="http://www.springframework.org/schema/util">
    
       <context:annotation-config/>
       <util:list id="aList" value-type="java.lang.String">
            <value>val1</value>   
            <value>val2</value>  
       </util:list>   
    </beans>
    Last edited by howitzer; Dec 16th, 2010, 05:27 AM. Reason: Changed Option to Optionl in Title

  • #2
    same question

    did you find out?

    Comment


    • #3
      @Autowired works

      @Autowired(required=false)

      is there a non spring-specific solution?

      Comment

      Working...
      X