Spring Bean Basics

1. If we want to access Beans in different XML Configuration files use ref tag bean attribute.
    example :

2.If we want to access Beans in same XML Configuration file use ref tag local attribute.
   example :

3..The ref tag can access bean in same or different XML file.For readability purpose we  use   local to refer to bean in same XML file.

4.In Spring there are 3 ways to inject values into  bean properties
  • Normal way :inject value using property tag.
        
               rest
         

  • Shortcut:inject value with "value" attribute.
         

  •  Using "p" schema  :  Inject values using P schemas an attributes.
         
               .....................................
              xmlns:p = http://org.springframework.com/schema/p
             
                p:name ="test" p:type = "rest"  />
        

              
5. In Large projects there are multiple configurations located at different folders for maintainability and modular.Better way to organize all spring bean configuration in single XML file and import all file resources.

 example: spring_combine.xml



.......................





Now Load all single XML file using

ApplicationContext context = new ClassPathXmlApplicationContext ("spring_combine.xml");


Then put this file under classpath

  classpath/spring_combine.xml

6. In Spring Framework,whenever a bean is used for only one particular property,it's  advise to declare it as an inner bean.And the inner bean is supported both in setter injection "property"
and constructor injection "constructor-arg".


 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
  id="CustomerBean" class="com.test.common.Customer">
   name="person" ref="PersonBean" />
 
>   id="PersonBean" class="com.test.common.Person"> name="name" value="test" /> name="address" value="address1" /> name="age" value="28" /> >   >
In general, it’s fine to reference like this, but since the ‘test’ person bean is only used for Customer bean only, it’s better to declare this ‘test’ person as an inner bean as following :
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
  id="CustomerBean" class="com.test.common.Customer">
   name="person">
    class="com.test.common.Person">
     name="name" value="test" />
     name="address" value="address1" />
     name="age" value="28" />
   
> > > >
This inner bean also supported in constructor injection as following :
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
  id="CustomerBean" class="com.test.common.Customer">
  >
    class="com.test.common.Person">
     name="name" value="test" />
     name="address" value="address1" />
     name="age" value="28" />
   
> > > >
Run it
package com.test.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App 
{
    public static void main( String[] args )
    {
     ApplicationContext context = 
       new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
 
     Customer cust = (Customer)context.getBean("CustomerBean");
     System.out.println(cust);
 
    }
}
Output
Customer [person=Person [address=address1, age=28, name=test]]
  ref :http://www.mkyong.com/tutorials/spring-tutorials/
 7. 5 types of bean scopes supported :
  1. singleton – Return a single bean instance per Spring IoC container
  2. prototype – Return a new bean instance each time when requested
  3. request – Return a single bean instance per HTTP request. *
  4. session – Return a single bean instance per HTTP session. *
  5. globalSession – Return a single bean instance per global HTTP session. *
In most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton.
In Singleton scope getBean() returns only one instance.
In Proptotype scope for every call of getBean() returns new instance.
Scope can be declared 
  •    in XML configuration file by using "scope"  tag.
                 ex:
  •       Using annotaion
    @Service
    @Scope("prototype")
    public class CustomerService {.......}
      and Enabling the component scan by
      base-package="com.test.customer" />
     
8.     
Spring examples to show you how to inject values into collections type (List, Set, Map, and Properties). 4 major collection types are supported :
List,Set,Map and properties
configutraion file example :
  id="CustomerBean" class="com.test.common.Customer">
 
  
   name="lists">
   >
    >1
> bean="PersonBean" /> class="com.mkyong.common.Person"> name="name" value="testList" /> name="address" value="address" /> name="age" value="28" /> > > >   name="sets"> > >1> bean="PersonBean" /> class="com.test.common.Person"> name="name" value="testSet" /> name="address" value="address" /> name="age" value="28" /> > > >   name="maps"> > key="Key 1" value="1" /> key="Key 2" value-ref="PersonBean" /> key="Key 3"> class="com.test.common.Person"> name="name" value="testMap" /> name="address" value="address" /> name="age" value="28" /> > > > >   name="pros"> > key="admin">admin@nospam.com> key="support">support@nospam.com> > >   >   id="PersonBean" class="com.test.common.Person"> name="name" value="test2" /> name="address" value="address 1" /> name="age" value="28" /> >   > run it


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App 
{
    public static void main( String[] args )
    {
     ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
 
     Customer cust = (Customer)context.getBean("CustomerBean");
     System.out.println(cust);
 
    }
}

Output
Customer [
 
lists=[
1, 
Person [address=address 1, age=28, name=test], 
Person [address=address, age=28, name=testList]
], 
 
maps={
key 1=1,
key 2=Person [address=address 1, age=28, name=test1], 
key 3=Person [address=address, age=28, name=testMap]
}, 
 
pros={admin=admin@nospam.com, support=support@nospam.com}, 
 
sets=[
1, 
Person [address=address 1, age=28, name= test1], 
Person [address=address, age=28, name=testSet]]
]