这是我的MySQL属性文件
mysql.properties
dialect=org.hibernate.dialect.MySQL57Dialect
hbm2ddl_method=validate
show_sql=true
format_sql=false
pool_name=testpool
jdbc_url=jdbc:mysql://localhost:3306/testdb
minimum_idle=2
uname=root
password=testpsw
cache_prep_stmts=true
prep_stmt_cache_size=256
prep_stmt_cache_sql_limit=2048
use_server_prep_stmts=true
maximum_pool_size=30
driver_class_name=com.mysql.jdbc.Driver和用于数据源配置的Oracle属性文件。
dialect=org.hibernate.dialect.Oracle10gDialect
hbm2ddl_method=validate
show_sql=true
format_sql=false
pool_name=testpool
jdbc_url=jdbc:oracle:thin:@localhost:1521:testdb
minimum_idle=2
uname=barn_act
password=testpsw
cache_prep_stmts=true
prep_stmt_cache_size=256
prep_stmt_cache_sql_limit=2048
use_server_prep_stmts=true
maximum_pool_size=30
driver_class_name=oracle.jdbc.OracleDriver我创建了两个这样的类来将属性绑定到字段中。
@Component("mysql_props")
@PropertySource(value = "classpath:/mysql.properties")
@ConfigurationProperties
@Getter
@Setter
public class HibernateMySQLProperties {
private String dialect;
//other props
}
@Component("oracle_props")
@PropertySource(value = "classpath:/oracle.properties")
@ConfigurationProperties
@Getter
@Setter
public class HibernateOracleProperties {
//same fileds as mysql
}当我将这两个bean注入PersistenceConfiguration类时,会注入相同的属性字段。
@Configuration
@EnableConfigurationProperties({ HibernateOracleProperties.class, HibernateMySQLProperties.class })
public class PersistenceConfig {
@Autowired
@Qualifier("oracle_props")
private HibernateOracleProperties oracleProps;
@Autowired
@Qualifier("mysql_props")
private HibernateMySQLProperties mysqlProps;
}


如何解决这个问题?
发布于 2019-09-19 19:32:21
这是spring在spring-boot之前的一个已知问题/行为。在spring中,属性占位符可以理解唯一键。您的两个属性文件具有相同的键名。
因此,解决方案将如下图所示,只需较少的更改。
如下所示更改属性文件。mysql.properties
mysql.dialect=org.hibernate.dialect.MySQL57Dialect
****** all otheres same start with mysql.Oracle属性文件
oracle.dialect=org.hibernate.dialect.Oracle10gDialect
****** all otheres same start with oracle.现在更改您的Hibernate*Properties.java @ConfigurationProperties注释。
@Component("oracle_props")
@PropertySource(value = "classpath:/oracle.properties")
@ConfigurationProperties(prefix = "oracle")
@Component("mysql_props")
@PropertySource(value = "classpath:/mysql.properties")
@ConfigurationProperties(prefix = "mysql")不需要在PersistenceConfig.java文件中进行任何更改。
https://stackoverflow.com/questions/58007844
复制相似问题