Java Code Examples for org.springframework.context.ApplicationContext#getResource()

The following examples show how to use org.springframework.context.ApplicationContext#getResource() . These examples are extracted from open source projects. 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 Project: spring-analysis-note   File: XmlViewResolver.java    License: MIT License 5 votes vote down vote up
/**
 * Initialize the view bean factory from the XML file.
 * Synchronized because of access by parallel threads.
 * @throws BeansException in case of initialization errors
 */
protected synchronized BeanFactory initFactory() throws BeansException {
	if (this.cachedFactory != null) {
		return this.cachedFactory;
	}

	ApplicationContext applicationContext = obtainApplicationContext();

	Resource actualLocation = this.location;
	if (actualLocation == null) {
		actualLocation = applicationContext.getResource(DEFAULT_LOCATION);
	}

	// Create child ApplicationContext for views.
	GenericWebApplicationContext factory = new GenericWebApplicationContext();
	factory.setParent(applicationContext);
	factory.setServletContext(getServletContext());

	// Load XML resource with context-aware entity resolver.
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
	reader.setEnvironment(applicationContext.getEnvironment());
	reader.setEntityResolver(new ResourceEntityResolver(applicationContext));
	reader.loadBeanDefinitions(actualLocation);

	factory.refresh();

	if (isCache()) {
		this.cachedFactory = factory;
	}
	return factory;
}
 
Example 2
private void resolveResourceLocations() {
	if (CollectionUtils.isEmpty(this.locationValues)) {
		return;
	}
	else if (!CollectionUtils.isEmpty(this.locations)) {
		throw new IllegalArgumentException("Please set either Resource-based \"locations\" or " +
				"String-based \"locationValues\", but not both.");
	}

	ApplicationContext applicationContext = obtainApplicationContext();
	for (String location : this.locationValues) {
		if (this.embeddedValueResolver != null) {
			String resolvedLocation = this.embeddedValueResolver.resolveStringValue(location);
			if (resolvedLocation == null) {
				throw new IllegalArgumentException("Location resolved to null: " + location);
			}
			location = resolvedLocation;
		}
		Charset charset = null;
		location = location.trim();
		if (location.startsWith(URL_RESOURCE_CHARSET_PREFIX)) {
			int endIndex = location.indexOf(']', URL_RESOURCE_CHARSET_PREFIX.length());
			if (endIndex == -1) {
				throw new IllegalArgumentException("Invalid charset syntax in location: " + location);
			}
			String value = location.substring(URL_RESOURCE_CHARSET_PREFIX.length(), endIndex);
			charset = Charset.forName(value);
			location = location.substring(endIndex + 1);
		}
		Resource resource = applicationContext.getResource(location);
		this.locations.add(resource);
		if (charset != null) {
			if (!(resource instanceof UrlResource)) {
				throw new IllegalArgumentException("Unexpected charset for non-UrlResource: " + resource);
			}
			this.locationCharsets.put(resource, charset);
		}
	}
}
 
Example 3
Source Project: java-technology-stack   File: XmlViewResolver.java    License: MIT License 5 votes vote down vote up
/**
 * Initialize the view bean factory from the XML file.
 * Synchronized because of access by parallel threads.
 * @throws BeansException in case of initialization errors
 */
protected synchronized BeanFactory initFactory() throws BeansException {
	if (this.cachedFactory != null) {
		return this.cachedFactory;
	}

	ApplicationContext applicationContext = obtainApplicationContext();

	Resource actualLocation = this.location;
	if (actualLocation == null) {
		actualLocation = applicationContext.getResource(DEFAULT_LOCATION);
	}

	// Create child ApplicationContext for views.
	GenericWebApplicationContext factory = new GenericWebApplicationContext();
	factory.setParent(applicationContext);
	factory.setServletContext(getServletContext());

	// Load XML resource with context-aware entity resolver.
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
	reader.setEnvironment(applicationContext.getEnvironment());
	reader.setEntityResolver(new ResourceEntityResolver(applicationContext));
	reader.loadBeanDefinitions(actualLocation);

	factory.refresh();

	if (isCache()) {
		this.cachedFactory = factory;
	}
	return factory;
}
 
