我如何twice
bean类@autowire
拥有1(String)参数构造函数,而在Spring Boot应用程序中不使用TransactionManagerImpl
?
即使搜索了很多帖子,如果不使用new
autowire
的任何线索
我需要在三个不同的类中new
autowire
,并且在所有三个类中的参数都不同。
这看起来是非常基本的情况。
TransactionManager
答案 0 :(得分:1)
您要使用@Service注释的地方有任何特定要求吗? 如果没有,则可以使用@Bean为TransactionManagerImpl创建一个如下所示的bean。
@Configuration
public class Config {
@Value("${txnLogFile}")
private String txnLogFile;
@Bean
public TransactionManager transactionManager() {
return new TransactionManagerImpl(txnLogFile);
}
}
并从TransactionManagerImpl中删除@Service注释。
答案 1 :(得分:0)
抛开其他并发症,可以这样做
public TransactionManagerImpl(@Value("${txnLogFile}") String txnLogFile) {
this.txnLogFile= txnLogFile;
}
答案 2 :(得分:0)
最后,我按照以下步骤进行操作,现在确定这是否是最佳方法。我不想仅仅因为一个变量就拥有三个实现。
application.yaml
app:
type-a:
txn-log-file: data/type-a-txn-info.csv
type-b:
txn-log-file: data/type-b-txn-info.csv
default:
txn-log-file: data/default/txn-info.csv
MainApplication.java
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(MainApplication.class).web(WebApplicationType.NONE).run(args);
}
@Bean
public TransactionManager transactionManager(@Value("${app.default.txn-log-file}") String txnLogFile) {
return new TransactionManagerImpl(txnLogFile);
}
@Bean
public CsvService csvService(String txnLogFile) {
return new CsvServiceImpl(txnLogFile);
}
}
TypeOneRoute.java
@Configuration
public class TypeOneRoute extends RouteBuilder {
@Value("${app.type-a.txn-log-file}")
private String txnLogFile;
@Autowired
private ApplicationContext applicationContext;
private TransactionManager transactionManager;
@Override
public void configure() throws Exception {
transactionManager = applicationContext.getBean(TransactionManager.class, txnLogFile);
transactionManager.someOperation();
}
}
TypeTwoRoute.java
@Configuration
public class TypeTwoRoute extends RouteBuilder {
@Value("${app.type-b.txn-log-file}")
private String txnLogFile;
@Autowired
private ApplicationContext applicationContext;
private TransactionManager transactionManager;
@Override
public void configure() throws Exception {
transactionManager = applicationContext.getBean(TransactionManager.class, txnLogFile);
transactionManager.create();
}
}
TransactionManager.java
@Service
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public interface TransactionManager {
public ZonedDateTime create() throws IOException, ParseException;
}
TransactionManagerImpl.java
public class TransactionManagerImpl implements TransactionManager {
@Autowired
private ApplicationContext applicationContext;
private String txnLogFile;
public TransactionManagerImpl(String txnLogFile) {
this.txnLogFile = txnLogFile;
}
private CsvService csvService;
@PostConstruct
public void init() {
csvService = applicationContext.getBean(CsvService.class, txnLogFile);
}
public ZonedDateTime create() throws IOException, ParseException {
try {
csvService.createTxnInfoFile();
return csvService.getLastSuccessfulTxnTimestamp();
} catch (IOException e) {
throw new IOException("Exception occured in getTxnStartDate()", e);
}
}
}
最初,TransactionManager Bean将向app.default.txn-info.csv
注册,当我实际上从ApplicationContext
获取它时,我将值替换为通过传递参数从ApplicationContext
获取Bean的参数。 >