60 Sec to Spring [TOC]
Bean Tutorial 3 - SETTER-BASED AND CONSTRUCTION-BASED DEPENDENCY INJECTION
Inversion of Control / Dependency Injection EXISTS in Two Major Variants:
Setter-based dependency injection is realized by calling setters on your beans after invoking a no-argument constructor to instantiate your bean. Beans defined in the BeanFactory that use setter-based dependency injection are true JavaBeans. Spring generally advocates usage of setter-based dependency Injection, Since A Large Number of Constructor Arguments Can Get Unwieldy, especially.
Constructor-based Dependency Injection Is Realized by Invoking a Construction With A Number of Arguments, Each Representing a Collaborator or Property.
Step 1: We Will Need Two Dummy Classes for Our Tutorial, AnotherBean.java and YetAnotherbean.java.
ORG / JARCHITECT / SPRING / TUTORIAL3 / ANOTHERBEAN.JAVA
Package org.jarchitect.spring.tutorial3; public class anotherbean {public anotherbean () {system.out.println ("construct anotherbean");}}
ORG / JARCHITECT / SPRING / TUTORIAL3 / YETANOTHERBEAN.JAVA
Package org.jarchitect.spring.tutorial3; public class yetanotherbean {public yetanotherbean () {system.out.println ("construct yetanotherbean);}}
Step 2: We Will Create a ConstructorexampleBean and a setterexamplebean.
ORG / JARCHITECT / SPRING / TUTORIAL3 / ConstructorXampleBean.java
package org.jarchitect.spring.tutorial3; public class ConstructorExampleBean {private AnotherBean beanOne; private YetAnotherBean beanTwo; private int i; public ConstructorExampleBean (AnotherBean anotherExampleBean, YetAnotherBean yetAnotherBean, int i) {this.beanOne = anotherExampleBean; this.beanTwo = yetAnotherBean; THISTEM.OUT.PRINTLN ("Construction Done !!!");} public constructorexamplebean () {system.out.println ("default constructor: shouth);}} or /jarchitect/spring/tutorial3/setterexamplebean.java
package org.jarchitect.spring.tutorial3; public class SetterExampleBean {private AnotherBean beanOne; private YetAnotherBean beanTwo; private int i; public SetterExampleBean () {System.out.println ( "Default Constructor");} public void setBeanOne (AnotherBean bean) {this.beanOne = bean; System.out.println ( "Set beanOne");} public void setBeanTwo (YetAnotherBean bean) {this.beanTwo = bean; System.out.println ( "Set beanTwo");} public void setIntegerProperty (INT I) {this.i = i; system.out.println ("set i");}}
Step 3: Specify The HelloBean Class in the bean.xml
Bean.xml
XML Version = "1.0" encoding = "UTF-8"?>
ORG / JARCHITECT / SPRING / TUTORIAL3 / Main.java
package org.jarchitect.spring.tutorial3; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import org.springframework.beans.factory.xml.XmlBeanFactory; public class Main {public static void main (String [] args) {try {// Read the configuration file InputStream is = new FileInputStream ( "bean.xml"); XmlBeanFactory factory = new XmlBeanFactory (is); // Instantiate SetterExampleBean SetterExampleBean setterBean = (SetterExampleBean) factory. getBean ( "setterBean"); // Instantiate ConstructorExampleBean ConstructorExampleBean constructorBean = (ConstructorExampleBean) factory.getBean ( "constructorBean");} catch (FileNotFoundException e) {e.printStackTrace ();}}} Step 5: Use an ANT script to .................
BELOW is The Output from Ant.
C: / 60sec2Spring / SpringTutorial3> ant Buildfile: build.xml build: [javac] Compiling 5 source files to C: / 60sec2Spring / SpringTutorial3 / bin run: [java] Jun 1, 2004 12:36:51 PM org.springframework. beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions [java] INFO: Loading XML bean definitions from (no description) [java] Jun 1, 2004 12:36:51 PM org.springframework.beans.factory.support.AbstractBeanFactory getBean [java] INFO: Creating shared instance of singleton bean 'setterBean' [java] Default Constructor [java] Construct AnotherBean [java] Jun 1, 2004 12:36:51 PM org.springframework.beans.factory.support.AbstractBeanFactory getBean [java] INFO : Creating shared instance of singleton bean 'anotherExampleBean' [java] Construct YetAnotherBean [java] Set beanOne [java] Set beanTwo [java] Jun 1, 2004 12:36:51 PM org.springframework.beans.factory.support.AbstractBeanFactory getBean [Java] Info: Creating Shared Instance of Singleton Bean 'Yetanotherbean' [Java] Set I [Java] Jun 1, 2004 12 : 36: 51 PM org.springframework.beans.factory.support.AbstractBeanFactory getBean [java] INFO: Creating shared instance of singleton bean 'constructorBean' [java] Construction Done !!! [java] Jun 1, 2004 12:36: 51 PM org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory autowireConstructor [java] INFO:. instantiated via constructor Bean 'constructorBean' [public org.jarchitect.spring.tutorial3 ConstructorExampleBean (org.jarchitect.spring.tutorial3.AnotherBean, org. Jarchitect.Spring.Tutorial3.Yetanotherbean, int)] Build Successful Total Time: 2 SECONDSOVERTIME
From bean.xml
......
....... [java] INFO: Creating shared instance of singleton bean 'constructorBean' [java] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'constructorBean' defined in (no description): 3 constructor arguments specified but no matching constructor found in bean 'constructorBean'. (hint: specify index arguments for simple parameters to avoid type ambiguities) [java] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory autowireConstructor (AbstractAutowireCapableBeanFactory.java:287 ) [java] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory. createBean (AbstractAutowireCapableBeanFactory.java:181) [java] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:159) [ Java] At Org.jarchitect.Spring.Tutorial3.main.main (unknown Source) [Java] Exception in Thread "Main" [Java] Java Result: 1 Buccessful Total Time: 1 SECOND
Reason: The argument is an int, but BeanFactory take it as a String, so the mapping is not obvious to the matching algorithm In case of primitives, it's recommended to specify the argument index explicitly.More Reason:. For example, constructors MyClass (Bool Flag, String Toto, Int Moo, FLOAT COW). There is no way for the beanfactory to Tell Which is Which from the string representations of each.
Next, try swapping
And Run Ant.
DONE.
[TOC]