Spring/Spring-Boot 学习 @Controller,@Component,@Service,@Repository的异同

本文转自

Spring典型注解-@Controller,@Component,@Service,@Repository的异同
spring常用注解-@Component, @Service, @Repository,@Controller,@Autowired,@Qualifier,@Scope

相同点:

@Controller@Service@Repository都有带@Component父注解,四个注解都可以说成是Component级别的注解,Spring框架自动扫描的注解也是检测是否有Component注解标记。把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>。这三个注解它们除了@Component的属性外还有其他的的场景应用。

@Repository

这个注解用来标识这个类是用来直接访问数据库的,dao层使用@repository注解。如@Repository(value="goodsDao")注解是告诉Spring,让Spring创建一个名字叫goodsDaoGoodsDaoImpl实例。当Service需要使用Spring创建的名字叫goodsDaoGoodsDaoImpl实例时,就可以使用@Resource(name = "goodsDao")注解告诉Spring,Spring把创建好的goodsDao注入给Service即可。
注解在数据访问层Bean,还可以将数据库操作抛出的原生异常翻译转化为spring的持久层异常。 举例:
@Repository(value="goodsDao")注解是告诉Spring,让Spring创建一个名字叫goodsDaoGoodsDaoImpl实例。

@Repository(value="goodsDao")
public class GoodsDaoImpl extends BasicDaoImpl implements GoodsDao {
}

Service需要使用Spring创建的名字叫goodsDaoGoodsDaoImpl实例时,就可以使用@Resource(name = "goodsDao")注解告诉Spring,Spring会自动把goodsDao创建好然后注入给goodsDao

@Resource(name = "goodsDao")
private GoodsDao goodsDao;

@Service

应用的业务逻辑通常在service层实现,因为我们一般使用@Service注解标记这个类属于业务逻辑层。如@Service("goodsService")注解是告诉spring,当Spring要创建GoodsServiceImpl的的实例时,bean的名字必须叫做"goodsService",这样当Action需要使用GoodsServiceImpl的的实例时,就可以由Spring创建好的"goodsService",然后注入给Action。
@Service("goodsService")注解是告诉Spring,让Spring创建一个名字叫goodsServiceGoodsServiceImpl实例。

@Service("goodsService")
   public class GoodsServiceImpl implements GoodsService {
}

Controller需要使用GoodsServiceImpl的实例时,只要在Controller中声明@Resource(name = "goodsService")来接收由Spring注入的goodsService,然后Spring会自动创建好goodsService,注入给Controller

@Resource(name = "goodsService")
private GoodsService goodsService;

@Controller

这个注解主要告诉Spring这个类作为控制器,可以看做标记为暴露给前端的入口。@Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法。通俗来说,被Controller标记的类就是一个控制器,这个类中的方法,就是相应的动作。@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
使用@Controller注解标识GoodsController之后,就表示要把GoodsController交给Spring容器管理,在Spring容器中会存在一个名字为goodsControllerController,这个名字是根据GoodsController类名首字母小写来取的(如果@Controller不指定value,则默认bean名称为这个类的类名首字母小写)

@Controller
public class GoodsController {
}

@Component

用来表示一个平常的普通组件,当一个类不合适用以上的注解定义时用这个组件修饰。

总结:

项目小的时候对这些注解没有特别的区分,但项目如果变得越来越大,那么就要划分的细致一些有一定的规则才方便大家的维护。有点像衣服,虽然都是衣服,但是有的是要穿出去开party的,有的是穿出去运动的,有的是休闲的。功能一样,但是用途不是很相同。

  • @Component spring基础的注解,被spring管理的组件或bean
  • @Repository 用于持久层,数据库访问层
  • @Service 用于服务层,处理业务逻辑
  • @Controller 用于呈现层,(spring-mvc)

其他配合注解 @Qualifier, @Scope

@Qualifier

注入指定bean的名称,有两个匹配的bean及以上会用到:

 @Service("service1")
 public class GoodsServiceImpl1 implements GoodsService {
 }
 
 @Service("service2")
 public class GoodsServiceImpl2 implements GoodsService {
 }
 
 @Controller
 public class GoodsController {
   @Autowired
   @Qualifier("service1")
   GoodsService goodsService;
 }

@Scope

Spring默认产生的bean是单例(即singleton)的,如果设置为prototype表示原型即每次都会new一个新的出来。

  @Controller
  @Scope("prototype")
      public class GoodsController {
  }
posted @ 2019-11-22 10:57  lllunaticer  阅读(564)  评论(0编辑  收藏  举报