当我们使用像cassandra / mongo这样的任何数据库时,Spring引导应用程序属性需要遵循https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html的约定。如果我们想要为数据库设置而不是spring-boot约定声明我们自己的属性,那么设置数据库需要做的所有步骤是什么?
答案 0 :(得分:1)
您可以执行此操作:Spring boot - custom variables in 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());
}
}
答案 1 :(得分:1)
这些是获取Jdbc数据源所需的必要步骤,mongodb在Spring Boot中设置
@Configuration
类
在它上面@Configuration
中使用此@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;
}
}
使用者初始化spring应用程序上下文以下是春季启动中mongodb数据源布线的一些片段
<强> DataSourceConfiguration.java 强>
UIObject