三V工作室·泰伯子仪

http://www.sanv.org/

60 seconds to Spring (3)

60 sec to Spring [TOC]

ruler.gif

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 when some properties are optional.

  • Constructor-based dependency injection is realized by invoking a constructor 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

org/jarchitect/spring/tutorial3/YetAnotherBean.java

Step 2: We will create a ConstructorExampleBean and a SetterExampleBean.

org/jarchitect/spring/tutorial3/ConstructorExampleBean.java

org/jarchitect/spring/tutorial3/SetterExampleBean.java

Step 3: Specify the HelloBean class in the bean.xml

bean.xml

        <beans>                                       </beans>

Step 4: reate a Main.java in src

org/jarchitect/spring/tutorial3/Main.java

                                                     

Step 5: Use an ANT script to build and execute the Main class. Just run ant from the command prompt will do the trick.

Below are the output from ANT.

                          

Overtime

From bean.xml

......              

Notice the index="2", try removing it and run ANT again. You will have the following error.

....... [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 BUILD SUCCESSFUL Total time: 1 second

Reason: The argument is an int, but BeanFactory take it as a String, so the mapping isn't 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]

阅读更多
个人分类: Framework
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