Is it possible to enter a value in an abstract class this way?
Abstract classes cannot be created, so nothing can be introduced into an abstract class. Instead, you should enter a value in your specific subclass.
Make sure your particular subclass is marked as @Component stereotype and is a "component scan" on Spring. @Component in an abstract class is not required because it cannot be created.
Update. Finally, I found out that you were trying to access the entered value inside the constructor, but found that the value was not set. This is because Spring will enter the value after the bean is created. Therefore, if the constructor injection is not used, the injected value cannot be accessible inside the constructor. You can use @PostContruct or implement InitializingBean as suggested by Matt.
The following shows if the XML configuration is used:
<context:property-placeholder location="classpath:xxxxx.properties" ignore-unresolvable="true" /> <bean id="parent" class="pkg.Parent" abstract="true" init-method="postConstruct"> <property name="sharedVal" value="${shared.val}" /> </bean> <bean id="child" class="pkg.ChildA" parent="parent">
Perform a common action using sharedVal inside Parent#postConstruct()
source share