0% found this document useful (0 votes)
28 views4 pages

Configurations

The document discusses different ways to define and configure beans in Spring including: creating beans from static/instance factory methods, setting bean properties through setters or constructors, defining bean scopes as singleton or prototype, resolving constructor arguments, and defining inner bean properties.

Uploaded by

patel jay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

Configurations

The document discusses different ways to define and configure beans in Spring including: creating beans from static/instance factory methods, setting bean properties through setters or constructors, defining bean scopes as singleton or prototype, resolving constructor arguments, and defining inner bean properties.

Uploaded by

patel jay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

>>> bean factory

Resource res=new ClassPathResource(".xml");


or
Resource res=new FileSystemResource(".xml");
BeanFactory bf=new XmlBeanFactory(res);

<beans>
<bean id="" class="" >
</bean>
</beans>

>> bean creating via static factory method


<bean id="id" class="class1"
factory-method="createInstance" />

ex :
public class democlass1 implements demointer
{
public static void method1()
{
}
}

<bean id="id" class="democlass1"


factory-method="method1" />

>>> bean creating via instance factory method

<bean id="" factory-bean=""


factory-method="" />

ex :
public class democlass1 implements demointer
{
public void method1()
{
}
}

<bean id="d1" factory-bean="democlass1"


factory-method="method1" />

>>>> singleton or not


<bean id="" class="" singleton="true/false" />
default is true

>>>>> setting bean properties and collaborators


setter based dependency injection
constructor-based dependency injection
I) setter-based

public class democlass


{
private Class1 class1;
private Class2 class2;
private int sno;
public void setClass1(Class1 c){ class1=c; }
public void setClass2(Class2 c){ class2=c; }
public void setSno(int i){ sno=i; }
}

<bean id="demo" class="democlass">


<property name="class1"><ref bean="c1" /></property>
<property name="class2"><ref bean="c2" /></property>
<property name="sno"><value>1</value></property>
</bean>
<bean id="c1" class="Class1" />
<bean id="c2" class="Class2" />

II) constructor based

public class democlass


{
private Class1 class1;
private Class2 class2;
private int sno;
public democlass(Class1 c1,Class2 c2,int i)
{
class1=c1;
class2=c2;
sno=i;
}
}
<bean id="demo" class="democlass">
<constructor-arg><ref bean="c1" /></constructor-arg>
<constructor-arg><ref bean="c2" /></constructor-arg>
<constructor-arg><value>1</value></constructor-arg>
</bean>
<bean id="c1" class="Class1" />
<bean id="c2" class="Class2" />

III) static factory method to return instance of the bean

public class democlass


{
private Class1 class1;
private Class2 class2;
private int sno;
public democlass(Class1 c1,Class2 c2,int i)
{
class1=c1;
class2=c2;
sno=i;
}
public static democlass method1(Class1 c1,Class2 c2,int i)
{
democlass d1=new democlass(c1,c2,i);
return d1;
}
}
<bean id="demo" class="democlass" factory-method="method1" >
<constructor-arg><ref bean="c1" /></constructor-arg>
<constructor-arg><ref bean="c2" /></constructor-arg>
<constructor-arg><value>1</value></constructor-arg>
</bean>
<bean id="c1" class="Class1" />
<bean id="c2" class="Class2" />

>>>> constructor arguments resolution

public class democlass


{
private int sno;
private String sname;
public democlass(int a,String b)
{
sno=a;
sname=b;
}

a) constructor argument type matching


<bean id="demo" class="democlass" >
<constructor-arg type="int"><value>101</value></constructor-arg>
<constructor-arg type="java.lang.String"><value>ram</value></constructor-arg>
</bean>

b)constructor argument index


<bean id="demo" class="democlass">
<constructor-arg index="0"><value>101</value></constructor-arg>
<constructor-arg index="1"><value>ram</value></constructor-arg>
</bean>

>>>>> bean properties and constructor arguments detailed


a) value element
<bean id="demo" class="democlass" destroy-method="close">
<property name="sno"><value>1001</value></property
</bean>

b) null element
<bean id="demo" class="democlass" destroy-method="close">
<property name="sname"><value></value></property
</bean>

or

<bean id="demo" class="democlass" destroy-method="close">


<property name="sno"><value/></property>
</bean>

c) collection elements

list ,set ,map,props as List ,Set,Map,Properties


<bean id="demo" class="democlass">
i) public void setPeople(java.util.Properties)
<property name="people">
<props>
<prop key="one">the magic number</prop>
<prop key="two">the other magic</prop>
</props>
</property>

ii) setSomeList(java.util.List)
<property name="someList">
<list>
<value> the list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>

iii) setSomeMap(java.util.Map)
<property name="someMap">
<map>
<entry>
<key><value> some entry</value></key>
<value>some string</value>
</entry>
<entry>
<key><value> some entry</value></key>
<value>some string</value>
</entry>
<map>
</property>

iv) setSomeSet(java.util.Set)
<property name="someSet">
<set>
<value> the set element followed by a reference</value>
<ref bean="myDataSource" />
</set>
</property>

>>>> inner bean definitions via nested bean elements

<bean id="demo" class="democlass" >


<property name="target">
<bean class="personimpl">
<property name="name"><value>ram</value></property>
<property name="age"><value>34</value></property>
</bean>
</property>
</bean>

You might also like