Spring - Constructing Beans
By default Spring uses no arg constructor to create the bean. Spring also employs the same way of construction object as you would do normally while creating object. So when you say<bean id=“user” class=“User"/>
Spring uses the default constructor or a no argument constructor supplied by you. Note that for the class User to be used as a Spring bean one does not have to implement any interface. In fact User class does not even knows that it is being used by Spring.
If the User class has a static method than also you can ask spring to use the static method.
User.java
public static User createUser(){ System.out.println("In createUser method"); return new User(); }
Now to create the bean using this factory method, in configuration file
<bean id=“factoryUser” class=“User“ factory-method=“createUser” />
Fetching the object
User factoryUser = (User)appContext.getBean("factoryUser");
If the object creation logic is in another factory class than also you can ask Spring to use the factory class to create the object
UserFactory.java
public class UserFactory { public User createUser(){ System.out.println("Using factory to createUser"); return new User(); } }
Register the factory class in Spring
<bean id="userFactory" class="UserFactory" />
Ask spring to use the factory class to create the User object
<bean id="userCreatedFromFactory“ factory-bean="userFactory"factory-method="createUser"> </bean>
Spring Framework Index Java Home Home
Sidebar
Last wiki comments
- AOP: Thanks
- Lalit Bhatt: Superb Collection
- Lalit Bhatt: J2EE training
- Introduction to ORM: timberland shoes
- Introduction to ORM: jordan shoes
- Introduction to ORM: nike air max
- Pune Tourist Spots: KONARK PARK CLOSED
- jQuery Form Validations: Jquery Developer
- Spring Introduction: RG
- SOAP: Re: Assertion
Sidebar
Random Pages
- Comprehensive Overview of Java Technologies
- SOAP
- Pune Hill Forts For Trekking
- Windows Antivirus
- Source Code Control Systems
- JavaFX Data Binding and Trigger
- Introduction to Seam
- CSS - Handling Browser Moods
- Parsers Introduction
- Spring Annotation driven Aspects
- JAXB Validation using Schema
- Spring POJO Aspects
- What markets work on?
- Why projects fail?
- Bharat Band - Jai ho
- The concept of Nation
- Don't hide complexity if it cannot be handled in a robustway
Last blog post comments
-
Bharat Band - Jai ho: How do we protest?
Wed 18 of Aug., 2010 13:13 IST
-
Divided by Destiny: Contact
Fri 23 of July, 2010 16:02 IST
-
Future of Java: thesis writing
Sat 17 of July, 2010 01:50 IST
-
Hang till Death Mr. Kasab: some change
Mon 28 of June, 2010 16:03 IST
-
God Religion : Why we are confused?: Re: Is GOD Necessary?
Tue 15 of June, 2010 17:29 IST
-
God Religion : Why we are confused?: Is GOD Necessary?
Tue 15 of June, 2010 13:06 IST
-
The reason in religion: good
Wed 10 of Mar., 2010 18:30 IST
-
The confusion of Design Patterns: I think at macro level you are right...
Tue 23 of Feb., 2010 03:31 IST
-
The Indian Municipality: Comment
Fri 22 of Jan., 2010 13:20 IST
-
What Government should do?: Re: Review of the Indian Law and Order and Justice Dispensation regime.
Fri 22 of Jan., 2010 13:16 IST
Post new comment