1

当我们使用像 cassandra/mongo 这样的任何数据库时,Spring Boot 应用程序属性需要遵循https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html的约定。如果我们想声明我们自己的数据库设置属性而不是 spring-boot 约定,那么设置数据库需要执行哪些步骤?

4

2 回答 2

1

这些是在 Spring Boot 中设置 Jdbc、mongodb 等数据源所需的基本步骤

  • 需要一个@Configuration启用了事务管理的类
  • 读取数据源的环境属性,即数据源 url、用户名、密码等。
  • 为数据源、会话工厂、事务管理器等创建 bean。
  • 完成上述所有设置后,@Configuration在您的使用者中使用它来初始化 Spring 应用程序上下文

这是spring boot中连接mongodb数据源的一些片段

数据源配置.java

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.example.xyz"})
public class DatabaseEntityConfiguration {

    public static final String DATABASE_ENTITY_DATA_SOURCE = "databaseDataSource";
    public static final String DATABASE_HIBERNATE_PROPERTIES = "databaseHibernateProperties";
    public static final String DATABASE_ENTITY_SESSION_FACTORY = "databaseSessionFactory";
    public static final String DATABASE_ENTITY_TRANSACTION_MANAGER = "databaseTransactionManager";
    public static final String DATABASE_ENTITY_DB_CONFIG_DAO = "dmdatabaseDbConfigDao";
    public static final String DATABASE_ENTITY_DB_CONFIG_SERVICE = "dmdatabaseDbConfigService";

    private static final String ENTITY_PACKAGE = "com.example.xyz.database.entity";

    @Autowired
    private org.springframework.core.env.Environment environment;

    @Bean(name = DATABASE_ENTITY_DATA_SOURCE)
    public DataSource databaseEntitydataSource() throws PropertyVetoException {

        // mongodb properties
        String driverClass = environment.getProperty("databaseEntity.mongodb.driverClassName");
        String mongodbUrl = environment.getProperty("databaseEntity.mongodb.dmdatabaseDataSource.url");
        String user = environment.getProperty("databaseEntity.mongodb.dmdatabaseDataSource.username");
        String password = environment.getProperty("databaseEntity.mongodb.dmdatabaseDataSource.password");

        Preconditions.checkArgument(StringUtils.isNotBlank(driverClass), "The property mongodb driverClass must not be null or blank");
        Preconditions.checkArgument(StringUtils.isNotBlank(mongodbUrl), "The property mongodb mongodbUrl must not be null or blank");
        Preconditions.checkArgument(StringUtils.isNotBlank(user), "The property mongodb user must not be null or blank");
        Preconditions.checkArgument(StringUtils.isNotBlank(password), "The property mongodb password must not be null or blank");

        dataSource.setDriverClass(driverClass);
        dataSource.setmongodbUrl(mongodbUrl);
        dataSource.setUser(user);
        dataSource.setPassword(password);

        return dataSource;
    }

    @Bean(name = DATABASE_ENTITY_SESSION_FACTORY)
    public AnnotationSessionFactoryBean databaseEntitySessionFactory() throws PropertyVetoException {
        AnnotationSessionFactoryBean annotationSessionFactoryBean = new AnnotationSessionFactoryBean();
        annotationSessionFactoryBean.setDataSource(databaseEntitydataSource());
        annotationSessionFactoryBean.setPackagesToScan(ENTITY_PACKAGE);
        annotationSessionFactoryBean.setAnnotatedClasses(DBConfig.class);

        annotationSessionFactoryBean.setHibernateProperties(databaseEntityHibernateProperties());
        return annotationSessionFactoryBean;
    }

    @Bean(name = DATABASE_ENTITY_TRANSACTION_MANAGER)
    public HibernateTransactionManager databaseEntityTransactionManager() throws PropertyVetoException {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(databaseEntitySessionFactory().getObject());
        return transactionManager;
    }
}
于 2017-12-16T05:50:13.700 回答
1

您可以这样做:Spring boot - Application.properties 中的自定义变量

或者您可以在 application.properties 文件中创建自己的属性,例如:

my.property.someDb.hostname=http://wherever.com然后在您的代码中引用它,例如:

@Value("${my.property.someDb.hostname}")
private String someDbHostname;

更新1:

如果要使用自己的属性创建 MongoDb,则必须在@Configuration文件中定义正确的 Java Bean。对于 MongoDB,它可能如下所示:

@Configuration
public class MyMongoConfig extends AbstractMongoConfiguration{ 

  @Value("${my.property.someDb.hostname}")
  private String someDbHostname;

  @Value("${my.property.someDb.myOwnPortDefinition}")
  private int myOwnPortDefinition;

  @Value("${my.property.someDb.myDatabasename}")
  private String myDatabasename;

  @Override
  protected String getDatabaseName() {
    return myDatabasename;
  }

  @Override
  @Bean
  public Mongo mongo() throws Exception{
    return new MongoClient(someDbHostname, myOwnPortDefinition );
  }

  @Bean
  public MongoTemplate mongoTemplate() throws Exception{
    return new MongoTemplate(mongo(), getDatabaseName());
  }    
}
于 2017-12-16T05:28:09.427 回答