如何在不使用xml或注释的情况下使Spring识别bean?

Meh*_*hdi 1 java xml spring annotations javabeans

我正在尝试将Spring框架集成到包含数千个pojos的现有项目中。
编写xml配置文件或遍历每个文件并为类添加注释将是一项艰巨的耗时任务,因此,有没有一种方法可以使Spring扫描程序包并仅基于名称约定来标识bean?

ska*_*man 5

@Component-scanning行为<context:component-scan>只是默认设置。您可以使用基于名称的过滤器来自定义其行为。有关示例,请参见手册的3.10.3节

<beans>

   <context:component-scan base-package="org.example">
      <context:include-filter type="regex" expression=".*Stub.*Repository"/>
      <context:exclude-filter type="annotation"
                              expression="org.springframework.stereotype.Repository"/>
   </context:component-scan>

</beans>
Run Code Online (Sandbox Code Playgroud)

因此,您可以通过类的命名约定使它检测到您的bean。