我想通过自定义 jar 提供默认 bean。仅当用户实现特定的abstract
应该跳过默认 bean 注入。
以下设置已经可以正常工作,除了一件事:default
有线类有null
!我可能会缺少什么?
@Configration
public class AppConfig {
//use the default service if the user does not provide an own implementation
@Bean
@Conditional(MissingServiceBean.class)
public MyService myService() {
return new MyService() {};
}
}
@Component
public abstract class MyService {
@Autowired
private SomeOtherService other;
//default impl of the method, that may be overridden
public void run() {
System.out.println(other); //null! Why?
}
}
public class MissingServiceBean implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return context.getBeanFactory().getBeansOfType(MyService.class).isEmpty();
}
}
The MyService
bean 已创建并且也可以注入。但包含的类为空。
如果我删除@Conditioanl
注释一切都按预期工作。
最简单的可能性是使用@Primary
注解。您定义接口/抽象类并构建默认实现。到这里为止,这就是基本的弹簧自动装配。
现在您可以使用以下命令创建另一个实现@Primary
并使其在应用程序上下文中可用。 Spring 现在将选择自动装配的主要实现。
Spring 4.1+ 中的另一种可能性是自动装配有序的List<Intf>
并询问接口supports(...)
调用以获取您输入的任何参数的当前实现supports
。您给默认实现一个low priority
越详细的优先级越高。像这样,您甚至可以构建更详细的默认行为。我将这种方法用于多种配置,以处理具有默认和特定实现的不同类。
一个例子是在权限评估期间,我们有一个针对基类的默认配置,另一个针对域类的更高配置,以及针对特定域实体的甚至更高的可能配置。权限评估器遍历列表并检查每个实现是否支持该类,并在这种情况下委托给实现。
我这里没有代码,但如果需要的话我可以稍后分享以使其更清楚。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)