dependency injection - What is the advantage of autowiring in spring -
what advantages of autowiring spring?
an example of autowiring in spring like
public class testclass { testmethod() { // ..... }; } public class mainclass { public static void main(string[] args) { applicationcontext ctx = new classpathxmlapplicationcontext("test.xml"); testmethod obj = (testclass) ctx.getbean("test"); obj.testmethod(); } }
test.xml
<bean id="test" class="testclass">
same in normal operation done using:
public class mainclass { public static void main(string[] args) { testclass obj = new testclass(); obj.testmethod(); } }
what advantage of spring, mean have heard terms inversion of control , dependency injection. in both examples reference of testclass used once through spring xml again through new
oerator. can in simple terms explain advantage.
autowiring in spring autowiring feature of spring framework enables inject object dependency implicitly. internally uses setter or constructor injection. autowiring can't used inject primitive , string values. works reference only.
advantage of autowiring requires less code because don't need write code inject dependency explicitly.
Comments
Post a Comment