As far as I'm concerned, Spring's DI was promoted to decouple classes,
but the above codes just inject properties. And I wonder whether
property injection is based on DI in Spring.
Assume that your HelloWorld
class is dependent upon some other interface called LanguageHelper
, then you can inject the implementation of LanguageHelper
dynamically at run time (Spring container creates/manages the objects for these classes, if found on the classpath), just by specifying the implementation class name in XML as shown below (or you can use Annotations):
<bean id = "helloWorld" class = "com.company.HelloWorld">
<property name = "languageHelper" ref = "languageHelper"/>
</bean>
<bean id = "languageHelper" class = "com.company.LanguageHelperImpl"></bean>
But, in your case, it is a simple String
value you want to inject to your bean dynamically at runtime (rather than hard coding in your class directly). So, it is an injection for which there are no additional dependencies to be evaluated, rather just set the values using the setter
methods provided.
So, it is all about how we can loose couple the class (could be simple values or other class implementation) for the future changes, rather than directly hard coding inside it.