spring - @Component vs new operator in java -
i new spring concept, have little confusion @component
default singleton creates instance whenever class loaded , same instance reused; same happens new operator. if class declared singleton can change properties of class using setters , getters same new operator also.
when call new
, creating instance during runtime manually.
assume , have controller being called 'n' number of times im turn calls service.
java
in plain java, creating new object calling new
. means , creating 'n'number of objects
spring
in spring, when deploy application in server or load spring xml/configuration class
, spring creates instances of classes annotated , , stores in spring container. now,even if controller called 'n'times, spring use same object again , again because wont call new
instead use annotation called autowired
Comments
Post a Comment