Spring中@Component注解,@Controller注解详解
在使用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开发的过程中,我个人更倾向于使用注解的方式,减少配置文件代码。
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(因为我这里将会讲到很多模块,所以我用一个主配置文件去加载各个模块的配置文件 ...
随机推荐
- Mongodb 字段类型转换
db.diningmembers.find({modifedDate:{$type:9}}).forEach(function(x){x.tel = String(x.tel);db.diningme ...
- private成员变量真的私有吗?(用指针刨他祖坟)
今天写程序时突然想到的,为什么不用指针去获取类的成员变量呢.于是做了这个实验.首先定义了一个类: class Test { private: int i; char c; int* p; public ...
- 4412开发板Android教程——Android平台简介
本文转自迅为开发板论坛:http://www.topeetboard.com Android和IOS Android的历史 Android公司 2005年Google收购成立22个月的Android公 ...
- Webshell实现与隐藏探究
一.什么是webshell webshell简介 webshell,顾名思义:web指的是在web服务器上,而shell是用脚本语言编写的脚本程序,webshell就是就是web的一个管理 工具,可以 ...
- 对于大学4年的反思(续),记我的ThoughtWorks面试
之前我写了一篇对于大学四年的反思,时隔一个月,为什么我这么快就要来写这篇续章呢?主要有两个原因,第一是感谢静子姐姐,记得知乎上有个回答里面说过人生需要有贵人的帮助,遇到贵人是一件很幸运的事情.我想,静 ...
- 深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12766 Accepted: 4722 D ...
- POJ 1269 Intersecting Lines【判断直线相交】
题意:给两条直线,判断相交,重合或者平行 思路:判断重合可以用叉积,平行用斜率,其他情况即为相交. 求交点: 这里也用到叉积的原理.假设交点为p0(x0,y0).则有: (p1-p0)X(p2-p0) ...
- Redis 一二事 - 在spring中使用jedis 连接调试单机redis以及集群redis
Redis真是好,其中的键值用起来真心强大啊有木有, 之前的文章讲过搭建了redis集群 那么咋们该如何调用单机版的redis以及集群版的redis来使用缓存服务呢? 先讲讲单机版的,单机版redis ...
- Unity CombineChildren和MeshCombineUtility
原理 Unity3D如何通过CombineChildren和MeshCombineUtility优化场景? 首先解释下联结的原理和意思:文档里说,显卡对于一个含100个面片的物体的和含1500个面片的 ...
- [ORACLE错误]ORA-00054:resource busy and acquire with nowait specified解决方法
当某个数据库用户在数据库中插入.更新.删除一个表的数据,或者增加一个表的主键时或者表的索引时,常常会出现ora-00054:resource busy and acquire with nowait ...