Spring中@Component注解,@Controller注解详解
来源:互联网 发布:网络玄幻小说合集32 编辑:程序博客网 时间:2024/06/05 19:09
在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成
Service层,Controller层等等的注入配置.使用过程中,在Service层中的实现类头上加@Compopnet注解,在Controller类头加@Controller注解,便完成了配置。例如在
Controller中当我们调用某个Service时就不需要Set方法了,直接通过@Autowried 注解对Service对象进行注解即可:例如
在Controller中:
@Controller
@RequestMapping("/test")public class ExampleController { @Autowired private ExampleService service; }
在Service中
@Component public class ExampleServiceImpl Implements ExampleService { @Autowired private ExampleDao exampleDao; }
Spring 中的XML配置:
<!-- 自动扫描service,controller组件 --><context:component-scan base-package="com.example.service.*"/><context:component-scan base-package="com.example.controller.*"/>
通常,在Bean为添加@Component注解的情况下,在启动服务时,服务会提前报出以下代码中这样的异常情况下,此时应该检查相应Bean是否正确添加@Component
注解,而在Controller层中未配置@Controller的情况,启动时服务可能不会爆出异常,但是你会发现页面请求中的URL地址是正确的,当时无论如何也访问不到Controller中相
对应的方法,这个时候就需要那么需要检查@Controller注解和@RequestMapping注解是否已经添加到Class上面了。
org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'example'No matching bean of type [com.example.ExampleService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
下面就详细介绍下@Component,@Controller注解:
- org.springframework.stereotype.Component (implements java.lang.annotation.Annotation)
在自动服务,spring初始化的时候,spring会把所有添加@Component注解的类作为使用自动扫描注入配置路径下的备选对象,同时在初始化spring@Autowired
注解相应的Bean时,@Autowired标签会自动寻找相应的备选对象完成对bean的注入工作。
- org.springframework.stereotype.Controller (implements java.lang.annotation.Annotation)
@Controller注解是一个特殊的Component,它允许了实现类可以通过扫描类配置路径的方式完成自动注入,通常@Controller是结合@RequestMapping注解一起使用的。
结语:
通过了解Spring的注解可以帮助我们在使用Spring开发过程中提高开发效率,同时也加强了我们对Spring的认识。在使用Spring开发的过程中,我个人更倾向于使用注解的方式,减少配置文件代码。
作者:Ziv小威
出处:http://www.cnblogs.com/zivxiaowei/
关于作者:专注于Java技术的程序员一枚,此外对JS开发保持着较高的兴趣。爱好音乐,阅读,FM等等。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
如有问题,可以邮件:wewoor@foxmail.com
微博:Ziv小威
- Spring中@Component注解,@Controller注解详解
- Spring中@Component注解,@Controller注解详解
- Spring中@component注解
- spring中@Component注解
- Spring注解@Controller 详解
- Spring注解@Controller 详解
- Spring中注解@Controller@Service@Repository@Component的区别
- spring中 Controller, Service , Component, Repository注解说明
- Spring 中@Component @Controller @Service @Repository注解的意思
- 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注解@Component、@Repository、@Service、@Controller
- Dumpbin命令查看软件32位/64位
- IT:从硬件开始
- iOS开发动画(Animation)总结
- php curl详解用法[真的详解]
- 计算型字段的应用
- Spring中@Component注解,@Controller注解详解
- javascript中Array.protototype方法总结
- iOS中百度地图API的总结
- iOS7中UITableView问题以及UITableViewCell的层级关系
- 静态文本框背景透明
- ORACLE 归档日志打开关闭方法
- 【Android】 Android NDK断点失效原因及解决方案
- linux--platform驱动理论和例程
- 从命令行运行codeigniter