Spring中@Component注解,@Controller注解详解
在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成
Service层,Controller层等等的注入配置.使用过程中,在Service层中的实现类头上加@Component注解,在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开发的过程中,我个人更倾向于使用注解的方式,减少配置文件代码。
文章转载至:https://www.cnblogs.com/zivxiaowei/p/3489144.html
Spring中@Component注解,@Controller注解详解的更多相关文章
- Spring中@Resource、@controller注解的含义
@Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象. Spring确实支 ...
- Spring中的循环依赖解决详解
前言 说起Spring中循环依赖的解决办法,相信很多园友们都或多或少的知道一些,但当真的要详细说明的时候,可能又没法一下将它讲清楚.本文就试着尽自己所能,对此做出一个较详细的解读.另,需注意一点,下文 ...
- Spring中@Value标签的使用详解
1.@Value标签 由于Spring对通过IOC的方式对对象进行统一管理,所以对任何对象而言,其生成方法均由Spring管理.传统的方法是通过XML配置每一个Bean,并对这个Bean的所有Fiel ...
- Spring中配置文件applicationContext.xml配置详解
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Spring中的ApplicationListener的使用详解案例
本文链接:https://blog.csdn.net/u010963948/article/details/83507185 1.ApplicationContext Spring的核心,Contex ...
- Spring中bean的作用域scope详解
参考文献:http://blog.csdn.net/jacklearntech/article/details/40157861 http://www.cnblogs.com/qq78292959/p ...
- spring中Bean的注入参数详解
字面值 一般指可用字符串表示的值,这些值可以通过<value>元素标签进行注入.在默认情况下,基本数据类型及其封装类.String等类型都可以采取字面值注入的方式,Spring容器在 ...
- Spring中@Component注解,@Controller注解详解
在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...
- Spring中注解的使用详解
一:@Rsource注解的使用规则 1.1.案例演示 Spring的主配置文件:applicationContext.xml(因为我这里将会讲到很多模块,所以我用一个主配置文件去加载各个模块的配置文件 ...
随机推荐
- webmagic保存数据
使用多线程:
- Python进阶8---面向对象基础1
面向对象 语言的分类 Python的类 定义 class ClassName: pass class MyCalss: """A example class"& ...
- 从JS的深拷贝与浅拷贝到jq的$.extend()方法
一.堆内存与栈内存 堆和栈都是内存中划分出来的用来存储的区域,栈为自动分配的内存空间,它由系统自动释放,堆为动态分配的内存,大小不定也不会自动释放. 二.js基本数据类型与引用类型的不同 基本数据类型 ...
- Async/Await 学习与示例
参考:Async/await学习 es 7 提供了对 promise 对象的更好的操作,省去了很多丧心病狂的链式异步请求,promise 是回调地狱的福音,而 Async/Await 则是 promi ...
- codeforces675D
Tree Construction CodeForces - 675D During the programming classes Vasya was assigned a difficult pr ...
- YApi二次开发环境部署
YApi 是去哪儿网大前端技术中心开源的一个可视化的接口管理平台.下面是在CentOS7下搭建二次开发环境步骤. 1 安装MongoDB 具体安装参考CentOS7安装MongoDB 2 安装Node ...
- hihoCoder #1954 : 压缩树(虚树)
题意 有一棵 \(n\) 个节点且以 \(1\) 为根的树,把它复制成 \(m\) 个版本,有 \(q\) 次操作,每次对 \([l, r]\) 这些版本的 \(v\) 节点到根的路径收缩起来. 收缩 ...
- $.ajax居然触发popstate事件?
我使用$.ajax用来实现一个搜索效果 近段时间因为苹果上微信浏览器的不知明原因需要处理返回事件,因此加多了popstate事件监听用来分别处理苹果跟安卓的返回. 可是居然影响到了我前面的ajax搜索 ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online J - Press the Button(思维)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4056 题意 有一个按钮.一个灯.一个计时器和一个计数器,每按一次按钮,计时 ...
- 1. Ansible 简介
目录 1. Ansible 是什么? 2. Ansible 特性 3. 控制主机需求 4. 被管理节点需求 1. Ansible 是什么? Ansible 是一个配置管理系统(configuratio ...