How to use Spring @Component, @Repository, @Service and @Controller Annotations?
2015-10-09 18:28
597 查看
In spring
autowiring concepts, we learned about
that it handles only wiring. You still have to define the beans themselves so the container is aware of them and can inject them for you. But with
in place and after enabling automatic component scanning, spring will automatically import the beans into the container so you don’t have to define them explicitly with XML. These annotations are called Stereotype
annotations as well.
Before jumping to example use of these annotations, let’s learn quick facts about these annotations which will help you in making a better decision about when to use which annotation.
1) The
marks a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context. To use this annotation, apply it over class as below:
2) Although above use of
good enough but you can use more suitable annotation that provides additional benefits specifically for DAOs i.e.
The
is a specialization of the
with similar use and functionality. In addition to importing the DAOs into the DI container, it also makes the
unchecked exceptions (thrown from DAO methods) eligible for translation into Spring
3) The
is also a specialization of the component annotation. It doesn’t currently provide any additional behavior over the
but it’s a good idea to use
service-layer classes because it specifies intent better. Additionally, tool support and additional behavior
might rely on it in the future.
4)
marks a class as a Spring Web MVC controller. It too is a
so beans marked with it are automatically imported into the DI container. When you add the
to a class, you can use another annotation i.e.
to map URLs to instance methods of a class.
In real life, you will face very rare situations where you will need to use
Most of the time, you will using
be used when your class does not fall into either of three categories i.e. controller, manager and dao.
If you want to define name of the bean with which they will be registered in DI container, you can pass the name in annotation itself
e.g. @Service (“employeeManager”).
Above four annotation will be scanned and configured only when they are scanned by DI container of spring framework. To enable this scanning, you will need to use “context:component-scan”
tag in your
e.g.
The context:component-scan element requires a base-package attribute, which, as its name suggests, specifies
a starting point for a recursive component search. You may not want to give your top package for scanning to spring, so you should declare three component-scan elements,
each with a base-package attribute pointing to a different package.
When component-scan is declared, you no longer need to declare context:annotation-config, because autowiring
is implicitly enabled when component scanning is enabled.
As I already said that you use
over DAO, manager and controller classes. But in real life, at DAO and manager layer we often have separate classes and interfaces. Interface for defining the contract, and classes for defining the implementations of contracts. Where to use these annotations?
Let’s find out.
Always use these annotations over concrete classes; not over interfaces.
Once you have these stereotype annotations on beans, you can directly use bean references defined inside concrete classes. Note the references are of type interfaces. Spring DI container is smart enough to inject the correct instance in this case.
EmployeeDAO.java and EmployeeDAOImpl.java
EmployeeManager.java and EmployeeManagerImpl.java
EmployeeController.java
EmployeeDTO.java
Let’s test the above configuration and annotations:
TestSpringContext.java
Drop me a comment/query if something needs more explanation.
Happy Learning !!
autowiring concepts, we learned about
@Autowiredannotation
that it handles only wiring. You still have to define the beans themselves so the container is aware of them and can inject them for you. But with
@Component,
@Repository,
@Serviceand
@Controllerannotations
in place and after enabling automatic component scanning, spring will automatically import the beans into the container so you don’t have to define them explicitly with XML. These annotations are called Stereotype
annotations as well.
Before jumping to example use of these annotations, let’s learn quick facts about these annotations which will help you in making a better decision about when to use which annotation.
@Component, @Repository, @Service and @Controller annotations
1) The @Componentannotation
marks a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context. To use this annotation, apply it over class as below:
@Componentis
good enough but you can use more suitable annotation that provides additional benefits specifically for DAOs i.e.
@Repositoryannotation.
The
@Repositoryannotation
is a specialization of the
@Componentannotation
with similar use and functionality. In addition to importing the DAOs into the DI container, it also makes the
unchecked exceptions (thrown from DAO methods) eligible for translation into Spring
DataAccessException.
3) The
@Serviceannotation
is also a specialization of the component annotation. It doesn’t currently provide any additional behavior over the
@Componentannotation,
but it’s a good idea to use
@Serviceover
@Componentin
service-layer classes because it specifies intent better. Additionally, tool support and additional behavior
might rely on it in the future.
4)
@Controllerannotation
marks a class as a Spring Web MVC controller. It too is a
@Componentspecialization,
so beans marked with it are automatically imported into the DI container. When you add the
@Controllerannotation
to a class, you can use another annotation i.e.
@RequestMapping;
to map URLs to instance methods of a class.
In real life, you will face very rare situations where you will need to use
@Componentannotation.
Most of the time, you will using
@Repository,
@Serviceand
@Controllerannotations.
@Componentshould
be used when your class does not fall into either of three categories i.e. controller, manager and dao.
If you want to define name of the bean with which they will be registered in DI container, you can pass the name in annotation itself
e.g. @Service (“employeeManager”).
How to enable component scanning
Above four annotation will be scanned and configured only when they are scanned by DI container of spring framework. To enable this scanning, you will need to use “context:component-scan”tag in your
applicationContext.xmlfile.
e.g.
a starting point for a recursive component search. You may not want to give your top package for scanning to spring, so you should declare three component-scan elements,
each with a base-package attribute pointing to a different package.
When component-scan is declared, you no longer need to declare context:annotation-config, because autowiring
is implicitly enabled when component scanning is enabled.
How to use @Component, @Repository, @Service and @Controller Annotations
As I already said that you use @Repository,
@Serviceand
@Controllerannotations
over DAO, manager and controller classes. But in real life, at DAO and manager layer we often have separate classes and interfaces. Interface for defining the contract, and classes for defining the implementations of contracts. Where to use these annotations?
Let’s find out.
Always use these annotations over concrete classes; not over interfaces.
Once you have these stereotype annotations on beans, you can directly use bean references defined inside concrete classes. Note the references are of type interfaces. Spring DI container is smart enough to inject the correct instance in this case.
EmployeeDAO.java and EmployeeDAOImpl.java
TestSpringContext.java
Happy Learning !!
相关文章推荐
- IOS 与JAVA RSA加密解密
- java例子10:javadoc
- eclipse导入别的项目报错问题
- Java无限的求π的近似值
- JavaWeb的开发模型(重要)
- java8 Stream和Lisp对比
- Java开发中的23种设计模式详解(转)
- 2 java jsp+servelt入门实例
- 1 java dynamic web project 没有web.xml
- myeclipse跟eclipse中使用github做版本控制工具
- 0 java连接数据库
- java集合框架详解
- 使用 Spring 3 来创建 RESTful Web Services
- Java关键字final、static使用总结
- Json字符串转换为java对象的各种实现方法【json_lib框架、Gson、org.json】
- MyEclipse中ctrl+F组合解决find查询相关问题
- Struts2之自定义局部类型转换器、全局类型转换器
- Struts2之自定义局部类型转换器、全局类型转换器
- eclipse jee中检出项目并转换为Maven project
- mysql 数据库连接池的 java 实现