Spring container call the instanceFactory() of the spring bean

previous-button-hisubmit-hi

 

Que: -Modify the above application in such a way that spring container call the instanceFactory() of the spring bean.

instance Factorymethod example

  1. beans.xml
  2. HelloBean.java
  3. ExecuteBean.java

 

Note: –ExecuteBean.java and HelloBean.java from previous application expect making getInstance () method as non-static method.

beans.xml”

<beans>
<bean name=”hb” class=”HelloBean” >
<constructor-arg value=”hello”>
< /bean>
<bean name=”hb1” class=”HelloBean” factory-bean=”hb” factory-method=”getInstance”/>
< /beans>

Que: -How to specify same user-defined initialization method and destruction method for all the beans of the spring application?

By using “default-init-method” and “default-destroy-method” attributes of <beans> tag we specify custom initialization and destruction methods for the multiple spring beans.

e.g.: -
<beans------default-init-method=”init”
default-destroy-method=”destroy”>

Que: -How to inject a list of based collection into a spring bean?

Step 1: –The spring bean class declare “java.util.List” type variable as dependency.

e.g.: –

public class College
{
List<Student> students;
----------
----------
}

Step 2: –Make use of one of the four collection configuration elements in bean configuration file i.e. use <list> tag.

e.g.: –

<list>
<ref bean=”s1”/>
<ref bean=”s2”/>
<ref bean=”s3”/>
< /list>

list based injection

  1. student.java
  2. college.java
  3. springApplication.java
  4. beans.xml

In this example college is the dependent bean collection of student bean is the dependency.

student.java”

package com.nareshit.spring;
public class student
{
Private int rollno;
Private String name;
public Student(int rollno, String name)
{
this.rollno=rollno;
this.name=name;
}
public void displayStudentDetails()
{
System.out.println(“Rollno:”+rollno);
System.out.println(“Name:”+name);
}//business method
}

College.java”

package com.nareshit.spring;
import java.util.list;
public class College //dependent
{
private String name; //dependency 1
private List<student> students; //dependency 2
private College(String name)
{
this.name=name;
}//for constructor injection
public void setStudents (List<student> students)
{
this.students=students;
}//fpr setter injection
public List<student> getStudents()
{
return students;
}//business method
}

beans.xml”

<beans>
<bean id=”s1” class=”com.nareshit.spring.Student”/>
<constructor-arg value=”101” />
<constructor-arg value=”Rama” />
< /bean>
<bean id=”s2” class=”com/nareshit.spring.Student”/>
<constructor-arg value=”102” />
<constructor-arg value=”Rohini” />
< /bean> 
<bean id=”s3” class=”com/nareshit.spring.Student”/>
<constructor-arg value=”103” />
<constructor-arg value=”David” />
< /bean> 
<bean id=”c” class=”com/nareshit.spring.Student”/>
<constructor-arg value=”NIT” />
<property name=”students” />
<list>
<ref bean=”s1” />
<ref bean=”s2”/>
<ref bean=”s3”/>
< /list>
< /property>
< /bean> 
< /beans>

SpringApplication.java”

import com.nareshit.spring.College;
import com.nareshit.spring.Student;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.FileSystemResource;
import java.util.List;

class SpringApplication
{
public static void main(String [] args)
{
Resource resource=new FileSystemResorce (“beans.xml”);
BeanFactory =new XmlBeanFactory (resource);
Object o=factory.getBean (“c”);
College c1= (college) o;
List <student> listOfStudent=c1.getStudents (); //business method call
int count=1;
for (Student s:listOfStudents)
{
System.out.println(“Student”+count+”details…”);
s.displayStudentDetails ();
count ++;
}
} //main
} //class

previous-button-hisubmit-hi

Share Button

One thought on “Spring container call the instanceFactory() of the spring bean

Leave a Reply

Your email address will not be published. Required fields are marked *