[bs-92] Add simple main wrapper for SpringApplication
* Added Spring class * Used in demo project in README * Also fixed cycle in spring-bootstrap jar [Fixes #49121565]pull/1/merge
parent
0a730beb2a
commit
504d96eb51
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.bootstrap.main;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.bootstrap.SpringApplication;
|
||||
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.SpringVersion;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Very simple main class that can be used to launch an application from sources (class,
|
||||
* package or XML). Useful for demos and testing, perhaps less for production use (where
|
||||
* the {@link SpringApplication} run methods are often more convenient).
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan
|
||||
public class Spring {
|
||||
|
||||
private static ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
|
||||
true);
|
||||
|
||||
private static ApplicationContext context;
|
||||
|
||||
/**
|
||||
* @return the context if there is one
|
||||
*/
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* A basic main that can be used to launch an application.
|
||||
*
|
||||
* @param args command line arguments
|
||||
* @see SpringApplication#run(Object[], String[])
|
||||
* @see SpringApplication#run(Object, String...)
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
List<String> strings = new ArrayList<String>();
|
||||
List<Object> sources = new ArrayList<Object>();
|
||||
|
||||
for (String arg : args) {
|
||||
if (ClassUtils.isPresent(arg, null)) {
|
||||
sources.add(ClassUtils.forName(arg, null));
|
||||
} else if (arg.endsWith(".xml")) {
|
||||
sources.add(arg);
|
||||
} else if (!scanner.findCandidateComponents(arg).isEmpty()) {
|
||||
sources.add(arg);
|
||||
} else {
|
||||
strings.add(arg);
|
||||
}
|
||||
}
|
||||
|
||||
if (sources.isEmpty()) {
|
||||
sources.add(Spring.class);
|
||||
}
|
||||
|
||||
context = SpringApplication.run(sources.toArray(new Object[sources.size()]),
|
||||
strings.toArray(new String[strings.size()]));
|
||||
|
||||
LogFactory.getLog(Spring.class).info(
|
||||
"Running Spring " + SpringVersion.getVersion() + " with sources: "
|
||||
+ sources);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.bootstrap.main;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SimpleMainTests {
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context instanceof ConfigurableApplicationContext) {
|
||||
((ConfigurableApplicationContext) this.context).close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyApplicationContext() throws Exception {
|
||||
Spring.main(new String[0]);
|
||||
this.context = Spring.getApplicationContext();
|
||||
assertNotNull(this.context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basePackageScan() throws Exception {
|
||||
Spring.main(new String[] { ClassUtils.getPackageName(Spring.class) });
|
||||
this.context = Spring.getApplicationContext();
|
||||
assertNotNull(this.context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlContext() throws Exception {
|
||||
Spring.main(new String[] { Spring.class.getName(),
|
||||
"org/springframework/bootstrap/sample-beans.xml" });
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue