What context:annotation-config is for?

It allow us to use @Autowire, @Required and @Qualifier annotations.

What context:component-scan is for?

It allow @Component, @Service, @Controller, etc.. annotations.

What is the advantage?

It allows us to manage dependency directly in the java class.

Step by Step example.

This example show how to classes:

- Adder.java, A class to do the arithmetic add operation

- Calculator.java, A class that calls the Adder class using the Autowired annotation

1. Write an applicationContext file and put it in the root of the classpath.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns: xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

       <context:component-scan base-package="test.example"/>
       <context:annotation-config/>
</beans>

you should provide to component-scan the base path you what to scan component

2. Write Adder.java

package test.example;
import org.springframework.stereotype.Component;
@Component("adder")
public class Adder {
    public int add(int a, int b){
        return a + b;
    }
}

3. Write Calculator.java

package test.example;

import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component("calculator")
public class Calculator {
    private Adder adder;
    @Autowired
    public void setAdder(Adder adder) {
        this.adder = adder;
    }
    public void makeAnOperation(){
        int r1 = adder.add(1,2);
        System.out.println("r1 = " + r1);   
    }
}

4. Write the main method (it can be in a class called Main.java):

    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
        xmlReader.loadBeanDefinitions(new ClassPathResource("applicationContext.xml"));
        context.refresh();
        Calculator calculator = (Calculator) context.getBean("calculator");
        calculator.makeAnOperation();
    }

How it is working?

1. We are declaring two components (@Component) 'Adder' and 'Calculator'. It is is very important to note that the name of the component is between ("....") just next to the @Component annotation.

2. We are declaring one autowired method in Calculator.java. With it, you don't have to instanciate manually an Adder. just use it. :)

About these ads