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 6198901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:05:39+00:00 2026-05-24T04:05:39+00:00

In my project there’s a common base class that all client classes extend. This

  • 0

In my project there’s a common base class that all client classes extend. This has an @Autowired field that needs to be injected by Hibernate. These are all grouped together in another class that has an @Autowired collection of the base class.

In order to reduce boilerplate for client code I’m trying to get @Component inherited. With @Component not doing this by default (apparently it used to though), I created this workaround annotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
@Inherited
public @interface InheritedComponent {
}

… and annotated the base class with it. Its not pretty but I hoped it would work. Unfortunately it didn’t, which really confuses me as @Inherited should make it work

Is there any other way to get @Component inherited? Or do I just have to say that any class that extends the base class needs this boilerplate?

  • 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-05-24T04:05:40+00:00Added an answer on May 24, 2026 at 4:05 am

    The problem is that the Component annotation type itself needs to be marked with @Inherited.

    Your @InheritedComponent annotation type is correctly inherited by any classes that extend a superclass which is marked with @InheritedComponent – but it does not inherit @Component. This is because you have @Component on the annotation, not the parent type.

    An example:

    public class InheritedAnnotationTest {
    
        @InheritedComponent
        public static class BaseComponent {
        }
    
        public static class SubClass extends BaseComponent {
        }
    
        public static void main(String[] args) {
            SubClass s = new SubClass();
    
            for (Annotation a : s.getClass().getAnnotations()) {
                System.out.printf("%s has annotation %s\n", s.getClass(), a);
            }
        }
    }
    

    Output:

    class brown.annotations.InheritedAnnotationTest$SubClass has annotation @brown.annotations.InheritedComponent()

    In other words, when resolving what annotations a class has, the annotations of the annotations are not resolved – they do not apply to the class, only the annotation (if that makes sense).

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

Sidebar

Related Questions

I have a project where there are multiple applications that have some common configuration
Is there a way to run a project inside a solution that has more
I'm sure you have all been there, you take on a project where there
There is another recent Project Euler question but I think this is a bit
In my project there is a managed object called Group. This object itself can
In my project there are some 'Prototype' factories that create instances by cloning a
I have a complex project where there are many directories that have POM files,
In my project there are situations where we have to send xml messages (as
I am working in a project where there are configuration files, one in number
I'm working on a project where there is a lot of external service messaging.

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.