60 seconds to Spring (3)

转载 2004年07月06日 23:26:00

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 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.



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



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


        <beans>                                       </beans>

Step 4: reate a Main.java in src



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.



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.



60 seconds to Spring (1)

原文地址:http://gabriel.jarchitect.org/spring/index.html 60 sec to Spring [TOC] Bean Tutorial 1 - Hello...
  • JasonSeaver
  • JasonSeaver
  • 2004年07月06日 23:24
  • 794

ubuntu12.04开机出现waiting up to 60 more seconds for network configuration问题的解决

问题是启动画面在读条的地方停好久 首先是提示 waiting for the network configuration 然后是 Waiting for 60 seconds more f...
  • liangxanhai
  • liangxanhai
  • 2012年06月16日 11:00
  • 10511

60 seconds to Spring

60 seconds to SpringIntroductionSpring is a layered Java/J2EE application framework, based on code p...
  • youlin
  • youlin
  • 2004年11月01日 10:32
  • 1255

60 seconds to Spring (2)

60 sec to Spring [TOC] Bean Tutorial 2 - To Singleton or Not To Singleton Beans are defined to be d...
  • JasonSeaver
  • JasonSeaver
  • 2004年07月06日 23:25
  • 656

UBUNTU : waiting-up-to-60-more-seconds-for-network-configuration

  • zhtw90
  • zhtw90
  • 2015年01月12日 10:53
  • 577

ubuntu 死机重启的方式 Login timed out after 60 seconds

ubuntu 死机有很多种原因。
  • aaashen
  • aaashen
  • 2014年05月14日 19:15
  • 1963

_00011 Redis 安装以及快速入门之一(Redis安装)

# Redis # Redis简介 redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(...
  • u012185296
  • u012185296
  • 2014年06月03日 17:38
  • 1163

which is more than the configured time (StuckThreadMaxTime) of "600" seconds

控制台错误信息: ds working on the request "Http Request: /amlm/updateT03_Bh_Tsdt.action", which is more...
  • woshishui6501
  • woshishui6501
  • 2012年07月23日 17:23
  • 5638

云之家API获取数据,Unirest.post 关闭Closing connections idle longer than 30 SECONDS

项目获取云之家的数据,写的demo在main方法里面执行没有问题,集成到自己公司的项目中,使用的是SSM框架,每当去获取数据的时候控制台一直跳: 14:05:25,954 INFO [STD...
  • qq_36856975
  • qq_36856975
  • 2017年10月09日 17:36
  • 107

warning:detected ap rate limiting,waiting 300seconds befoe re-checking

  • kingaming
  • kingaming
  • 2014年02月12日 22:54
  • 9168
您举报文章:60 seconds to Spring (3)