Announcement Announcement Module
Collapse
No announcement yet.
Spring Auto-wiring. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Auto-wiring.

    Assuming i have two classes Circle and Rectangle that implement an interface called Shape.
    And i have the following class:
    Code:
    public class ObjectFactory {
        @Autowired
        @Qualifier("circle")
        Shape shape;
    .......
    .......
    .......
    }
    What i want to do is to remove the @Qualifier and make the autowiring depend on some condition (for example if condition is true then inject Circle, if the condition is false, inject the rectangle) or make the wiring depend on a naming convention.
    Is there a way to do that ?

    Let me explain, If i remove the @Qualifier annotation, i'm going to end up with the following exception:
    Code:
    org.springframework.beans.factory.NoSuchBeanDefinitionException:
    No unique bean of type [com.app.objects.Shape] is defined: 
    expected single matching bean but found 2: [circle, rectangle]
    Is there a way to handle programmatically that exception in order to have a condition dependent auto-wiring ?

  • #2
    Originally posted by M3hdi View Post
    Assuming i have two classes Circle and Rectangle that implement an interface called Shape.
    And i have the following class:
    Code:
    public class ObjectFactory {
        @Autowired
        @Qualifier("circle")
        Shape shape;
    .......
    .......
    .......
    }
    Is there a way to handle programmatically that exception in order to have a condition dependent auto-wiring ?

    I don't know about programmatically handling the exception -but you can provide a Factory method http://static.springsource.org/sprin...tml/beans.html [3.3.2.3 Instantiation using an instance factory method] that returns a shape, which could be Circle or a Rectangle.

    The other thing you could do is to not use @Autwired but set the property of the shape property of the ObjectFactory using SPEL, which would allow you to say check a system property or call a static method of a class to determine the reference for example, if the System.setProperty sets "myFavShape" (or you were to use -DmyFavShape=circle) to circle the bean with id circle would be injected. Not sure what you are trying to do...

    Code:
    <bean id="circle" class="test.Circle"/>
    <bean id="rectangle" class="test.Rectangle"/>
    <bean id="objectFactory" class="test.ObjectFactoryImpl">
    	<property name="shape" ref="#{systemProperties['myFavShape']}"/>
    </bean>

    Comment


    • #3
      thank you for the quick reply.
      i'm not allowed to use annotations or write xml configuration for each bean separately, in fact, the project i am working on is an enterprise application that is already coded an fully functional, what i'm trying to do is to integrate spring into the application and make the framework handle the instantiation of the objects. With thousands of already existing classes that need to be handled by spring (over 20000 to be precise), it's impossible to go through each class and annotate it manually, that would take like forever, that's why i'm trying to perform the wiring programmatically.
      I have already managed to make spring detect the beans without using annotations or writing the corresponding <bean>......</bean> for each class, by using the following code
      Code:
      <beans>
      
         <context:component-scan base-package="com.app">
            <context:include-filter type="assignable" expression="com.app.objects.Shape"/>
            <context:include-filter type="assignable" expression="com.app.objects.ObjectFactory"/>
         </context:component-scan>
      
      </beans>
      now what i want to do is to do is to handle the auto-wiring's conflicts programatically.
      Last edited by M3hdi; Mar 26th, 2011, 07:17 AM. Reason: formatting

      Comment

      Working...
      X