下面列出了org.springframework.boot.SpringApplication#setDefaultProperties ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Tests a Logback-access configuration exception in the case where the main configuration file is invalid.
*/
@Test
public void logbackAccessConfigurationExceptionWhereMainConfigurationFileIsInvalid() {
Map<String, Object> properties = new HashMap<>();
properties.put("server.port", findAvailableTcpPort());
SpringApplication application = new SpringApplication(contextConfiguration);
application.setDefaultProperties(properties);
Throwable throwable = catchThrowable(application::run);
Optional<LogbackAccessConfigurationException> exc = findLogbackAccessConfigurationException(throwable);
assertThat(exc).hasValueSatisfying(value ->
assertThat(value)
.hasMessageStartingWith("Could not configure Logback-access:")
.hasMessageContaining("config=[classpath:logback-access.xml]")
.hasCauseInstanceOf(JoranException.class)
);
}
/**
* test sofa.middleware.log.console.level config
*/
@Test
public void testGlobalConsoleLevelConfig() {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH, "true");
properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL, "debug");
SpringApplication springApplication = new SpringApplication(EmptyConfig.class);
springApplication.setDefaultProperties(properties);
springApplication.run(new String[] {});
logger.info("global space console");
logger.debug("global space console debug");
Assert.assertTrue(outContent.toString().contains("global space console"));
Assert.assertTrue(outContent.toString().contains("global space console debug"));
LogEnvUtils.processGlobalSystemLogProperties().remove(
Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH);
LogEnvUtils.processGlobalSystemLogProperties().remove(
Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL);
}
/**
* test sofa.middleware.log.console.logback.pattern config
*/
@Test
public void testLogbackLogConsolePattern() {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH, "true");
properties.put(Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN,
"logback-test-console-pattern"
+ Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN_DEFAULT);
SpringApplication springApplication = new SpringApplication(EmptyConfig.class);
springApplication.setDefaultProperties(properties);
springApplication.run(new String[] {});
logger.info("global space console");
Assert.assertTrue(outContent.toString().contains("logback-test-console-pattern"));
LogEnvUtils.processGlobalSystemLogProperties().remove(
Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH);
LogEnvUtils.processGlobalSystemLogProperties().remove(
Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN);
}
/**
* Set a default to use when no profile is configured.
*
* @param app the Spring application
*/
public static void addDefaultProfile(SpringApplication app) {
Map<String, Object> defProperties = new HashMap<>();
/*
* The default profile to use when no other profiles are defined
* This cannot be set in the <code>application.yml</code> file.
* See https://github.com/spring-projects/spring-boot/issues/1219
*/
defProperties.put(SPRING_PROFILE_DEFAULT, JHipsterConstants.SPRING_PROFILE_DEVELOPMENT);
app.setDefaultProperties(defProperties);
}
@Setup
public void setup() {
Map<String, Object> neo4jConfig = prepareNeo4j();
SpringApplication springApplication = new SpringApplication();
springApplication.addPrimarySources(Collections.singletonList(Application.class));
springApplication.setLazyInitialization(true);
springApplication.setDefaultProperties(neo4jConfig);
this.applicationContext = springApplication.run();
this.movieRepository = applicationContext.getBean(MovieRepository.class);
this.driver = applicationContext.getBean(Driver.class);
}
@Setup
public void setup() {
Map<String, Object> neo4jConfig = prepareNeo4j();
SpringApplication springApplication = new SpringApplication();
springApplication.addPrimarySources(Collections.singletonList(Application.class));
springApplication.setLazyInitialization(true);
springApplication.setDefaultProperties(neo4jConfig);
this.applicationContext = springApplication.run();
this.movieRepository = applicationContext.getBean(MovieRepository.class);
this.driver = applicationContext.getBean(Driver.class);
}
public static void main(String[] args) {
final SpringApplication springApplication =
new SpringApplication(Application.class);
// ir is being added here for LOCAL run ONLY , spring profiles should be be run time parameters when run spring boot jar
springApplication.setDefaultProperties(Collections.singletonMap("spring.profiles.default","LOCAL"));
springApplication.addListeners(new ApplicationPidFileWriter());
springApplication.run(args);
}
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
InputStream in = ChatApplication.class.getClassLoader().getResourceAsStream("chat.properties");
properties.load(in);
SpringApplication app = new SpringApplication(ChatApplication.class);
app.setDefaultProperties(properties);
app.run(args);
}
/**
* Set a default to use when no profile is configured.
*
* @param app the Spring application
*/
public static void addDefaultProfile(SpringApplication app) {
Map<String, Object> defProperties = new HashMap<>();
/*
* The default profile to use when no other profiles are defined
* This cannot be set in the <code>application.yml</code> file.
* See https://github.com/spring-projects/spring-boot/issues/1219
*/
defProperties.put(SPRING_PROFILE_DEFAULT, JHipsterConstants.SPRING_PROFILE_DEVELOPMENT);
app.setDefaultProperties(defProperties);
}
public static void main(String[] args) {
// To initialized the logger for spring.
// Have to init here, otherwise, in other class we won't able to get logger.
UicdCoreDelegator.getInstance();
if (loadFromConfigFile()) {
return;
}
SpringApplication application = new SpringApplication(Application.class);
application.setDefaultProperties(DataSourceConfig.getDBProperties());
Application.context = application.run(args);
Application.args = args;
}
/**
* Set a default to use when no profile is configured.
*
* @param app the Spring application
*/
public static void addDefaultProfile(SpringApplication app) {
Map<String, Object> defProperties = new HashMap<>();
/*
* The default profile to use when no other profiles are defined
* This cannot be set in the <code>application.yml</code> file.
* See https://github.com/spring-projects/spring-boot/issues/1219
*/
defProperties.put(SPRING_PROFILE_DEFAULT, JHipsterConstants.SPRING_PROFILE_DEVELOPMENT);
app.setDefaultProperties(defProperties);
}
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
Map<String, Object> defaults = new HashMap<String, Object>();
defaults.put("clamd.host", "192.168.50.72");
defaults.put("clamd.port", 3310);
defaults.put("clamd.timeout", 500);
defaults.put("clamd.maxfilesize", "20000KB");
defaults.put("clamd.maxrequestsize", "20000KB");
app.setDefaultProperties(defaults);
app.run(args);
}
/**
* Set a default to use when no profile is configured.
*
* @param app the Spring application
*/
public static void addDefaultProfile(SpringApplication app) {
Map<String, Object> defProperties = new HashMap<>();
/*
* The default profile to use when no other profiles are defined
* This cannot be set in the <code>application.yml</code> file.
* See https://github.com/spring-projects/spring-boot/issues/1219
*/
defProperties.put(SPRING_PROFILE_DEFAULT, JHipsterConstants.SPRING_PROFILE_DEVELOPMENT);
app.setDefaultProperties(defProperties);
}
public static void main(final String[] args) {
final SpringApplication application = new SpringApplication(AddressMigratorApp2.class);
final Properties properties = new Properties();
properties.putIfAbsent("server.port", 9010);
application.setDefaultProperties(properties);
application.run(args);
}
@Test
public void testDisableBizStateEndpoint() {
Map<String, Object> properties = new HashMap<>();
properties.put("management.endpoint.bizState.enabled", "false");
SpringApplication springApplication = new SpringApplication(EmptyConfiguration.class);
springApplication.setDefaultProperties(properties);
ConfigurableApplicationContext applicationContext = springApplication.run(new String[] {});
Assert.assertFalse(applicationContext.containsBean("introspectBizEndpoint"));
applicationContext.close();
}
public static void main(String[] args) throws IOException {
//更改properties配置文件名称,避免依赖冲突
Properties properties = new Properties();
InputStream in = PortalApplication.class.getClassLoader().getResourceAsStream("portal.properties");
properties.load(in);
SpringApplication app = new SpringApplication(PortalApplication.class);
app.setDefaultProperties(properties);
app.run(args);
}
/**
* Set a default to use when no profile is configured.
*
* @param app the Spring application
*/
public static void addDefaultProfile(SpringApplication app) {
Map<String, Object> defProperties = new HashMap<>();
/*
* The default profile to use when no other profiles are defined
* This cannot be set in the <code>application.yml</code> file.
* See https://github.com/spring-projects/spring-boot/issues/1219
*/
defProperties.put(SPRING_PROFILE_DEFAULT, JHipsterConstants.SPRING_PROFILE_DEVELOPMENT);
app.setDefaultProperties(defProperties);
}
/**
* Set a default to use when no profile is configured.
*
* @param app the Spring application
*/
public static void addDefaultProfile(SpringApplication app) {
Map<String, Object> defProperties = new HashMap<>();
/*
* The default profile to use when no other profiles are defined
* This cannot be set in the <code>application.yml</code> file.
* See https://github.com/spring-projects/spring-boot/issues/1219
*/
defProperties.put(SPRING_PROFILE_DEFAULT, JHipsterConstants.SPRING_PROFILE_DEVELOPMENT);
app.setDefaultProperties(defProperties);
}
/**
* Set a default to use when no profile is configured.
*
* @param app the Spring application
*/
public static void addDefaultProfile(SpringApplication app) {
Map<String, Object> defProperties = new HashMap<>();
/*
* The default profile to use when no other profiles are defined
* This cannot be set in the <code>application.yml</code> file.
* See https://github.com/spring-projects/spring-boot/issues/1219
*/
defProperties.put(SPRING_PROFILE_DEFAULT, JHipsterConstants.SPRING_PROFILE_DEVELOPMENT);
app.setDefaultProperties(defProperties);
}
@BeforeClass
public static void startServer() throws Exception {
if (server == null || !server.isActive()) {
System.setProperty("ws.maxSessions", Integer.toString(MAX_WS_CONNECTIONS));
System.setProperty("java.security.egd", "file:/dev/./urandom");
Properties properties = new Properties();
properties.put("server.port", getPort());
SpringApplication application = new SpringApplication(BootTestApplication.class);
application.setDefaultProperties(properties);
log.debug("Properties: {}", properties);
server = application.run();
}
}