下面列出了com.google.inject.name.Names#bindProperties ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void registerConfigFiles(InputStream... configFiles) {
try {
/**
* Allow *.properties config to be overridden by system properties
*/
final Properties systemProperties = System.getProperties();
for (InputStream configFile : configFiles) {
Properties properties = new Properties();
properties.load(configFile);
properties.entrySet().forEach(propertyEntry -> {
if (systemProperties.containsKey(propertyEntry.getKey())) {
propertyEntry.setValue(systemProperties.getProperty((String) propertyEntry.getKey()));
}
});
Names.bindProperties(binder(), properties);
systemProperties.putAll(properties);
}
} catch (IOException e) {
System.out.println(Arrays.toString(e.getStackTrace()));
}
}
@Override
protected void configure() {
bind(SampleRepository.class).to(CassandraSampleRepository.class);
bind(CassandraSession.class).to(CassandraSessionImpl.class);
Properties properties = new Properties();
properties.put("cassandra.keyspace", System.getProperty("cassandra.keyspace", "newts"));
properties.put("cassandra.hostname", System.getProperty("cassandra.host", "localhost"));
properties.put("cassandra.port", System.getProperty("cassandra.port", "9042"));
properties.put("cassandra.username", System.getProperty("cassandra.username", "admin"));
properties.put("cassandra.password", System.getProperty("cassandra.password", "admin"));
properties.put("cassandra.ssl", System.getProperty("cassandra.ssl", "false"));
properties.put("cassandra.compression", System.getProperty("cassandra.compression", "NONE"));
properties.put("samples.cassandra.time-to-live", System.getProperty("cassandra.time-to-live", "0"));
Names.bindProperties(binder(), properties);
bind(MetricRegistry.class).toInstance(new MetricRegistry());
}
@Override
protected void configure() {
try {
Properties props = loadProperties();
Names.bindProperties(binder(), props);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void configure() {
try {
Names.bindProperties(binder(), getProperties());
} catch (IOException e) {
throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
@Override
protected void configure() {
/*
Grab all the string properties and register them with Guice so they can be @Inject-ed
without any further configuration.
*/
Map<String, String> props = getPropertiesMap();
Names.bindProperties(binder(), props);
}
@Override
public void configure() {
/*
* Install other guice modules
*/
install(new ObjectMapperModule());
install(new HttpClientModule());
install(new GsonModule());
install(new PropertiesInjectorModule(serverEnv));
install(new CsvParserModule());
//install(new KafkaModule());
/*
* Bind Direct classes, classes to interfaces etc
*/
bind(ZeroCodeMultiStepsScenarioRunner.class).to(ZeroCodeMultiStepsScenarioRunnerImpl.class);
bind(ApiServiceExecutor.class).to(ApiServiceExecutorImpl.class);
bind(HttpApiExecutor.class).to(HttpApiExecutorImpl.class);
bind(JavaMethodExecutor.class).to(JavaMethodExecutorImpl.class);
bind(ZeroCodeAssertionsProcessor.class).to(ZeroCodeAssertionsProcessorImpl.class);
bind(ZeroCodeValidator.class).to(ZeroCodeValidatorImpl.class);
bind(ZeroCodeReportGenerator.class).to(ZeroCodeReportGeneratorImpl.class);
bind(ZeroCodeExternalFileProcessor.class).to(ZeroCodeExternalFileProcessorImpl.class);
bind(ZeroCodeParameterizedProcessor.class).to(ZeroCodeParameterizedProcessorImpl.class);
// ------------------------------------------------
// Bind properties for localhost, CI, DIT, SIT etc
// ------------------------------------------------
Names.bindProperties(binder(), getProperties(serverEnv));
}
@Override
public void configure(Binder binder) {
// Bind PostgreSQL-specific properties
JdbcHelper.PostgreSQL.configure(binder);
// Bind MyBatis properties
Names.bindProperties(binder, myBatisProperties);
// Bind JDBC driver properties
binder.bind(Properties.class)
.annotatedWith(Names.named("JDBC.driverProperties"))
.toInstance(driverProperties);
}
@Override
public void configure(Binder binder) {
// Check which MySQL-compatible driver is in use
switch(mysqlDriver) {
// Bind MySQL-specific properties
case MYSQL:
JdbcHelper.MySQL.configure(binder);
break;
// Bind MariaDB-specific properties
case MARIADB:
JdbcHelper.MariaDB.configure(binder);
break;
default:
throw new UnsupportedOperationException(
"A driver has been specified that is not supported by this module."
);
}
// Bind MyBatis properties
Names.bindProperties(binder, myBatisProperties);
// Bind JDBC driver properties
binder.bind(Properties.class)
.annotatedWith(Names.named("JDBC.driverProperties"))
.toInstance(driverProperties);
}
@Override
public void configure(Binder binder) {
// Bind SQLServer-specific properties with the configured driver.
switch(sqlServerDriver) {
case JTDS:
JdbcHelper.SQL_Server_jTDS.configure(binder);
break;
case DATA_DIRECT:
JdbcHelper.SQL_Server_DataDirect.configure(binder);
break;
case MICROSOFT_LEGACY:
JdbcHelper.SQL_Server_MS_Driver.configure(binder);
break;
case MICROSOFT_2005:
JdbcHelper.SQL_Server_2005_MS_Driver.configure(binder);
break;
default:
throw new UnsupportedOperationException(
"A driver has been specified that is not supported by this module."
);
}
// Bind MyBatis properties
Names.bindProperties(binder, myBatisProperties);
// Bind JDBC driver properties
binder.bind(Properties.class)
.annotatedWith(Names.named("JDBC.driverProperties"))
.toInstance(driverProperties);
}
protected void bindProperties(Binder binder, String propertyFilePath) {
try {
InputStream in = getClass().getClassLoader().getResourceAsStream(propertyFilePath);
if (in != null) {
Properties properties = new Properties();
properties.load(in);
Names.bindProperties(binder, properties);
} else {
throw new IllegalStateException("Couldn't find property file : " + propertyFilePath);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
protected Properties tryBindProperties(Binder binder, String propertyFilePath) {
try {
InputStream in = getClass().getClassLoader().getResourceAsStream(propertyFilePath);
if (in != null) {
Properties properties = new Properties();
properties.load(in);
Names.bindProperties(binder, properties);
return properties;
} else
return null;
} catch (IOException e) {
return null;
}
}
@Override
protected void configure() {
Names.bindProperties(binder(), config.toProperties());
bind(JobFactory.class).to(SchedulerFactory.class);
bind(Cache.class).toProvider(CacheProvider.class);
bind(MangooAuthorizationService.class).to(AuthorizationService.class);
}
@Override
public void configure(Binder binder) {
// Bind SQLServer-specific properties with the configured driver.
switch(sqlServerDriver) {
case JTDS:
JdbcHelper.SQL_Server_jTDS.configure(binder);
break;
case DATA_DIRECT:
JdbcHelper.SQL_Server_DataDirect.configure(binder);
break;
case MICROSOFT_LEGACY:
JdbcHelper.SQL_Server_MS_Driver.configure(binder);
break;
case MICROSOFT_2005:
JdbcHelper.SQL_Server_2005_MS_Driver.configure(binder);
break;
default:
throw new UnsupportedOperationException(
"A driver has been specified that is not supported by this module."
);
}
// Bind MyBatis properties
Names.bindProperties(binder, myBatisProperties);
// Bind JDBC driver properties
binder.bind(Properties.class)
.annotatedWith(Names.named("JDBC.driverProperties"))
.toInstance(driverProperties);
}
private void bindNonConstantProperties() {
Properties p = getProperties();
for (String overridableProperty : constantGuiceProperties()) {
p.remove(overridableProperty);
}
Names.bindProperties(this.binder(), p);
}
@Override
protected void configureServlets() {
Map<String, String> params = new HashMap<String, String>();
params.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
params.put("applicationFactoryClassName", GuiceWebApplicationFactory.class.getName());
params.put("injectorContextAttribute", Injector.class.getName());
bind(WicketFilter.class).in(Singleton.class);
filter("/*").through(WicketFilter.class, params);
Names.bindProperties(binder(), properties);
bindOrientDbProperties(properties);
String applicationClass = properties.getProperty("orienteer.application");
Class<? extends OrienteerWebApplication> appClass = OrienteerWebApplication.class;
if (applicationClass != null) {
try {
Class<?> customAppClass = Class.forName(applicationClass);
if (OrienteerWebApplication.class.isAssignableFrom(appClass)) {
appClass = (Class<? extends OrienteerWebApplication>) customAppClass;
} else {
LOG.error("Orienteer application class '" + applicationClass + "' is not child class of '" + OrienteerWebApplication.class + "'. Using default.");
}
} catch (ClassNotFoundException e) {
LOG.error("Orienteer application class '" + applicationClass + "' was not found. Using default.");
}
}
bind(appClass).asEagerSingleton();
Provider<? extends OrienteerWebApplication> appProvider = binder().getProvider(appClass);
if (!OrienteerWebApplication.class.equals(appClass)) {
bind(OrienteerWebApplication.class).toProvider(appProvider);
}
bind(OrientDbWebApplication.class).toProvider(appProvider);
bind(WebApplication.class).toProvider(appProvider);
bind(Properties.class).annotatedWith(Orienteer.class).toInstance(properties);
install(
loadFromClasspath(new OrienteerModule())
);
}
@Override
public void configure(Binder binder) {
// Bind PostgreSQL-specific properties
JdbcHelper.PostgreSQL.configure(binder);
// Bind MyBatis properties
Names.bindProperties(binder, myBatisProperties);
// Bind JDBC driver properties
binder.bind(Properties.class)
.annotatedWith(Names.named("JDBC.driverProperties"))
.toInstance(driverProperties);
}
@Override
protected void configure() {
Names.bindProperties(binder(), gossipConfig);
}
@Override
protected void configure() {
Names.bindProperties(binder(), properties);
}
@Override
protected void configure() {
Names.bindProperties(binder(), properties);
logProperties();
}
/**
* This method reads property files and creates following bindings:
* <ul>
* <li>a named binding for each property,
* <li>a binding for Properties class; bound Property instance contains all the properties.
* </ul>
* Property file paths are retrieved from configuration.paths property. If configuration.paths is not set,
* bindProperties will look in default location, i.e. "src/main/config". Property configuration.paths can
* contain any number of paths. Paths in configuration.paths should be separated with semicolons.
*
* @param binder The Binder instance that will store the newly created property bindings.
* @param configStrategy the selected configuration strategy
*/
public static void bindProperties(Binder binder, ConfigStrategy configStrategy) {
Properties properties = configStrategy.gatherProperties();
Names.bindProperties(binder, properties);
binder.bind(Properties.class).toInstance(properties);
}