Forums Register Login

using setter method in spring

+Pie Number of slices to send: Send
Why do we have to write a setter method inside a controller class when we declare an object variable instead of directly creating an object?
+Pie Number of slices to send: Send
This allows the Spring container to inject the required object by reading the proper class name from beans configuration file.
This is called "Depenedency Injection" where your class is not responsible to create its associated objects. Another concept here is "Decoupling": this means you use the interface (not the actual implementation) to decalre you class fields. This is important because you may have different implementations of the same service interface, therefore, you don't couple your class with one implementation. For example, insted of using:


Use:


Spring container allows you to inject the actual implementation using beans configuration file.
There's a city wid manhunt for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 799 times.
Similar Threads
How to make a Immutable Object?
Bean setter methods........
BJO - 3 main feature of OOP
converting the json to bean exception
what is better way to initialize attributes of object constructor or through method ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:24:44.