我有一个类的层次结构.我想用
@Component标记它们.我试图只标记父类.我希望Spring也将孩子视为组件.但它不会发生.
我尝试使用自定义的@InheritedComponent注释,如here所述.它不起作用.
我写了一个单元测试.它失败了:“没有’bean.inherit.component.Child’类型的限定bean可用”. Spring版本是4.3.7.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
@Inherited
public @interface InheritedComponent {}
@InheritedComponent
class Parent { }
class Child extends Parent {
}
@Configuration
@ComponentScan(basePackages = "bean.inherit.component")
class Config {
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class InheritComponentTest {
@Autowired
private Child child;
@Test
public void name() throws Exception {
assertNotNull(child);
}
}