Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8515083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:02:52+00:00 2026-06-11T05:02:52+00:00

In most cases I have a lot of components which are having the same

  • 0

In most cases I have a lot of components which are having the same classes to be injected by an OSGi Declarative Service. The services will be used to execute some logic which is the same for all derived components. Therefore to avoid duplicated code it would be the best to use abstract classes. Is there any possibility to move the DI reference methods (set/unset) to an abstract class. I’m using Bnd.

For Example:

@Component 
public class B implements IA {
   private ServiceC sc;

   @Reference
   public void setServiceC(ServiceC sc) {
      this.sc = sc;
   }  

   public void execute() {
      String result = executeSomethingDependendOnServiceC();

      // do something with result
   }

   protected String executeSomethingDependendOnServiceC() {
      // execute some logic
   }     
}

@Component 
public class D implements IA {
   private ServiceC sc;

   @Reference
   public void setServiceC(ServiceC sc) {
      this.sc = sc;
   } 

   public void execute() {
      String result = executeSomethingDependendOnServiceC();

      // do something different with result
   }

   protected String executeSomethingDependendOnServiceC() {
      // execute some logic
   }      
}

I want to move the setter for ServiceC and the method executeSomethingDependendOnServiceC() to an abstract class. But how does it look like in OSGi in connection with Bnd annotation. Just annotate the class with @Component is not working, because A and D will create different instances of the abstract class and the @Component is alsp creating an instance.

Maybe someone experience the same problem and give me some advices how a workaround could look like. At least a best practice solution would be fine as well 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:02:53+00:00Added an answer on June 11, 2026 at 5:02 am

    The DS annotations must be on the class being instantiated for the component. Annotations on super classes are not supported. There is a proposal to change the in a future spec release.

    What you can do is move the method to the super class, but you will need to trivially override the method in the subclass so that you can annotate it in the subclass.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Roxygen have made my work a lot easier and is in most cases nice
In most cases, I've been setting an event class handler that will apply to
Dealing with Java - which the option you prefer in the most cases -
I refactor some classes from standard SQL to JPA/ORM usage. In most cases the
I have a lot of Java 1.4 code, which I'm in the process of
I have two matrices X and Y (in most of my cases they are
I have entries table with a content field which might contain a lot of
Hi i do a lot of research on the web but at most cases
I have a lot of model classes with ralations between them with a CRUD
I worked a lot with Hibernate as my JPA implementation. In most cases it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.