Spring引导常用应用程序属性
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring引导常用应用程序属性相关的知识,希望对你有一定的参考价值。
当我们使用像cassandra / mongo这样的任何数据库时,Spring引导应用程序属性需要遵循https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html的约定。如果我们想要为数据库设置而不是spring-boot约定声明我们自己的属性,那么设置数据库需要做的所有步骤是什么?
答案
你可以这样做:Spring boot - custom variables in Application.properties
或者你可以在application.properties文件中创建自己的属性,如:
my.property.someDb.hostname=http://wherever.com
and然后在你的代码中引用它,如:
@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());
}
}
另一答案
这些是获取Spring Boot中设置的Jdbc,mongodb等数据源所需的基本步骤
- 需要一个启用了事务管理的
@Configuration
类 - 读取数据源的环境属性,即dataSource url,username,password等。
- 为数据源,会话工厂,事务管理器等创建bean。
- 完成上述所有设置后,在您的使用者中使用此
@Configuration
来初始化spring应用程序上下文
以下是一些在春季启动时连接mongodb数据源的片段
data source configuration.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;
}
}
以上是关于Spring引导常用应用程序属性的主要内容,如果未能解决你的问题,请参考以下文章