首页 > 其他分享 >@Repository相关注解

@Repository相关注解

时间:2022-11-30 10:35:57浏览次数:62  
标签:Repository Spring 访问 Bean 相关 注解 异常

@Repository

@Repository注解修饰哪个类,则表明这个类具有对对象进行CRUD(增删改查)的功能,而且@Repository是@Component注解的一个派生品,所以被@Repository注解的类可以自动的被@ComponentScan 通过路径扫描给找到,这也在一定程度上解释了,为什么被@Repository注解的类也能@Autowired;

Spring的注解形式:

@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。
@Repository和@Controller、@Service、@Component的作用差不多,都是把对象交给spring管理。
@Repository用在持久层的接口上,这个注解是将接口的一个实现类交给spring管理。

@Repository的作用:

这是因为该注解的作用不只是将类识别为Bean,同时它还能将所标注的类中抛出的数据访问异常封装为 Spring 的数据访问异常类型。
Spring本身提供了一个丰富的并且是与具体的数据访问技术无关的数据访问异常结构,用于封装不同的持久层框架抛出的异常,使得异常独立于底层的框架。

@repository需要在Spring中配置扫描包地址,然后生成dao层的bean,之后被注入到ServiceImpl中。

标签:Repository,Spring,访问,Bean,相关,注解,异常
From: https://www.cnblogs.com/imagine-xw/p/16937653.html

相关文章