Java Code Examples for org.springframework.context.support.ClassPathXmlApplicationContext#refresh()

The following examples show how to use org.springframework.context.support.ClassPathXmlApplicationContext#refresh() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MarkingFunctionsFactory.java    From datawave with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: SpringContextLoader.java    From verigreen with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: ConfigurationDtoPostProcessorRasterLayerTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@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.");
	}
}
 
Example 4
Source File: ConfigurationDtoPostProcessorVectorLayerTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@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.");
	}
}
 
Example 5
Source File: ConfigurationDtoPostProcessorRasterLayerTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@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.");
	}
}
 
Example 6
Source File: QueryLogicFactoryBeanTest.java    From datawave with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 7
Source File: ConfigurationDtoPostProcessorVectorLayerTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@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.");
	}
}
 
Example 8
Source File: UserCreateTools.java    From disconf with Apache License 2.0 6 votes vote down vote up
/**
 * @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);
}
 
Example 9
Source File: DBUnitDataExtractorRunner.java    From gazpachoquest with GNU General Public License v3.0 6 votes vote down vote up
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");

}
 
Example 10
Source File: ConfigurationDtoPostProcessorVectorLayerTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 .");
	}
}
 
Example 11
Source File: ConfigurationDtoPostProcessorVectorLayerTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@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());
}
 
Example 12
Source File: DemoServiceProviderXmlBootstrap.java    From dubbo-registry-nacos with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 13
Source File: TypeMetadataProvider.java    From datawave with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: SpringContainer.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 15
Source File: XmlConfigBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 16
Source File: Test.java    From tcc-transaction with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example 17
Source File: DubboApplicationListenerTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 18
Source File: Test.java    From galaxy with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example 19
Source File: DispatcherWebscriptMockitoTest.java    From alfresco-mvc with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 20
Source File: SpringBusFactoryTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@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();
}