spring5-基于注解进行组件扫描

  	可以用@Controller @Service @Commpont @Repository 这4个都能进行组件扫描

原文如下:
Spring提供了更多的构造型注解:@Component, @Service, 和@Controller. @Component 可用于管理任何Spring的组件。 @Repository, @Service, 或 @Controller是@Component的特殊化。用于更具体的用例(分别在持久性,服务和表示层中)。 因此,您可以使用@Component注解组件类,但是,通过使用@Repository, @Service, 和 @Controller注解它们,能够让你的类更易于被合适的工具处理或与相应的切面关联。 例如,这些注解可以使目标组件变成切入点。在Spring框架的未来版本中,@Repository, @Service, 和 @Controller也可能带有附加的语义。 因此,如果在使用@Component 或 @Service 来选择服务层时,@Service显然是更好的选择。同理,在持久化层要选择@Repository,它能自动转换异常。

使用方法很简单只要在类上面加上那四个注解就可以了

@Component
public class MyAnnotation {
    public void add(){
        System.out.println("add...");
    }
}

使用xml进行扫描

<!--base-package  扫描的包路径-->
<context:component-scan base-package="com.gq.demo.annotation" ></context:component-scan>


<!--只扫描哪些内容 多个用逗号隔开-->
<context:component-scan base-package="com.gq.demo.annotation" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>


<!--哪些不用扫描-->
<context:component-scan base-package="com.gq.demo.annotation" >
    <context:exclude-filter  type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值