spring注解:@Repository、@Service、@Controller
来源:互联网 发布:linux怎么开机进入系统 编辑:程序博客网 时间:2024/06/07 00:47
spring注解形式中:
@Repository对应存储层(数据层)Bean
@Service对应业务层Bean
@Controller对应展示层Bean
为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,需要在 XML 配置文件中启用Bean 的自动扫描功能,这可以通过<context:component-scan/>实现。如下所示:
// 首先使用 @Repository 将 DAO 类声明为 Bean package bookstore.dao; @Repository public class UserDaoImpl implements UserDao{ …… } // 其次,在 XML 配置文件中启动 Spring 的自动扫描功能 <beans … > …… <context:component-scan base-package=”bookstore.dao” /> …… </beans>
如此,我们就不再需要在 XML 中显式使用 <bean/> 进行Bean 的配置。Spring 在容器初始化时将自动扫描 base-package 指定的包及其子包下的所有 class文件,所有标注了 @Repository 的类都将被注册为 Spring Bean。
为什么 @Repository 只能标注在 DAO 类上呢?这是因为该注解的作用不只是将类识别为Bean,同时它还能将所标注的类中抛出的数据访问异常封装为 Spring 的数据访问异常类型。 Spring本身提供了一个丰富的并且是与具体的数据访问技术无关的数据访问异常结构,用于封装不同的持久层框架抛出的异常,使得异常独立于底层的框架。
Spring 2.5 在 @Repository的基础上增加了功能类似的额外三个注解:@Component、@Service、@Constroller,它们分别用于软件系统的不同层次:
- @Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
- @Service 通常作用在业务层,但是目前该功能与 @Component 相同。
- @Constroller 通常作用在控制层,但是目前该功能与 @Component 相同。
另外,除了上面的四个注解外,用户可以创建自定义的注解,然后在注解上标注 @Component,那么,该自定义注解便具有了与所@Component 相同的功能。不过这个功能并不常用。
更过内容可以参照;点击打开链接
0 0
- Spring注解@Repository、@Service、@Controller
- spring注解:@Repository、@Service、@Controller
- Spring注解@Component、@Repository、@Service、@Controller区别
- Spring注解@Component、@Repository、@Service、@Controller区别
- Spring 注解@Component,@Service,@Controller,@Repository
- Spring 注解@Component,@Service,@Controller,@Repository
- Spring注解@Component、@Repository、@Service、@Controller区别
- Spring注解@Component、@Repository、@Service、@Controller区别
- Spring注解@Component、@Repository、@Service、@Controller区别
- Spring注解@Component、@Repository、@Service、@Controller区别
- Spring注解@Componen、@Repository@Service、@Controller区别
- Spring注解@Component、@Repository、@Service、@Controller
- Spring注解@Component、@Repository、@Service、@Controller
- Spring注解@Component、@Repository、@Service、@Controller区别
- Spring注解@Component、@Repository、@Service、@Controller区别
- (转载)Spring 注解@Component,@Service,@Controller,@Repository
- Spring 注解@Component,@Service,@Controller,@Repository
- Spring 注解@Component,@Service,@Controller,@Repository
- RabbitMQ学习(六)之远程过程调用(RPC)(java)
- WebView
- rbenv 管理多个版本Ruby
- Java中位运算(移位、位与、或、异或、非)
- cannot Go to definition in visual studio code
- spring注解:@Repository、@Service、@Controller
- TextField控件详解
- angularjs系列之轻松使用$q进行异步编程
- JS正则表达式的RegExp对象和括号的使用等需要注意的
- C#、filestream、streamwriter,html方式生成excel
- mac连接网络以后网络不好用
- 键盘样式
- 使用JXL 模板导出数据
- 大数据服务入口