This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Spring and the fly likes  BeanFactory getBean(String name) method implementation. Is it a factory method ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark " BeanFactory getBean(String name) method implementation. Is it a factory method ?" Watch " BeanFactory getBean(String name) method implementation. Is it a factory method ?" New topic
Author

BeanFactory getBean(String name) method implementation. Is it a factory method ?

Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

Hi,

I am new to Spring.

Please help in below doubts :

1. Which class/classes provide the implementation of getBean() method from BeanFactory interface ? I need to see the code in getBean() implementation.

2. Is the getBean() method a factory method as in factory method design pattern.
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52


Please confirm my understanding on Spring ApplicationContext and getBean(String name) method.

As per my understanding :


The above line of code reads the config file and loads all the classes defined in the config file into the JVM. And it does not actually creates objects.


The above line of code actually creates HelloWorld object.

Please confirm if my understanding is correct.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1646
    
    7

into the JVM

What do you mean here? I think you are trying to get to low level.



No unless the bean is specified as lazily loaded (and no eagerly loaded beans require it) all the objects are created.


Nope. This just finds the bean in the context and returns it (excepting the aforementioned lazy load use case).

[How To Ask Questions][Read before you PM me]
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 296

hi Abineet,


Please follow the link where you will come to know classes which implements the getbean() method.
http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/beans/factory/BeanFactory.html#getBean(java.lang.String, java.lang.Class) ..

Beanfactory is interface , Reference type assigned to object created by getbean() method. In documentation all classes are mentioned which implements the getbean() method.

Please correct me if i am wrong..

Regards
Jatan
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

Bill, Thanks a lot.
Thanks Jatan.

I have done some more reading on the same lines.

BeanFactory supports lazy instantiation.

So, can I say the method getBean(String name) here acts as a factory-method from design patterns ?
Because the object creation logic shall be encapsulated inside this method.

Where as ApplicationContext supports eager or pre-instantiation.
In this scenario all objects will be created when the application context is initialized.

So, the role of the method getBean(String name) is to locate the objects.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1646
    
    7

You do not typically use the BeanFactory directly. You use the ApplicationContext.

Read here:
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-beanfactory

Read here for more info on the lazy initialization.
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-lazy-init

Also the pull model your are talking about with getBean() is rarely used. It tightly couples your application code to the framework and defeats the purpose of DI. The usual case where you will see this is in a stand-alone application in the main method. You must get a Spring managed bean from the context to start things, but after that you should be letting Spring inject the dependencies rather than pulling them in.

To your question BeanFactory is an implementation of the Factory design pattern.

As was stated BeanFactory itself is an interface. The implementations of course differ from each other in a number of ways but generally speaking it will instantiate, configure and manage the life cycle of the beans. It loads the bean definitions immediately but depending may not instantiate them until they are requested.

Understanding all the intricacies is not a requirement for using the Spring Framework as by and large its handled for you, and as I said most of the time your application is unaware of bean factories or application contexts.

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1646
    
    7

Also I see you posted this on the Spring forum too.
http://forum.springsource.org/showthread.php?129769-BeanFactory-getBean%28String-name%29-method-implementation-Is-it-a-factory-method

Please read BeForthrightWhenCrossPostingToOtherSites
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

Point taken..

Thanks Bill
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: BeanFactory getBean(String name) method implementation. Is it a factory method ?
 
Similar Threads
nonintrusive in spring
Spring Controllers singleton or not?
BeanFactory Return Class Rather Than Object
Advantages of DI over factory pattern
Lazy and preloading of beans