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. 🙂
March 10, 2009 at 6:05 pm
Thanks you very much for this article, I really appreciate it because I am very new in Spring and is great to find someone that explains how things works in a very simple way, thanks!.
February 23, 2010 at 1:48 am
Component-Scan automatically detects annotations, so there is no need for
April 6, 2010 at 11:34 am
I appreciate your “neatness” in explaning the issue. Thanks..
February 17, 2011 at 9:05 pm
First off, half your code is cut off and can’t be seen. Secondly, isn’t the whole point of component scanning so you don’t have to write code like
Calculator calculator = (Calculator) context.getBean(“calculator”);
November 8, 2011 at 3:36 pm
I believe you still have to do the Calculator calculator = (Calculator) context.getBean(“calculator”); for you to test the above code..Otherwise how are you gonna get hold of the reference of the Calculator object in your test code. Can you explain please ?
January 8, 2012 at 10:46 am
Wrong, the whole point of (dependency injection) is that the class will be retrieved without having to instantiate it directly. It is “given” to the class rather than created.
Notice there isn’t a “new” statement? That’s the effect.
March 16, 2011 at 7:17 pm
Thanks very much for this article… Clean , to the point… I wish if all the article go in this manner..
April 18, 2011 at 3:40 pm
Thanks for this tutorial. I think that using simple tutorials is very useful and not only for me …
May 5, 2011 at 12:55 pm
Need for what?
June 19, 2011 at 5:52 am
Good article
June 22, 2011 at 2:02 am
cool. Thanks for sharing.
August 8, 2011 at 7:38 am
those who are trying to run this, remove the space after the colon.
xmlns: xsi=”http://www.w3.org/2001/XMLSchema-instance”
October 19, 2011 at 2:19 am
Besides, @Repository also could be used in component scan.
December 16, 2011 at 4:41 pm
context:component-scan has an attribute ‘annotation-config’ true by default so you doesn’t need context:annotation-config
December 28, 2011 at 3:45 pm
Really good Example.. Thanks 🙂
May 2, 2013 at 12:59 pm
Spring annotations is a mesh of redudndancy. I know how hard it is to be as simple and as to the point.
Great job!
October 26, 2014 at 4:22 am
Thanks.. it really helps !!