..


Sponsored Links

Core Container - Spring beans

In this section we analyze the configuration through the IOC container xml metadata.
For a bean instazionare nell'IoC contanier enough for us just two infomarzioni:

  • Qualified Name of the class to which the object belongs
  • A unique id to be assigned to the bean in the context
Example:





 <beans id="myBean" class="com.MyClass"/>









 <beans class="com.MyClass1"/>



The second example is deliberately omitted the id attribute, because if you do not specify it generates a random Spring. However, it is recommended that you always specify an id, so you can reference the bean if necessary. A bean can have multiple identifiers that can be added through the name attribute or by the alias tag:
 



 <beans id="myBean" class="com.MyClass" name="alias1 alias2" />

 
or
 



 <alias name="myBean" alias="alias1" />

 

To instantiate the Spring beans using three methods:

  • Through the manufacturer
  • Through a factory method
  • Through a bean factory
If you do not specify a method to instantiate the beans, Spring uses the default constructor, so each bean must have the default construttore (later we will see how a bean instantiated using a constructor with parameters).
If you want to use a factory method, which is typical of a singleton , you have to use the factory-method attribute.





 {public class FactoryMethodBean





  



 private static instance = new FactoryMethodBean FactoryMethodBean ();





  



 Private FactoryMethodBean () {}





  



 public static FactoryMethodBean getInstance () {



    



 return instance;



  



 }









 }



and nell'applicationContext. xml
 



 <bean id="factoryMethodExample" class="it.mrwebmaster.singleton.FactoryMethodBean" factory-method="getInstance"/>

 

The last method of instantiation uses, in addition to the factory method, a bean factory, which is typical of the Service Locator .






 FactoryBean {public class





  



 Private ExampleBean former;





  



 public FactoryBean () {



    



 super ();



    



 Former ExampleBeanImpl = new ();



  



 }





  



 public ExampleBean getExampleBean () {



    



 return ex;



  



 }









 }



and nell'applicationContext. xml





 <bean id="factoryBean" class="it.mrwebmaster.factory.FactoryBean" />









 <bean id="exampleBean" class="it.mrwebmaster.factory.ExampleBean" factory-bean="factoryBean" factory-method="getExampleBean"/>



Scopes

An important property of a bean is its scope, which defines the life cycle. Spring supports five types of scope:

  • Singleton, creates a single instance of the bean in the container that is shared by all the other beans.
  • prototype, which is required every time a particular bean will be instantiated a new one, ie any other bean that refers to a prototype scoped bean will have its own instance.
  • request for each HTTP request is created an instance of this bean
  • session, for each http session is created an instance of this bean
  • global session, should be used only in applications that make use of portlets, which is present in the concept of global session between the various portlets.

The singleton and prototype scopes are available for all ApplicationContext as request, session, global session and are valid only for the WebApplicationContext.

If the scope attribute is not specified, the bean becomes singleton scope by default.

In addition to these scopes there exists a sixth, but the scope thread is not enabled by default. In Spring you can create custom scopes, but is left to the reader the depth of this topic.

Spring Java Guide
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links