-1

I have multiple similar configuration classes in my project.

@Configuration
public class configA {
  private final static String CONFIG_STRING = "configA";

  @Bean( name = "first" + CONFIG_STRING )
  public First first() {
    ...
  }

  @Bean( name = "second" + CONFIG_STRING )
  public Second second() {
    ...
  }
}

@Configuration
public class configB {
  private final static String CONFIG_STRING = "configB";

  @Bean( name = "first" + CONFIG_STRING )
  public First first() {
    ...
  }

  @Bean( name = "second" + CONFIG_STRING )
  public Second second() {
    ...
  }
}

They're all the same, only the CONFIG_STRING is different.

Can I refactor those classes?

Rurien
  • 359
  • 1
  • 13
  • Well first of all, you can always update / change / evolve or "refactor" your code. But then again, whats the concrete question in here? Have you tried your way around refactoring and had an issue? Which one? SO is always willing to help, but you have to ask a specific question to get a specific answer :) – phaen Jan 25 '23 at 07:37
  • @phaen I want to remove duplicate methods in multiple configuration classes. ```first()``` and ```second()``` in my code. – Rurien Jan 25 '23 at 07:55
  • I need to create 10+ similar configuration classes. That's why I want to reduce them. – Rurien Jan 25 '23 at 07:56
  • 2
    Why don't you use inheritance? – Osama.070032 Jan 25 '23 at 08:01
  • This again is not really a question: you are asking SOs to give a solution to your problem without getting your head around it. Have you tried something? As @Osama.070032 already said: use inheritance, define a base class for your configurations with shared attributes / functionality and make all the implementations extend it. – phaen Jan 25 '23 at 08:05
  • 1
    An idea you can try - add all config strings in a collection, iterate and register the beans programmatically. This question should help with registration - [Add Bean Programmatically to Spring Web App Context](https://stackoverflow.com/questions/4540713/add-bean-programmatically-to-spring-web-app-context). – Chaosfire Jan 25 '23 at 08:13
  • 1
    @phaen I can't understand why you're so aggressive in my question. Some people may not even know they need to use inheritance. Answers don't have to be code. Osama.070032 simply answered in my question. – Rurien Jan 25 '23 at 08:14

1 Answers1

1

You can use Template Method in order to let subclasses override specific steps of the algorithm without changing its structure.

Here is an example below. If there is a method that keeps the shared behaviour, this approach would also be a good option.


BaseConfig:

@Configuration
public abstract class BaseConfig {

    protected abstract String first();

    protected abstract String second();
}

ConfigA:

@Configuration
public class ConfigA extends BaseConfig {

    private final static String CONFIG_STRING = "configA";

    @Override
    @Bean( name = "first" + CONFIG_STRING )
    protected String first() {
        return CONFIG_STRING;
    }

    @Override
    @Bean( name = "second" + CONFIG_STRING )
    protected String second() {
        return CONFIG_STRING;
    }
}

ConfigB:

@Configuration
public class ConfigB extends BaseConfig {

    private final static String CONFIG_STRING = "configB";

    @Override
    @Bean( name = "first" + CONFIG_STRING )
    protected String first() {
        return CONFIG_STRING;
    }

    @Override
    @Bean( name = "second" + CONFIG_STRING )
    protected String second() {
        return CONFIG_STRING;
    }
}
Murat Yıldız
  • 11,299
  • 6
  • 63
  • 63