下面列出了org.springframework.context.support.ClassPathXmlApplicationContext#refresh ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testUserStyle() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
context.setId("test");
context.setDisplayName("test");
context.setConfigLocation(
"/org/geomajas/spring/geomajasContext.xml "+
"/org/geomajas/internal/configuration/layerDefaultStyle.xml ");
context.refresh();
VectorLayer layerDefaultStyle = (VectorLayer)context.getBean("layerDefaultStyle");
List<NamedStyleInfo> styles = layerDefaultStyle.getLayerInfo().getNamedStyleInfos();
Assert.assertEquals(1, styles.size());
NamedStyleInfo defaultStyle = styles.get(0);
UserStyleInfo userStyle = defaultStyle.getUserStyle();
List<RuleInfo> rules = userStyle.getFeatureTypeStyleList().get(0).getRuleList();
Assert.assertEquals(1, rules.size());
Assert.assertEquals(null, rules.get(0).getName());
}
@Test
public void testDuplicateAttribute() {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
context.setId("test");
context.setDisplayName("test");
context.setConfigLocation(
"/org/geomajas/spring/geomajasContext.xml " +
"/org/geomajas/internal/configuration/layerBeansDuplicateAttribute.xml " +
"");
context.refresh();
Assert.fail("Context initialization should have failed.");
} catch (BeanCreationException bce) {
assertThat(bce.getCause().getCause().getMessage()).startsWith(
"Duplicate attribute name stringAttr in layer beans, path .");
}
}
public static void main(final String[] args) throws Exception {
String dbEngine = "db_postgres";
logger.info("Extracting data from {} database in DBUnit format", dbEngine);
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles("postgres", dbEngine);
ctx.refresh();
ctx.setConfigLocations(new String[] { "dbunitextractor-datasource-context.xml", "dbunitextractor-context.xml" });
/*-
ctx.getEnvironment().getPropertySources()
.addLast(new ResourcePropertySource(String.format("classpath:/database/%s.properties", dbEngine))); */
ctx.refresh();
DBUnitDataExtractor extractor = (DBUnitDataExtractor) ctx.getBean("dbUnitDataExtractor");
extractor.extract();
ctx.close();
logger.info("Done successfully. Check your target directory");
}
/**
* @param args
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles("production");
ctx.setConfigLocation("applicationContext.xml");
ctx.refresh();
userDao = (UserDao) ctx.getBean("userDaoImpl");
/**
* 生成测试用户 SQL
*/
UserCreateCommon.generateCreateTestUserSQL(userDao);
/**
* 生成指定用户 SQL
*/
UserCreateCommon.generateCreateSpecifyUserSQL(userDao, "msoa", "msoaSH", RoleEnum.ADMIN, "");
System.exit(1);
}
@Test
public void testDuplicateAttributeInManyToOne() {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
context.setId("test");
context.setDisplayName("test");
context.setConfigLocation(
"/org/geomajas/spring/geomajasContext.xml " +
"/org/geomajas/internal/configuration/layerBeansDuplicateAttrManyToOne.xml " +
"");
context.refresh();
Assert.fail("Context initialization should have failed.");
} catch (BeanCreationException bce) {
assertThat(bce.getCause().getCause().getMessage()).startsWith(
"Duplicate attribute name stringAttr in layer beans, path /manyToOneAttr.");
}
}
public static synchronized MarkingFunctions createMarkingFunctions() {
if (markingFunctions != null)
return markingFunctions;
ClassLoader thisClassLoader = MarkingFunctionsFactory.class.getClassLoader();
// ignore calls to close as this blows away the cache manager
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
try {
context.setClassLoader(thisClassLoader);
context.setConfigLocations("classpath*:/MarkingFunctionsContext.xml", "classpath*:/CacheContext.xml");
context.refresh();
markingFunctions = context.getBean("markingFunctions", MarkingFunctions.class);
} catch (Throwable t) {
log.warn("Could not load spring context files! Got " + t);
if (log.isDebugEnabled()) {
log.debug("Failed to load Spring contexts", t);
}
}
return markingFunctions;
}
@Before
public void setup() throws IllegalArgumentException, IllegalAccessException {
System.setProperty(NpeUtils.NPE_OU_PROPERTY, "iamnotaperson");
System.setProperty("dw.metadatahelper.all.auths", "A,B,C,D");
Logger.getLogger(ClassPathXmlApplicationContext.class).setLevel(Level.OFF);
Logger.getLogger(XmlBeanDefinitionReader.class).setLevel(Level.OFF);
Logger.getLogger(DefaultListableBeanFactory.class).setLevel(Level.OFF);
ClassPathXmlApplicationContext queryFactory = new ClassPathXmlApplicationContext();
queryFactory.setConfigLocation("TestQueryLogicFactory.xml");
queryFactory.refresh();
factoryConfig = queryFactory.getBean(QueryLogicFactoryConfiguration.class.getSimpleName(), QueryLogicFactoryConfiguration.class);
Whitebox.setInternalState(bean, QueryLogicFactoryConfiguration.class, factoryConfig);
Whitebox.setInternalState(bean, ClassPathXmlApplicationContext.class, queryFactory);
ctx = createMock(EJBContext.class);
logic = createMockBuilder(BaseQueryLogic.class).addMockedMethods("setLogicName", "getMaxPageSize", "getPageByteTrigger").createMock();
DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of("CN=Poe Edgar Allan eapoe, OU=acme", "<CN=ca, OU=acme>"), UserType.USER, null, null, null,
0L);
principal = new DatawavePrincipal(Collections.singletonList(user));
}
@Test
public void testNullTileHeight() throws Exception {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
context.setId("test");
context.setDisplayName("test");
context.setConfigLocation(
"/org/geomajas/spring/geomajasContext.xml " +
"/org/geomajas/internal/configuration/RasterLayerZeroTileHeight.xml " +
"");
context.refresh();
Assert.fail("Context initialization should have failed.");
} catch (BeanCreationException bce) {
assertThat(bce.getCause().getCause().getMessage()).startsWith(
"Layer layerOsm is not correctly configured: tileHeight should not be zero.");
}
}
@Test
public void testAttributeInvalidNameCheck() {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
context.setId("test");
context.setDisplayName("test");
context.setConfigLocation(
"/org/geomajas/spring/geomajasContext.xml " +
"/org/geomajas/testdata/beanContext.xml " +
"/org/geomajas/testdata/layerBeans.xml " +
"/org/geomajas/internal/configuration/layerBeansInvalid.xml " +
"");
context.refresh();
Assert.fail("Context initialization should have failed.");
} catch (BeanCreationException bce) {
assertThat(bce.getCause().getCause().getMessage()).startsWith(
"Invalid attribute name manyToOne.stringAttr in layer beans.");
}
}
@Test
public void testNullTileWidth() throws Exception {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
context.setId("test");
context.setDisplayName("test");
context.setConfigLocation(
"/org/geomajas/spring/geomajasContext.xml " +
"/org/geomajas/internal/configuration/RasterLayerZeroTileWidth.xml " +
"");
context.refresh();
Assert.fail("Context initialization should have failed.");
} catch (BeanCreationException bce) {
assertThat(bce.getCause().getCause().getMessage()).startsWith(
"Layer layerOsm is not correctly configured: tileWidth should not be zero.");
}
}
@Override
public ApplicationContext loadContext(String... locations) {
String[] allLocations = generateConfigLocations(Arrays.asList(locations));
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(allLocations, false);
/*
* Set validating=false to avoid validation of XML configuration files.
* This decreases startup time significantly.
*/
context.setValidating(false);
context.refresh(); // Load the context.
SpringContextHolder.getInstance().setApplicationContext(context);
return context;
}
@Override
public void start() {
String configPath = ConfigUtils.getProperty(SPRING_CONFIG);
if (configPath == null || configPath.length() == 0) {
configPath = DEFAULT_SPRING_CONFIG;
}
context = new ClassPathXmlApplicationContext(configPath.split("[,\\s]+"), false);
context.addApplicationListener(new DubboApplicationListener());
context.registerShutdownHook();
context.refresh();
context.start();
}
@Test
public void testLoadBusWithApplicationContext() throws BusException {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(new String[] {"/org/apache/cxf/systest/bus/basic.xml"});
Bus bus = ctx.getBean("cxf", Bus.class);
ctx.refresh();
bus = ctx.getBean("cxf", Bus.class);
checkBindingExtensions(bus);
checkHTTPTransportFactories(bus);
checkOtherCoreExtensions(bus);
ctx.close();
}
@Test
public void testOneShutdownHook() {
DubboShutdownHook spyHook = Mockito.spy(DubboShutdownHook.getDubboShutdownHook());
ClassPathXmlApplicationContext applicationContext = getApplicationContext(spyHook, false);
applicationContext.refresh();
applicationContext.close();
Mockito.verify(spyHook, Mockito.times(1)).destroyAll();
}
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
context.setConfigLocation("/META-INF/spring/dubbo-provider-context.xml");
context.refresh();
System.out.println("DemoService provider (XML) is starting...");
System.in.read();
}
public static synchronized TypeMetadataProvider createTypeMetadataProvider() {
if (typeMetadataProvider != null)
return typeMetadataProvider;
ClassLoader thisClassLoader = TypeMetadataProvider.Factory.class.getClassLoader();
// ignore calls to close as this blows away the cache manager
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
try {
// To prevent failure when this is run on the tservers:
// The VFS ClassLoader has been created and has been made the current thread's context classloader, but its resource paths are empty at this
// time.
// The spring ApplicationContext will prefer the current thread's context classloader, so the spring context would fail to find
// any classes or context files to load.
// Instead, set the classloader on the ApplicationContext to be the one that is loading this class.
// It is a VFSClassLoader that has the accumulo lib/ext jars set as its resources.
// After setting the classloader, then set the config locations and refresh the context.
context.setClassLoader(thisClassLoader);
context.setConfigLocations("classpath:/TypeMetadataBridgeContext.xml", "classpath:/TypeMetadataProviderContext.xml");
context.refresh();
typeMetadataProvider = context.getBean("typeMetadataProvider", TypeMetadataProvider.class);
} catch (Throwable t) {
// got here because the VFSClassLoader on the tservers does not implement findResources
// none of the spring wiring will work.
log.warn("Could not load spring context files. got " + t);
}
return typeMetadataProvider;
}
public static void main(String[] args) {
// 构建 XML 配置驱动 Spring 上下文
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
// 设置 XML 配置文件的位置
context.setConfigLocation("classpath:/META-INF/spring/context.xml");
// 启动上下文
context.refresh();
// 获取名称为 "user" Bean 对象
User user = context.getBean("user", User.class);
// 输出用户名称:"小马哥"
System.out.printf("user.getName() = %s \n",user.getName());
// 关闭 Spring 上下文
context.close();
}
public static void main(String[] args){
ClassPathXmlApplicationContext ctx= new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ctx.refresh();
log.error("eeeee");
System.out.println("!!!Start!!!");
long startTime = System.currentTimeMillis();
Test test= ctx.getBean(Test.class);
String result= test.fn("helloworld",123);
System.out.println("result = " + result);
System.out.println("!!!OK!!! Spent time = " + (System.currentTimeMillis() - startTime) + "MS");
}
public static void main(String[] args){
ClassPathXmlApplicationContext ctx= new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ctx.refresh();
log.error("eeeee");
System.out.println("!!!Start!!!");
long startTime = System.currentTimeMillis();
Test test= ctx.getBean(Test.class);
String result= test.fn("helloworld",123);
System.out.println("result = " + result);
System.out.println("!!!OK!!! Spent time = " + (System.currentTimeMillis() - startTime) + "MS");
}
@BeforeAll
public void beforeAll() throws Exception {
MockitoAnnotations.initMocks(this);
webScript.setServletContext(new MockServletContext());
webScript.setContextConfigLocation("test-webscriptdispatcher-context.xml");
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext();
applicationContext.setConfigLocation("web-context-test.xml");
applicationContext.refresh();
webScript.setApplicationContext(applicationContext);
webScript.onApplicationEvent(new ContextRefreshedEvent(applicationContext));
mockWebscript = MockWebscriptBuilder.singleWebscript(webScript);
}