Spring启动配置属性问题

时间:2017-04-20 21:42:04

标签: java spring spring-boot properties

我一直致力于创建自定义数据源bean。代码和.properties文件如下:

@Configuration
@EnableConfigurationProperties
@EnableWebMvc
public class ConfigClass {

    @Bean(name="DbDataSource", destroyMethod="close")
    @ConfigurationProperties(prefix="db.datasource")
    public DataSource dbDataSource() {
        DataSource ds;
        ds = DataSourceBuilder.create().build();
        return  ds;
    }
    @Bean(name = "taxTransactionManager")
    public JpaTransactionManager getJpaTransactionManager(){
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setDataSource(dbDataSource());
    }

=============================================== ========================

#db datasource
db.datasource.type=javax.sql.DataSource
db.datasource.driver-class-name=oracle.jdbc.OracleDriver

问题是它似乎没有加载/应用属性到数据源bean,因为我不断收到以下错误:

The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database Connectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl@982e6a52". 
    at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:72)
    at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:849)
    at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getConnectionFactory(JDBCConfigurationImpl.java:732)
    at sun.reflect.GeneratedMethodAccessor47.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)

我已尝试将配置放在所有文件中,我也尝试使用单独的文件并在另一个文件中引用一个配置文件,如下所示:

@Configuration
@EnableConfigurationProperties
public class ConfigClass {
    @Bean(name="dbDataSource", destroyMethod="close")
    @ConfigurationProperties(prefix="posdb.datasource")
    public DataSource dbDataSource() {
        return DataSourceBuilder.create().build();
    }}

=============================================== =========

@Configuration
@Import(ConfigClass.class)
public class ConfigClass2{

@Autowired
ConfigClass config1;

        @Bean(name = "genericTransactionManager")
        public JpaTransactionManager getJpaTransactionManager(){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setDataSource(config1.dbDataSource());
        return transactionManager;
    }}

正在导入配置类以配置和运行我的应用程序:

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,
    SessionAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,
    JpaRepositoriesAutoConfiguration.class,JndiDataSourceAutoConfiguration.class,JndiConnectionFactoryAutoConfiguration.class,
    RedisAutoConfiguration.class})
@Import(ConfigClass.class)
public class ServiceBootstrap {

    public static void main(String[] args) {
        SpringApplication.run(ServiceBootstrap.class, args);
    }
}

spring boot app位于另一个项目中,正在导入配置类,使其成为一个依赖项目。我也尝试在ServiceBootstrap类中导入我的ConfigClass2,但我仍然遇到同样的错误。此外,我尝试自动装配数据源bean本身而不是配置类,并且bean返回为null。有谁知道可能导致这个问题的原因?

0 个答案:

没有答案