Spring Tutorial Home

  • Managing Database Transactions
  • Remoting in Spring
  • Working with the Web Layer

Spring Home

Java Resources

Community

Site

Example to Demonstrate Spring Auto wiring constructor and autodetect Autowiring :-









<?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-2.5.xsd"

default-destroy-method="destroy" default-init-method="createInstance">

    <bean id="test" class="com.visualbuilder.beans.TestBean" init-method="createInstance" destroy-method="destroy" />

    <bean id="disposableInitBean" class="com.visualbuilder.beans.DisposableInitBean" />

    <bean id="collectionInjectionBean" class="com.visualbuilder.beans.CollectionInjectionBean" >

        <property name="products">

            <list>

                <value>CAR</value>

                <value>MOTORCYCLE</value>

                <value>SCOOTER</value>

            </list>

        </property>

        <property name="productSet">

            <set>

                <value>CAR</value>

                <value>MOTORCYCLE</value>

                <value>SCOOTER</value>

            </set>

        </property>


        <property name="prices">

            <map>

                <entry key="
CAR" value="100" />

                <entry key="
MOTORCYCLE" value="200" />

                <entry key="
SCOOTER" value="10" />

            </map>

        </property>

    </bean>

    <bean id="typeCollectionInjectionBean" class="com.visualbuilder.beans.TypeCollectionInjectionBean" >


        <property name="products">

            <list>

                <value>CAR</value>

                <value>MOTORCYCLE</value>

                <value>SCOOTER</value>

            </list>

        </property>

        <property name="prices">

            <map>

                <entry key="CAR" value="100" />

                <entry key="MOTORCYCLE" value="200" />

                <entry key="SCOOTER" value="10" />

            </map>

        </property>

    </bean>

    <bean id="constructorInjectionBean" class="com.visualbuilder.beans.ConstructorInjectionBean" >

        <constructor-arg type="java.lang.String" value="John Smith" />

        <constructor-arg index="1" value="25" />

        <constructor-arg value="M" />

    </bean>


    <bean id="setterInjectionBean" class="com.visualbuilder.beans.SetterInjectionBean" >

          <property name="name" value="John Smith" />

          <property name="age" value="25" />

          <property name="sex" value="M" />


    </bean>

    <bean id="
wrapperBeanByName" class="com.visualbuilder.beans.WrapperBean" autowire="byName"></bean>

    <bean id="
wrapperBeanByType" class="com.visualbuilder.beans.WrapperBean" autowire="byType"></bean>


    <bean id="wrapperBeanAuto" class="com.visualbuilder.beans.WrapperConstructorBean" autowire="autodetect"></bean>

    <bean id="
wrapperBeanConstructor" class="com.visualbuilder.beans.WrapperConstructorBean" autowire="constructor"></bean>



</beans>



WrapperConstructorBean.java









package com.visualbuilder.beans;


public class WrapperConstructorBean {

    private CollectionInjectionBean collectionInjectionBean;


    private ConstructorInjectionBean constructorInjectionBean;


    private DisposableInitBean disposableInitBean;


    private SetterInjectionBean setterInjectionBean;


    private TypeCollectionInjectionBean typeCollectionInjectionBean;


    public WrapperConstructorBean() {


        }


    public WrapperConstructorBean(CollectionInjectionBean collectionInjectionBean,ConstructorInjectionBean constructorInjectionBean,

        DisposableInitBean disposableInitBean,SetterInjectionBean setterInjectionBean,TypeCollectionInjectionBean typeCollectionInjectionBean) {

            this.collectionInjectionBean = collectionInjectionBean;

            this.constructorInjectionBean = constructorInjectionBean;

            this.disposableInitBean = disposableInitBean;

            this.setterInjectionBean = setterInjectionBean;

            this.typeCollectionInjectionBean = typeCollectionInjectionBean;

}


    public CollectionInjectionBean getCollectionInjectionBean() {

        return collectionInjectionBean;

        }


    public void setCollectionInjectionBean(CollectionInjectionBean collectionInjectionBean) {

                this.collectionInjectionBean = collectionInjectionBean;

        }


    public ConstructorInjectionBean getConstructorInjectionBean() {

        return constructorInjectionBean;

        }


    public void setConstructorInjectionBean(ConstructorInjectionBean constructorInjectionBean) {

                this.constructorInjectionBean = constructorInjectionBean;

        }


    public DisposableInitBean getDisposableInitBean() {

        return disposableInitBean;

        }


    public void setDisposableInitBean(DisposableInitBean disposableInitBean) {

                this.disposableInitBean = disposableInitBean;

        }


    public SetterInjectionBean getSetterInjectionBean() {

        return setterInjectionBean;

        }


    public void setSetterInjectionBean(SetterInjectionBean setterInjectionBean) {

                this.setterInjectionBean = setterInjectionBean;

        }


    public TypeCollectionInjectionBean getTypeCollectionInjectionBean() {

        return typeCollectionInjectionBean;

        }


    public void setTypeCollectionInjectionBean( TypeCollectionInjectionBean typeCollectionInjectionBean) {

                 this.typeCollectionInjectionBean = typeCollectionInjectionBean;

        }

    }



AutoDetectExample1.java








package com.visualbuilder.example;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.visualbuilder.beans.WrapperConstructorBean;


public class AutoDetectExample1 {

    public static void main(String[] ar) {

                ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "com/visualbuilder/xml/spring-beans1.xml" });

                System.out.println("Geting the Bean Autowired byConstructor");

                WrapperConstructorBean bean = (WrapperConstructorBean) context .getBean("wrapperBeanConstructor");

        bean.getConstructorInjectionBean().printData();

                System.out.println("Geting the Bean Autowired Autodetect");

                WrapperConstructorBean bean1 = (WrapperConstructorBean) context .getBean("wrapperBeanAuto");

        bean1.getConstructorInjectionBean().printData();

        }

}



Output;-


Jun 8, 2008 10:17:17 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Sun Jun 08 10:17:17 IST 2008]; root of context hierarchy

Jun 8, 2008 10:17:17 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans1.xml]

Jun 8, 2008 10:17:17 AM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9

afterPropertiesSet method is called

Geting the Bean Autowired byConstructor

Assigned Name is John Smith

Assigned Age is 25

Assigned Sex is M

Geting the Bean Autowired Autodetect

Assigned Name is John Smith

Assigned Age is 25

Assigned Sex is M


Jun 8, 2008 10:17:17 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9: defining beans [test,disposableInitBean,collectionInjectionBean,typeCollectionInjectionBean,constructorInjectionBean,setterInjectionBean,wrapperBeanByName,wrapperBeanByType,wrapperBeanAuto,wrapperBeanConstructor]; root of factory hierarchy

                    

Copyright © 2010 VisualBuilder. All rights reserved