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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:33:42+00:00 2026-06-09T06:33:42+00:00

I have three classes and one is a superclass. class abstract Base { public

  • 0

I have three classes and one is a superclass.

class abstract Base {
   public abstract String getV();
}

class A extends Base {
   private String v;
   @Value("${A.v}") public String getV() { return v; };
}

class B extends Base {
   private String v;
   @Value("${B.v}") public String getV() { return v; };
}

Is there any way to simplify the use of spring @Value annotation? I don’t want to copy-paste field declaration and the getter for all Base children. All I get so far is:

class abstract Base {
   protected String v;
   public String getV() { return v; };
   public abstract void setV(String v);
}

class A extends Base {
   @Value("${A.v}") public void setV(String v) { this.v = v;};
}

class B extends Base {
   @Value("${B.v}") public void setV(String v) { this.v = v;};
}

Is there any more efficient way? Thanks for any help.

  • 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-09T06:33:43+00:00Added an answer on June 9, 2026 at 6:33 am

    I doubt it. Annotations CANNOT be dynamic. There’s no way to put any kind of annotation on the abstract setV method which will adapt to the subclass. What you’ve done is really the right way to go.

    However i’d modify your code slightly:

    class Base {
       private String v;
       public String getV() { return v; };
       public void setV(String v) { this.v = v; }
    }
    
    class A extends Base {
       @Value("${A.v}") public String setV(String v) { super.setV(v); }
    }
    
    class B extends Base {
       @Value("${B.v}") public String setV(String v) { super.setV(v); }
    }
    

    I’ve had this issue not only with @Value annotations, but with @Resource/@Qualifier annotations as well.

    This is a big reason i’m not a huge fan of autowiring. I’ve found myself making a bunch of subclasses for no reason other than to autowire things with different qualifiers/values.

    Another alternative might be java based configuration. I know it’s not a huge departure from doing it in XML, but i find it cleaner than xml, and certainly better than overriding all your setters.

    @Configuration
    public class MyConfig {
        @Value("${A.v}")
        private String av;
    
        @Value("${B.v}")
        private String bv;
    
        @Bean
        public A a() {
           A a = new A();
           a.setV(av);
        }
    
    
        @Bean
        public B b() {
           B b = new B();
           b.setV(bv);
        }
    }
    

    And then in your XML:

    <context:annotation-config />
    <bean class="MyConfig" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have three classes class A class B extends A class C extends
Say I have three classes: class X{}; class Y{}; class Both : public X,
I have three classes; Classes A and B both reference class C . How
I have three classes: ClassA , ClassB , ClassC . ClassC extends ClassB which
I have three classes called Broker , Instrument and BrokerInstrument . using Iesi.Collections.Generic; public
Right now I have classes like: abstract class Record { // Required fields val
I have three links on which I manually set classes: One defaults to .active
I have three classes that implement an interface and I need one Method that
I have a superclass which instantiates my Sql class so all derived classes will
I have one java project in netBeans. There are some packages, and classes in

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.