Spring Boot Abstract Classes #6858
Unanswered
malte-baumann
asked this question in
Q&A
Replies: 2 comments 5 replies
-
Hi @malte-baumann , So If I understand you correctly you have the abstract class implementing the interface and then the concrete classes with the 'toggle-able' function extend the abstract class. Sounds very reasonable. I can think of a few ways of avoiding duplication (eg. separate the interfaces) but it all depends on your context and use-case Would you be willing to share some code? A minimal repo reproducing? |
Beta Was this translation helpful? Give feedback.
1 reply
-
This is how I would separate the code to achieve something similar to what you want that works without using an abstract class: public interface CommonFeatureDemoService {
String common();
}
public interface FeatureDemoService extends CommonFeatureDemoService {
@Toggle(name = "demo-toggle", alterBean = "featureNewService")
String featureDemo();
}
public class CommonFeatureDemoServiceImpl implements CommonFeatureDemoService {
@Override
public String common() {
return "common";
}
}
@Service("featureNewService")
public class FeatureDemoNewServiceImpl extends CommonFeatureDemoServiceImpl implements FeatureDemoService {
@Override
public String featureDemo() {
return "In new implementation...";
}
}
@Service("featureOldService")
public class FeatureDemoOldServiceImpl extends CommonFeatureDemoServiceImpl implements FeatureDemoService {
@Override
public String featureDemo() {
return "In old implementation...";
}
}
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
So this is potentially a bug or just me not knowing what's going on under the hood.
The Problem I am trying to solve
I have an Interface which has lets say 10 Methods. But I just want to toggle 1 of them. (So one of them is annotated with
@Toggle()
.)That means I have two implementation Classes which have 9 times the same duplicated Methods and just 1 Method which is different. One is the old, one is the new implementation.
My Solution
The Solution I came up with is the following. I have created an abstract class which holds the common default methods and implements the Interface. Then I created two classes which extends the abstract class. These I have annotated with
@Service()
. These classes then just implement the one Method I want to toggle.The Problem which now occurs
This solution does not work. Whatever the Feature Toggle state is, I am always getting the old implementation and not the
alterBean
(new Implementation)Ideas?
Does any one have an Idea whats going on? Or is there a better way of minimizing the duplicated code?
Beta Was this translation helpful? Give feedback.
All reactions