We have upgraded spring context in our application from 4.3.2 to 5.3.13. In the latest spring context bean overriding is restricted by default. So we have enabled the bean overriding by setting the spring.main.allow-bean-definition-overriding=true. By doing so we ran into an issue where the app was unable to create a bean for database connectivity.
If in case we do not use the spring.main.allow-bean-definition-overriding=true, we are running into bean creation issue as follows.
The bean 'connectionFactory', defined in class path resource [com/company/myorg/my/bank/dept/configuration/MQConnectionFactoryConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/company/myorg/by/bank/sale/configuration/MQConfiguration.class] and overriding is disabled.
We have tried renaming the bean too, that did not work. Can someone share ideas on how to resolve this please ?
======= MQConfiguration====
@Configuration
@PropertySource("classpath:application-${IafConfigSuffix:default}.properties")
public class MQConfiguration {
@Setter
private static Logger logger;
@Value("${host-name}") @Setter
private String hostName;
@Value("${port}") @Setter
private int port;
@Value("${channel}") @Setter
private String channel;
@Value("${time-to-live}") @Setter
private int timeToLive;
@Autowired
@Qualifier("connectionFactory") @Setter
ConnectionFactory connectionFactory;
public MQConfiguration() {
logger = getLogger();
}
@Bean(name = "jmsTemplate")
public JmsTemplate provideJmsTemplate() {
==== MQConnectionFactoryConfiguration =======
@Configuration
@PropertySource("classpath:application-${IafConfigSuffix:default}.properties")
public class MQConnectionFactoryConfiguration {
@Setter
private static Logger logger;
@Value("${host-name}") @Setter
private String hostName;
@Value("${port}") @Setter
private int port;
@Value("${channel}") @Setter
private String channel;
public MQConnectionFactoryConfiguration() {
logger = getLogger();
}
** @Bean(name = "connectionFactory")**
public ConnectionFactory buildConnectionFactory() throws JMSException {
=========DataSourceConfiguration=====
@Configuration
public class DataSourceConfiguration {
@Bean(name="db2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db2")
@Primary
public DataSource db2DataSource() {
return getDataSourceBuilder().build();
}
@Bean(name = "jdbcDb2")
public NamedParameterJdbcTemplate jdbcTemplate(@Qualifier("db2DataSource") DataSource datasource {
return new NamedParameterJdbcTemplate(db2DataSource);
}
protected DataSourceBuilder getDataSourceBuilder() {
return DataSourceBuilder.create();
}
We upgraded spring-boot-starter-parent(From 1.4.0 to 2.4.13) , spring-context (from 4.3.2 to 5.3.13), spring-boot-starter (From 1.4.0 to 2.4.13), spring-boot-starter-web and spring-boot-starter-test (From 1.4.0 to 2.4.13), spring-boot-starter-tomcat (From 1.4.0 to 2.4.13), org.codehaus.janino (from 2.7.8 to 3.0.8)
Click here to see changes made to pom file
We tried to set spring.main.allow-bean-definition-overriding=true which caused the datasource to DB failing. We have tried renaming the bean too, that did not work
Expectation is, someone to help us out with a solution or an alternative approach.