60 seconds to Spring (3)

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

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]

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:27
  • 10710

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:00
  • 665

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:00
  • 1267

Fatal error: Maximum execution time of 60 seconds exceeded in

如题,如果安装Magento 出现如上面的错误,请到php安装目录里面更改php.ini里面的max_execution_time的值,默认时60,把它改为180就够了,至少我的机器是够服务器响应的!...
  • ddjohn
  • ddjohn
  • 2010-07-09 19:53:00
  • 2297

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

ubuntu 死机有很多种原因。
  • aaashen
  • aaashen
  • 2014-05-14 19:15:53
  • 2073

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:00
  • 805

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

https://petermolnar.eu/linux-tech-coding/ubuntu-11-10-disable-waiting-up-to-60-more-seconds-for-netw...
  • zhtw90
  • zhtw90
  • 2015-01-12 10:53:46
  • 609

Ubuntu 11.10 Waiting up to 60 more seconds for network configuration…

http://www.codewhirl.com/2011/10/ubuntu-11-10-waiting-up-to-60-more-seconds-for-network-configuratio...
  • sinat_34586770
  • sinat_34586770
  • 2016-12-01 20:34:24
  • 267

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

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

Maximum execution time of 30 seconds exceeded 错误解决方法

简单总结一下解决办法: 报错一:内存超限,具体报错语句忘了,简单说一下解决办法。 利用循环分批导入; 每个循环内部开始处使用sleep(5);语句,做延迟执行,防止服务器内存同一时间占用...
  • huihuangjiuai
  • huihuangjiuai
  • 2014-07-08 10:04:35
  • 625
收藏助手
不良信息举报
您举报文章:60 seconds to Spring (3)
举报原因:
原因补充:

(最多只允许输入30个字)