在使用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注解:

  在自动服务,spring初始化的时候,spring会把所有添加@Component注解的类作为使用自动扫描注入配置路径下的备选对象,同时在初始化spring@Autowired

注解相应的Bean时,@Autowired标签会自动寻找相应的备选对象完成对bean的注入工作。

  @Controller注解是一个特殊的Component,它允许了实现类可以通过扫描类配置路径的方式完成自动注入,通常@Controller是结合@RequestMapping注解一起使用的。

结语:

   通过了解Spring的注解可以帮助我们在使用Spring开发过程中提高开发效率,同时也加强了我们对Spring的认识。在使用Spring开发的过程中,我个人更倾向于使用注解的方式,减少配置文件代码。

Spring中@Component注解,@Controller注解详解的更多相关文章

  1. Spring中@Resource、@controller注解的含义

    @Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象. Spring确实支 ...

  2. Spring中的循环依赖解决详解

    前言 说起Spring中循环依赖的解决办法,相信很多园友们都或多或少的知道一些,但当真的要详细说明的时候,可能又没法一下将它讲清楚.本文就试着尽自己所能,对此做出一个较详细的解读.另,需注意一点,下文 ...

  3. Spring中@Value标签的使用详解

    1.@Value标签 由于Spring对通过IOC的方式对对象进行统一管理,所以对任何对象而言,其生成方法均由Spring管理.传统的方法是通过XML配置每一个Bean,并对这个Bean的所有Fiel ...

  4. Spring中配置文件applicationContext.xml配置详解

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  5. Spring中的ApplicationListener的使用详解案例

    本文链接:https://blog.csdn.net/u010963948/article/details/83507185 1.ApplicationContext Spring的核心,Contex ...

  6. Spring中bean的作用域scope详解

    参考文献:http://blog.csdn.net/jacklearntech/article/details/40157861 http://www.cnblogs.com/qq78292959/p ...

  7. spring中Bean的注入参数详解

    字面值    一般指可用字符串表示的值,这些值可以通过<value>元素标签进行注入.在默认情况下,基本数据类型及其封装类.String等类型都可以采取字面值注入的方式,Spring容器在 ...

  8. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  9. Spring中注解的使用详解

    一:@Rsource注解的使用规则 1.1.案例演示 Spring的主配置文件:applicationContext.xml(因为我这里将会讲到很多模块,所以我用一个主配置文件去加载各个模块的配置文件 ...

随机推荐

  1. Python UnicodeDecodeError

    出于对goagent的兴趣,看了python,后来又想了解一下gae,于是就按照gae python创建hello world应用程序,可是一开始就遇到这样一个问题: UnicodeDecodeErr ...

  2. 分享jstl实现分页,类似百度分页

    <c:if test=" ${requestScope.curPage <= 0}"> </ c:if> < c:if test=" ...

  3. Effective Java 75 Consider using a custom serialized form

    Principle Do not accept the default serialized form without first considering whether it is appropri ...

  4. SQL 相关

    SET STATISTICS TIME ON 记录查询的相关数据 生成随机Guid SELECT NewID() 按照某一列排序并生成序号 select Row_Number() OVER (ORDE ...

  5. 修复 Java 内存模型,第 1 部分——Brian Goetz

    转自Java并发大师Brain Goetz:http://www.ibm.com/developerworks/cn/java/j-jtp02244/ (中文地址) http://www.ibm.co ...

  6. AngularJS的一点学习笔记

    ng-options="item.action for item in todos" ng-options表达式的基本形式, 形如 "<标签> for < ...

  7. sphinx增量索引

    首先建立一个计数表,保存数据表的最新记录ID CREATE TABLE `sph_counter` (  `id` int(11) unsigned NOT NULL,  `max_id` int(1 ...

  8. Linux Commands intro1

    $((expression)) echo $(2+2) :wrong echo $((2+2))  : right echo Front-{A,B,C}-Back Front-A-Back Front ...

  9. Varchar2 size how to decide?

    When you execute a complicate store procedure, maybe it will execute a long time, maybe you want to ...

  10. ORA-01034: ORACLE not available如何解决

    一个小小的问题,让我折腾了一个上午,下午三点彻底解决了,分享一个给大家解决方法,尽管在测试服务器上,但是经验是值得总结和分享的. ERROR:ORA-01034: ORACLE not availab ...