美文网首页SSM
@Component注解的用法

@Component注解的用法

作者: 山不转人自转 | 来源:发表于2019-03-19 10:53 被阅读218次

遇到的问题

踩到一个坑,有一个接口,在这个接口的实现类里,需要用到@Autowired注解,一时大意,没有在实现类上加上@Component注解,导致了Spring报错,找不到这个类

一旦使用关于Spring的注解出现在类里,例如我在实现类中用到了@Autowired注解,被注解的这个类是从Spring容器中取出来的,那调用的实现类也需要被Spring容器管理,加上@Component

@Component("conversionImpl")
//其实默认的spring中的Bean id 为 conversionImpl(首字母小写)
public class ConversionImpl implements Conversion {
    @Autowired
    private RedisClient redisClient;
}

介绍

开发中难免会遇到这个这个注解@Component

@controller 控制器(注入服务)
用于标注控制层,相当于struts中的action层

@service 服务(注入dao)
用于标注服务层,主要用来进行业务的逻辑处理

@repository(实现dao访问)
用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件
.

@component (把普通pojo实例化到spring容器中,相当于配置文件中的

泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。

相关文章

  • @Component注解的用法

    遇到的问题 踩到一个坑,有一个接口,在这个接口的实现类里,需要用到@Autowired注解,一时大意,没有在实现类...

  • 【Spring源码】@Configuration和@Compon

    @Configuration 该注解派生自@Component,和@Component注解有相同的功能 相同点: ...

  • springboot 设置定时器

    使用到的注解:@Component @EnableScheduling@Scheduled() 注解解释: 1、@...

  • @Component

    @Component : 默认扫描注解所在的包及其子包 @Component("package name") : ...

  • @Component VS @Bean

    作用对象不同: @Component 注解作用于类,而 @Bean 注解作用于方法; @Component 通常是...

  • Spring注解大全

    Spring使用的注解大全和解释 注解解释@Controller组合注解(组合了@Component注解),应用在...

  • Component 标签(17)

    一、component标签用法 component标签的用法,动态的根据data值显示不同的组件 二、点击按钮切换...

  • spring boot 装配模式

    模式注解装配 @Controller @Service @Component注解扩展 @Configuration...

  • Spring Boot 总结

    注解 一、org.springframework.stereotype 下的注解与Bean相关@Component...

  • 常用注解@Controller,@Service,@reposi

    SpringMVC常用注解@Controller,@Service,@repository,@Component ...

网友评论

    本文标题:@Component注解的用法

    本文链接:https://www.haomeiwen.com/subject/enlbmqtx.html