Table of Contents
So basically all 4 annotations are used to register bean definition with spring’s application context.
@Component:
@Component is generic annotation for bean definition and registers with application context.
@Service :
@Service is specialised component annotation which is used to annotate classes which belongs to service layer.
@Repository :
@Repository annotation is specialised component annotation which is used to annotate classes at DAO layer. It also makes unchecked exception eligible for conversion into Spring DataAccessException.
@Controller :
@ Controller annotation is specialised component annotation which is used to annotate classes at Presentation layer. It is widely used in Spring MVC applications.
You should not use @Component annotation unless you are sure that it does not belong to @Service, @Repository and @Controller annotation.
Enable component Scanning :
All these annotations will work only when you use context:component-scan in applicationcontext.xml. It basically scans for above 4 annotated classes and registers bean with Spring application context.
1 2 3 |
<context:component-scan base-package="org.arpit.java2blog" /> |
Spring annotations example :
In this example,we are going to create country object with help of controller, service and Dao classes by using above annotations.
Lets first create our bean class
Country.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package org.arpit.java2blog.bean; public class Country{ String countryName; long population; public Country() { super(); } public Country(String countryName,long population) { super(); this.countryName = countryName; this.population=population; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog.controller; import org.arpit.java2blog.bean.Country; import org.arpit.java2blog.service.CountryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller ("countryController") public class CountryController { @Autowired CountryService countryService; public Country createNewCountry() { return countryService.createNewCountry(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog.service; import org.arpit.java2blog.bean.Country; import org.arpit.java2blog.dao.CountryDAO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service("countryService") public class CountryService { @Autowired CountryDAO countryDAO; public Country createNewCountry() { return countryDAO.createNewCountry(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog.dao; import org.arpit.java2blog.bean.Country; import org.springframework.stereotype.Repository; @Repository("countryDAO") public class CountryDAO { public Country createNewCountry() { // You should get it from database Country country = new Country("Ïndia", 40000); return country; } } |
1 2 3 4 5 6 7 8 9 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="org.arpit.java2blog" /> </beans> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog.main; import org.arpit.java2blog.bean.Country; import org.arpit.java2blog.controller.CountryController; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringApplicationMain { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); CountryController controller = (CountryController) context.getBean("countryController"); Country country = controller.createNewCountry(); System.out.println("Country Name : " + country.getCountryName()); System.out.println("Country's Population : " + country.getPopulation()); } } |
1 2 3 4 5 6 7 8 |
Aug 08, 2016 12:00:42 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c2709da: startup date [Mon Aug 08 00:00:42 IST 2016]; root of context hierarchy Aug 08, 2016 12:00:42 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [applicationContext.xml] Country Name : Ïndia Country's Population : 40000 |