Example 4
private void resolveResourceLocations() {
	if (CollectionUtils.isEmpty(this.locationValues)) {
		return;
	}
	else if (!CollectionUtils.isEmpty(this.locations)) {
		throw new IllegalArgumentException("Please set either Resource-based \"locations\" or " +
				"String-based \"locationValues\", but not both.");
	}

	ApplicationContext applicationContext = obtainApplicationContext();
	for (String location : this.locationValues) {
		if (this.embeddedValueResolver != null) {
			String resolvedLocation = this.embeddedValueResolver.resolveStringValue(location);
			if (resolvedLocation == null) {
				throw new IllegalArgumentException("Location resolved to null: " + location);
			}
			location = resolvedLocation;
		}
		Charset charset = null;
		location = location.trim();
		if (location.startsWith(URL_RESOURCE_CHARSET_PREFIX)) {
			int endIndex = location.indexOf(']', URL_RESOURCE_CHARSET_PREFIX.length());
			if (endIndex == -1) {
				throw new IllegalArgumentException("Invalid charset syntax in location: " + location);
			}
			String value = location.substring(URL_RESOURCE_CHARSET_PREFIX.length(), endIndex);
			charset = Charset.forName(value);
			location = location.substring(endIndex + 1);
		}
		Resource resource = applicationContext.getResource(location);
		this.locations.add(resource);
		if (charset != null) {
			if (!(resource instanceof UrlResource)) {
				throw new IllegalArgumentException("Unexpected charset for non-UrlResource: " + resource);
			}
			this.locationCharsets.put(resource, charset);
		}
	}
}
 
Example 5
@Test
public void testGetResource() {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/*.xml");
	Resource resource = ctx.getResource("spring/spring-beans.xml");
	Assert.assertNotNull(resource);
	((ClassPathXmlApplicationContext) ctx).close();
}
 
Example 6
Source Project: hasor   File: BuildConfig.java    License: Apache License 2.0 5 votes vote down vote up
public Hasor build(Object parentObject, ApplicationContext applicationContext) throws IOException {
    Hasor hasorBuild = (parentObject == null) ? Hasor.create() : Hasor.create(parentObject);
    hasorBuild.parentClassLoaderWith(applicationContext.getClassLoader());
    //
    // make sure mainConfig
    String config = this.mainConfig;
    if (!StringUtils.isBlank(config)) {
        config = SystemPropertyUtils.resolvePlaceholders(config);
        Resource resource = StringUtils.isNotBlank(config) ? applicationContext.getResource(config) : null;
        if (resource != null) {
            hasorBuild.mainSettingWith(resource.getURI());
        }
    }
    //
    // merge Properties
    if (this.envProperties != null) {
        this.envProperties.forEach((k, v) -> {
            hasorBuild.addVariable(k.toString(), v.toString());
        });
    }
    if (this.refProperties != null) {
        this.refProperties.forEach((k, v) -> {
            hasorBuild.addVariable(k.toString(), v.toString());
        });
    }
    if (this.customProperties != null) {
        this.customProperties.forEach((k, v) -> {
            hasorBuild.addVariable(k.toString(), v.toString());
        });
    }
    //
    // import Properties to Settings
    if (this.useProperties) {
        hasorBuild.importVariablesToSettings();
    }
    //
    return hasorBuild.addModules(this.loadModules);
}
 
Example 7
public WallRideResourceTemplateResource(final ApplicationContext applicationContext, final String location, final String characterEncoding) {
	super();

	Validate.notNull(applicationContext, "Application Context cannot be null");
	Validate.notEmpty(location, "Resource Location cannot be null or empty");
	// Character encoding CAN be null (system default will be used)

	this.resource = applicationContext.getResource(location);
	this.characterEncoding = characterEncoding;
}
 
Example 8
/**
 * ApplicationContext aware version of method
 */
protected Resource getFileResource(String sourceName, ApplicationContext applicationContext) {
    return applicationContext.getResource(sourceName);
}