When we add an annotation to a class, then Spring will actually scan our java classes for those annotations. When it finds a class that has a special Spring Annotation on it, it will automatically register that bean with the Spring Container.
So instead of doing everything long hand via XML config file, Spring will just scan and register beans with the Spring Container. These are two main points in scanning for component classes in Spring Framework.
Now, let's use the tag <context:component-scan> and property base-package with the value is the package that we want Spring Framework will scan.
We need to add the @Component annotation into our java classes, so with this annotation, we tell to the Spring that this class should be registered when it starts scanning.
When we use the @Component annotation we can provide it any bean id name that we want to use. See example below.
EnglishCoach.java
1 2 3 4 5 6 7 8 910111213
packagecom.spring.core.spring.inversion.of.control.annotation;importorg.springframework.stereotype.Component;@Component("englishCoach")publicclassEnglishCoachimplementsCoach{@OverridepublicStringgetDailyHomeWork(){return"Spend 1 hour to practise Speaking Skill!";}}
By default, if we don't provide any bean id name, then the Spring Framework will set the bean id automatically by class name with the first lower case character. Ex: EnglishCoach class -> the bean id will be englishCoach.
In this step, we will do the same what we did before in retrieving beans from container. See the example below:
12345
// create a spring containerClassPathXmlApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext.xml");//retrieve bean from spring containerEnglishCoachenglishCoach=context.getBean("englishCoach",Coach.class);
As you can see, we will use the tag <context:component-scan> to tell Spring Framework that we want Spring Framework scans our package and the property base-package is used to tell Spring Framework that where it should do the scanning and register the beans.
packagecom.spring.core.spring.inversion.of.control.annotation;importorg.springframework.stereotype.Component;@Component("englishCoach")publicclassEnglishCoachimplementsCoach{@OverridepublicStringgetDailyHomeWork(){return"Spend 1 hour to practise Speaking Skill!";}}
As you can see, in the implement class EnglishCoach. We will use the annotation @Component with value is englishCoach which will tell Spring Framework that, this class is a bean with the bean id is englishCoach and Spring Framework should register it during the scanning.
Next, we can use the bean in the Spring Container by using getBean() method.
SpringApplication.java
1 2 3 4 5 6 7 8 9101112131415161718
packagecom.spring.core.spring.inversion.of.control.annotation;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassSpringApplication{publicstaticvoidmain(String[]args){ClassPathXmlApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext.xml");//retrieve bean from the spring containerCoachenglishCoach=context.getBean("englishCoach",Coach.class);//use the beanSystem.out.println(englishCoach.getDailyHomeWork());context.close();}}