【发布时间】:2019-08-29 13:38:37
【问题描述】:
以Spring @Autowired and @Qualifier的帖子为参考
我们有这个例子来解决自动装配冲突:
public interface Vehicle {
public void start();
public void stop();
}
有两个 bean,Car 和 Bike 实现了 Vehicle 接口。
@Component(value="car")
public class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car started");
}
@Override
public void stop() {
System.out.println("Car stopped");
}
}
@Component(value="bike")
public class Bike implements Vehicle {
@Override
public void start() {
System.out.println("Bike started");
}
@Override
public void stop() {
System.out.println("Bike stopped");
}
}
@Component
public class VehicleService {
@Autowired
@Qualifier("bike")
private Vehicle vehicle;
public void service() {
vehicle.start();
vehicle.stop();
}
}
这是解决这个问题的一个很好的例子。
但是当我遇到同样的问题但在应用程序上下文中没有这些应答器时:
<context:component-scan></context:component-scan>
<context:annotation-config></context:annotation-config>
使用@Qualifier注解解决了所有问题,但在我的情况下,我们不使用允许使用注解的应答器。
问题是:
如何仅使用应用程序上下文中的配置解决此问题,仅此而已,不使用注释?
我搜索了很多 I found people talking about autowire attribute in the bean declaration <bean id="dao" class="package.IDao" autowire="byName"></bean>,我需要更多关于它的解释。
【问题讨论】:
标签: java spring dependency-injection