gpt4 book ai didi

java - spring 核心中的 Autowiring 冲突与 xml 配置

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:11:18 25 4
gpt4 key购买 nike

以帖子Spring @Autowired and @Qualifier为引用

我们有这个例子来解决 Autowiring 冲突:

public interface Vehicle {
public void start();
public void stop();
}

有两个 bean ,CarBike工具 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> 我需要更多的解释。

最佳答案

How can I fix this issue just using the configuration in application context?

您可以像下面这样使用 qualifier 标签(参见 https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers )

<context:annotation-config/>
<beans>
<bean class="your_pkg_route.Vehicle">
<qualifier value="bike"/>
</bean>
</beans>
</context:annotation-config>

I found people talking about autowire attribute in the bean declaration and I need more explanation about it

使用注释

@Autowired 用于 bean 声明方法,通过(另一个)声明的 bean 注入(inject)定义的依赖项。现在,如果您的依赖项与您的应用程序位于相同的上下文中,则您根本不需要使用 @Autowired 注释,因为 Spring 能够自行找出它们。因此,如果您的依赖项在您的应用上下文之外,那么您可以使用它。

例如,引用下面的代码:

@Autowired
@Bean
public MyBean getMybean(Dependency1 depdency1, Dependency2 depdency2) {
return new MyBean(depdency1.getSomeStuff(), depdency2.getSomeOtherStuff());
}

在这里,@Autowired 将找到 Dependency1Dependency2 的实例,并将提供它们用于创建 MyBean 的实例。

使用xml配置

Pro Spring 5 ... Spring 支持五种 Autowiring 模式。

  • byName :当使用 byName Autowiring 时,Spring 尝试将每个属性装配到同名的 bean。因此,如果目标 bean 具有名为 foo 的属性,并且在 foo 中定义了 ApplicationContext bean,则将 foo bean 分配给目标的 foo 属性。
  • byType :当使用 byType Autowiring 时,Spring 尝试连接每个通过自动使用相同类型的 bean 在目标 bean 上的属性 ApplicationContext
  • constructor :这个函数就像 byType 接线一样,除了它使用构造函数而不是 setter 来执行注入(inject)。 Spring 尝试在构造函数中匹配最大数量的参数。因此,如果您的 bean 有两个构造函数,一个接受 String ,另一个接受 StringInteger ,并且您的 String 中同时有一个 Integer 和一个 ApplicationContext bean,则 Spring 使用双参数构造函数。
  • default :Spring 将在 constructorbyType 模式之间进行选择自动地。如果您的 bean 具有默认(无参数)构造函数,则 Spring 使用 byType ;否则,它使用构造函数。
  • no : 这是默认值

所以,在你的情况下你需要做这样的事情(但是,我不推荐它。为什么?,你需要将 Vehicle 类声明为一个 bean 和一个不正确的组件,参见 Spring: @Component versus @Bean 。在另一方面,我不确定您是否可以仅将其声明为 bean 来使用它):

// xml config
<context:annotation-config/>
<beans>

// use the primary tag here too! in order to say this the primary bean
// this only works when there are only two implementations of the same interface
<bean id="bike" primary="true" class="your_pkg_route.Bike"/>
<bean id="car" class="your_pkg_route.Car"/>

<bean autowire="byName" class="your_pkg_route.VehicleService"/>

<beans>
</context:annotation-config>

// VehicleService
@Component
public class VehicleService {

private Vehicle bike; // call attribute 'bike' so it is autowired by its name

public void service() {
//...
}
}

如您所见,尝试使用 xml 配置执行此操作会很复杂,因此我建议您尽可能使用注释选项。

相关帖子:

PS:我没有测试任何发布的代码。

关于java - spring 核心中的 Autowiring 冲突与 xml 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55572845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com