Results 1 to 4 of 4

Thread: Injection based on parent class / interface

  1. #1
    Join Date
    Nov 2007
    Posts
    7

    Default Injection based on parent class / interface

    I am looking for a solution to this problem. Let's have an abstract class:

    Code:
    public abstract class Base  
    {  
      protected Service service;  
       
      public void setService(Service service)  
      {  
         this.service = service;  
      }  
    }
    And now I want all spring mannaged instances, which extends the Base class, to be automaticaly injected with the Service instance. Is it possible in Spring? And how? Thank you for your help!

  2. #2
    Join Date
    Aug 2008
    Location
    Phoenix, AZ
    Posts
    76

    Default

    Code:
    public abstract class Base  
    {  
      protected Service service;  
       
      @Autowired
      public void setService(Service service)  
      {  
         this.service = service;  
      }  
    }
    Also, in your config file, make sure you set up annotations for autowiring.

    Code:
    <context:annotation-config/>
    See the following for more:
    http://static.springframework.org/sp...otation-config

  3. #3
    Join Date
    Nov 2007
    Posts
    7

    Default

    Is there a way to achieve this functionality without annotations? I am stuck with java 1.4

  4. #4
    Join Date
    Nov 2007
    Posts
    420

    Default

    Quote Originally Posted by lfty View Post
    Is there a way to achieve this functionality without annotations? I am stuck with java 1.4
    - define abstract bean
    - define your dependencies for it
    - use parent attribute of the any class that needs to inject service

    Code:
    <bean id="abstractWithService" abstract="true">
        <property name="service" ref="service" />
    </bean>
    
    <bean id="service" class="xxx.xxx.Service">
     ...
    </bean>
    
    
    <bean id="anyClassExtendingBase" class="xxx.xxx.Service" parent="abstractWithService">
     ...
    </bean>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •