213 shaares
Moi ce que je voudrais, c'est pouvoir définir MES valeurs par défaut pour mon application Spring-Boot, que je pourrais surcharger avec un fichier properties.
Donc, je veux surcharger programmatiquement les valeurs par défault de Spring-Boot, et quand même permettre la surcharge par fichier. Dans l'idée, ça marche :
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertiesPropertySource;
import java.util.Properties;
@Configuration
@PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true)
public class DatabaseConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseConfiguration.class.getCanonicalName());
public static final String DATABASE_URL = "jdbc:h2:./db/dbfile;MODE=MySQL;";
public static final String DATABASE_USER = "sa";
public static final String DATABASE_DRIVER = "org.h2.Driver";
public static final String DATABASE_SCHEMA = "PUBLIC";
@Autowired
private Environment env;
@Value("${spring.datasource.url:}") // Defaulted with empty value
private String databaseUrl;
@Value("${spring.datasource.username:}") // Defaulted with empty value
private String databaseUser;
@Value("${spring.datasource.password:}") // Defaulted with empty value
private String databasePassword;
@Value("${spring.datasource.driver-class-name:}") // Defaulted with empty value
private String databaseDriver;
@Value("${spring.datasource.schema:}") // Defaulted with empty value
private String databaseSchema;
private void overrideProperty(String name, String currentValue, String defaultValue) {
if (currentValue == null || currentValue.isEmpty()) {
currentValue = defaultValue;
ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) this.env;
PropertiesPropertySource propertySource = (PropertiesPropertySource) configurableEnvironment.getPropertySources().get("myProps");
Properties myProperties = new Properties();
if (propertySource != null) {
for (String propertyName : propertySource.getPropertyNames()) {
myProperties.put(propertyName, propertySource.getProperty(propertyName));
}
}
myProperties.put(name, currentValue);
configurableEnvironment.getPropertySources().addFirst(new PropertiesPropertySource("myProps", myProperties));
System.out.println("Property " + name + " not found and defaulted to => " + currentValue);
} else {
System.out.println("Property unchanged " + name + " => " + currentValue);
}
}
/**
* I swear to you, I tried to override configuration !!!
*/
@Bean
public void defaultConfiguration() {
this.overrideProperty("spring.datasource.url", this.databaseUrl, DATABASE_URL);
this.overrideProperty("spring.datasource.jdbc-url", this.databaseUrl, DATABASE_URL);
this.overrideProperty("spring.datasource.user", this.databaseUser, DATABASE_USER);
this.overrideProperty("spring.datasource.driver-class-name", this.databaseDriver, DATABASE_DRIVER);
this.overrideProperty("spring.datasource.schema", this.databaseSchema, DATABASE_SCHEMA);
}
}
Mais mon script s'exécute après la création de la DataSource et pas moyen de l'exécuter avant tout le reste T__T
Faut je creuse encore...