Tech Rocks

Coldfusion
Java
JQuery

An online resource for latest web technologies like Coldfusion, JRun, Pro*C, JQuery, HTML5, PHP, W3C, Java, J2EE, C, C++, ORACLE, PL/SQL, MySql, Ajax, Coldbox, Fusebox, UNIX, JavaScript, NodeJS and much more... contact@tech-rocks.org

Showing posts with label bean. Show all posts
Showing posts with label bean. Show all posts

Tuesday, October 22, 2013

Spring Bean Auto wiring

The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory without using <constructor-arg> and <property> elements.

 

The autowiring functionality has five modes which can be used to instruct Spring container to use autowiring for dependency injection:

  • no: This is default setting which means no autowiring and you should use explicit bean reference for wiring. You have nothing to do special for this wiring. This is what you already have seen in Dependency Injection chapter.

  • byName: Autowiring by property name. Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file.

  • byType: Autowiring by property datatype. Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exist, a fatal exception is thrown.

  • constructor: Similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.

  • autodetect: Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType.

  •  

    Limitations of autowiring are:

  • Overriding possibility: You can still specify dependencies using <constructor-arg> and <property> settings which will always override autowiring.

  • Primitive data types: You cannot autowire so-called simple properties such as primitives, Strings, and Classes.

  • Confusing nature: Autowiring is less exact than explicit wiring, so if possible prefer using explicit wiring.

  •  

    Annotation wiring is not turned on in the Spring container by default. So, before we can use annotation-based wiring, we will need to enable it in our Spring configuration file by configuring <context:annotation-config/>.

     

    Note: null and empty string values can be injected in Spring

    Spring Bean Lifecycle

    • Instantiate - First the spring container finds the bean's definition from the XML file and instantiates the bean..

    • Populate properties - Using the dependency injection, spring populates all of the properties as specified in the bean definition..

    • Set Bean Name - If the bean implements BeanNameAware interface, spring passes the bean's id to setBeanName() method.

    • Set Bean factory - If Bean implements BeanFactoryAware interface, spring passes the beanfactory to setBeanFactory() method.

    • Pre Initialization - Also called postprocess of bean. If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesserBeforeInitialization() method.

    • Initialize beans - If the bean implements IntializingBean,its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is called.

    • Post Initialization - If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.

    • Ready to use - Now the bean is ready to use by the application.

    • Destroy - If the bean implements DisposableBean , it will call the destroy() method .

    Spring Bean

    The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML <bean/> definitions.

     

    The bean definition contains the information called configuration metadata which is needed for the container to know the followings:

    • How to create a bean
    • Bean's lifecycle details
    • Bean's dependencies

     

    When defining a <bean> in Spring, you have the option of declaring a scope for that bean. For example, to force Spring to produce a new bean instance each time one is needed, you should declare the bean's scope attribute to be prototype. Similar way if you want Spring to return the same bean instance each time one is needed, you should declare the bean's scope attribute to be singleton.

     

    The Spring Framework supports following five scopes, three of which are available only if you use a web-aware ApplicationContext.

    • singleton: This scopes the bean definition to a single instance per Spring IoC container.
    • prototype: This scopes a single bean definition to have any number of object instances.
    • request: This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
    • session: This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
    • global-session: This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

    The default scope of bean is Singleton for Spring framework and No, singleton beans are not thread-safe in Spring framework.

     

    Inner beans in Spring

    A <bean/> element inside the <property/> or <constructor-arg/> elements defines a so-called inner bean. An inner bean definition does not require a defined id or name; the container ignores these values. It also ignores the scope flag. Inner beans are always anonymous and they are always scoped as prototypes.

     

    To add a bean in spring application

    <?xml version="1.0" encoding="UTF-8"?>

    <beans 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-3.0.xsd">

    <bean id="helloWorld" class="com.jeetu.HelloWorld">
    <property name="message" value="Hello World!"/>
    </bean>

    </beans>

    XmlBeanFactory class

    The most commonly used BeanFactory implementation is the XmlBeanFactory class. This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application.