Searching...
Thursday, 27 June 2013

Spring Beans AutoWiring examples

10:33

Spring Autowiring

Spring frameworks provides autowiring feature, it enables you to inject the object dependency implicitly. It internally uses setter or constructor injection.
it helps cut down on the amount of XML configuration you write for a big Spring based application.

Types of Autowiring

There are couple of autowiring modes which can be used to instruct Spring container to use autowiring for dependency injection. Some of them are

no
This is default setting which means no autowiring and you should use explicit bean reference for wiring.
byName
Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file.
byType
Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file.
constructor
Similar to byType, but type applies to constructor arguments.
autodetect
Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType

XML Based example

We have below two classes, CarInsurance and CarOrder and in CarOrder class we want to inject CarInsurance class, 

CarInsurance.java

public class CarInsurance {
    private String name;
    private String policyDetails;
//Getters and setters

CarOrder.java

public class CarOrder {

    private String carDetails;
    private CarInsurance carInsurance;
    public CarOrder(CarInsurance carInsurance) {
        super();
        this.carInsurance = carInsurance;
    }  
    public CarOrder() {}
//Getters and setters

Next, our goal is to inject CarInsurance bean to CarOrder bean..

<!-- Car insurance bean -->
    <bean id="carInsurance" class="com.vinod.test.CarInsurance">
        <property name="name" value="Progressive" />
        <property name="policyDetails" value="Full cover" />
    </bean>
   
    <!-- Autowiring by name -->
    <bean id="carOrder" class="com.vinod.test.CarOrder" autowire="byName">
    </bean>
   
    <!-- Autowiring by constructor -->
    <bean id="carOrder1" class="com.vinod.test.CarOrder"  autowire="constructor">
    </bean>

The above configuration, the bean carOrder will get the CarInsurance bean reference via auto wiring by name as we declared the property name in CarOrder, the second bean carOrder1 bean will get the CarInsurance reference via constructor as we defined the Constructor in CarOrder.

Annotation based Autowiring example

Annotation based auto wiring is very easy and it helps to inject dependencies via @Autowired annotation., see this example

1. Do the component scan in our Spring configuration


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.vinod.test")
public class MySpringConifg {
   
}

2. Create Java components with @Component annotaions

package com.vinod.test;
import org.springframework.stereotype.Component;

@Component
public class Customer {
    public String getCustomer() {
        return "Thomas....Regular customer";
    }
}
 

 

package com.vinod.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class CustomerDetails {
    @Autowired
    Customer customer;
    public Customer getCustomerDetails() {
        return customer;
    }
}
 

The CustomerDetails class we are using the @Autowired annotation to inject Customer bean, let us execute this program

3. Test class

package com.vinod.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class CustomerTest {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(MySpringConifg.class);
        context.refresh();
        CustomerDetails cd = context.getBean(CustomerDetails.class);
        System.out.println(cd.getCustomerDetails().getCustomer());
    }

}
 

Output

Thomas....Regular customer

 

Done!!!!

Download this complete example..

https://github.com/kkvinodkumaran/spring



0 comments: