Bean Overview:



Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata:
  • A package-qualified class name: typically the actual implementation class of the bean being defined.
  • Bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).
  • References to other beans that are needed for the bean to do its work; these references are also called collaborators or dependencies.
  • Other configuration settings to set in the newly created object, for example, the number of connections to use in a bean that manages a connection pool, or the size limit of the pool.

èThe ApplicationContext implementations also permit the registration of existing objects that are created outside the container, by users. This is done by accessing the ApplicationContext's BeanFactory via the method getBeanFactory() which returns the BeanFactory implementation DefaultListableBeanFactory. DefaultListableBeanFactory supports this registration through the methodsregisterSingleton(..) and registerBeanDefinition(..).

Every bean has one or more identifiers. A bean usually has only one identifier, but if it requires more than one, the extra ones can be considered aliases. In XML-based configuration metadata, you use the id and/or name attributes to specify the bean identifier(s). bean id uniqueness is still enforced by the container, though no longer by XML parsers. If no name or id is supplied explicitly, the container generates a unique name for that bean. Motivations for not supplying a name are related to using inner beans and autowiring collaborators. These names can be equivalent aliases to the same bean, and are useful for some situations, such as allowing each component in an application to refer to a common dependency by using a bean name that is specific to that component itself.  In XML-based configuration metadata, you can use the  element to accomplish this.
name="fromName" alias="toName"/>
if you have a class called Foo in the com.example package, and this Foo class has a static inner class called Bar, the value of the 'class' attribute on a bean definition would be...
com.example.Foo$Bar
Notice the use of the $ character in the name to separate the inner class name from the outer class name.
Instantiating beans
If you use XML-based configuration metadata, you specify the type (or class) of object that is to be instantiated in the class attribute of the  element. This classattribute, which internally is a Class property on a BeanDefinition instance, is usually mandatory.
You use the Class property in one of two ways:
  • Typically, to specify the bean class to be constructed in the case where the container itself directly creates the bean by calling its constructor reflectively, somewhat equivalent to Java code using the new operator.
  • To specify the actual class containing the static factory method that will be invoked to create the object, in the less common case where the container invokes astaticfactory method on a class to create the bean. The object type returned from the invocation of the static factory method may be the same class or another class entirely.
Instantiation with a constructor
When you create a bean by the constructor approach, all normal classes are usable by and compatible with Spring.
Instantiation with a static factory method
id="clientService"
      class="examples.ClientService"
      factory-method="createInstance"/>
When defining a bean that you create with a static factory method, you use the class attribute to specify the class containing the static factory method and an attribute named factory-method to specify the name of the factory method itself
Instantiation using an instance factory method
Similar to instantiation through a static factory method, instantiation with an instance factory method invokes a non-static method of an existing bean from the container to create a new bean. To use this mechanism, leave the class attribute empty, and in the factory-bean attribute, specify the name of a bean in the current (or parent/ancestor) container that contains the instance method that is to be invoked to create the object.
id="serviceLocator" class="examples.DefaultServiceLocator">
 


id="clientService"
      factory-bean="serviceLocator"
      factory-method="createClientServiceInstance"/>
One factory class can also hold more than one factory method as shown here:
id="serviceLocator" class="examples.DefaultServiceLocator">
 

id="clientService"
      factory-bean="serviceLocator"
      factory-method="createClientServiceInstance"/>

id="accountService"
      factory-bean="serviceLocator"
      factory-method="createAccountServiceInstance"/>

public class DefaultServiceLocator {
  private static ClientService clientService = new ClientServiceImpl();
  private static AccountService accountService = new AccountServiceImpl();
 
  private DefaultServiceLocator() {}
 
  public ClientService createClientServiceInstance() {
    return clientService;
  }
 
  public AccountService createAccountServiceInstance() {
    return accountService;
  }
}


No comments:

Post a Comment