gpt4 book ai didi

java - 在 Spring 中传递 Bean 依赖项的最佳方式是什么

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:13:44 24 4
gpt4 key购买 nike

在阅读 Spring 框架的文档时,我偶然发现了两种不同的在工厂方法中传递 Bean 依赖项的方式。第一种是这样的(直接使用依赖的工厂方法):

@Configuration
public class MessagingConfiguration {

@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
}

@Bean
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(connectionFactory());
}

}

第二个看起来像这样(将依赖项作为参数注入(inject)工厂方法):

@Configuration
public class MessagingConfiguration {

@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
}

@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
}

}

我想知道这两种解决方案的优缺点以及哪一种更可取?

最佳答案

在正常使用情况下,这两种方法之间不应该有如此大的差异。但是,如果您想将配置拆分为多个类,则不能简单地使用第一种解决方案。假设您想将两个@Bean 方法放入单独的类中,并通过@Import 将第一个方法导入第二个方法,编译器就没有机会知道 RabbitTemplate(connectionFactory( )) 第二类的构造函数。所以你会遇到编译器错误。要解决此问题,您可以使用 spring 文档推荐的第二种方法:Injecting dependencies on imported @Bean definitions

Injecting dependencies on imported @Bean definitions

The example above works, but is simplistic. In most practical scenarios, beans will have dependencies on one another across configuration classes. When using XML, this is not an issue, per se, because there is no compiler involved, and one can simply declare ref="someBean" and trust that Spring will work it out during container initialization. Of course, when using @Configuration classes, the Java compiler places constraints on the configuration model, in that references to other beans must be valid Java syntax.

Fortunately, solving this problem is simple. As we already discussed, @Bean method can have an arbitrary number of parameters describing the bean dependencies. Let’s consider a more real-world scenario with several @Configuration classes, each depending on beans declared in the others:

@Configuration
public class ServiceConfig {

@Bean
public TransferService transferService(AccountRepository accountRepository) {
return new TransferServiceImpl(accountRepository);
}

}

@Configuration
public class RepositoryConfig {

@Bean
public AccountRepository accountRepository(DataSource dataSource) {
return new JdbcAccountRepository(dataSource);
}

}

@Configuration
@Import({ServiceConfig.class, RepositoryConfig.class})
public class SystemTestConfig {

@Bean
public DataSource dataSource() {
// return new DataSource
}

}

public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class);
// everything wires up across configuration classes...
TransferService transferService = ctx.getBean(TransferService.class);
transferService.transfer(100.00, "A123", "C456");
}

关于java - 在 Spring 中传递 Bean 依赖项的最佳方式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39778265/

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