org.springframework.context.ApplicationContext#getResource ( )源码实例Demo

下面列出了org.springframework.context.ApplicationContext#getResource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: spring-analysis-note   文件: XmlViewResolver.java
/**
 * 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;
}
 
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);
		}
	}
}
 
源代码3 项目: java-technology-stack   文件: XmlViewResolver.java
/**
 * 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;
}
 
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);
		}
	}
}
 
源代码5 项目: spring-tutorial   文件: SpringResoucesTest.java
@Test
public void testGetResource() {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/*.xml");
	Resource resource = ctx.getResource("spring/spring-beans.xml");
	Assert.assertNotNull(resource);
	((ClassPathXmlApplicationContext) ctx).close();
}
 
源代码6 项目: hasor   文件: BuildConfig.java
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);
}
 
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;
}
 
源代码8 项目: kfs   文件: DataDictionary.java
/**
 * ApplicationContext aware version of method
 */
protected Resource getFileResource(String sourceName, ApplicationContext applicationContext) {
    return applicationContext.getResource(sourceName);
}