• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
jatan bhavsar
Ranch Hand
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Point taken..

Thanks Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic