下面列出了怎么用org.springframework.core.io.FileSystemResourceLoader的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* configures logging using custom properties file if specified, or the default one.
* Log4j also uses any file called log4.properties in the classpath
*
* <p>To configure a custom logging file, set a JVM system property on using -D. For example
* -Dalt.log4j.config.location=file:/home/me/kuali/test/dev/log4j.properties
* </p>
*
* <p>The above option can also be set in the run configuration for the unit test in the IDE.
* To avoid log4j using files called log4j.properties that are defined in the classpath, add the following system property:
* -Dlog4j.defaultInitOverride=true
* </p>
* @throws IOException
*/
protected void configureLogging() throws IOException {
ResourceLoader resourceLoader = new FileSystemResourceLoader();
String altLog4jConfigLocation = System.getProperty(ALT_LOG4J_CONFIG_LOCATION_PROP);
Resource log4jConfigResource = null;
if (!StringUtils.isEmpty(altLog4jConfigLocation)) {
log4jConfigResource = resourceLoader.getResource(altLog4jConfigLocation);
}
if (log4jConfigResource == null || !log4jConfigResource.exists()) {
System.out.println("Alternate Log4j config resource does not exist! " + altLog4jConfigLocation);
System.out.println("Using default log4j configuration: " + DEFAULT_LOG4J_CONFIG);
log4jConfigResource = resourceLoader.getResource(DEFAULT_LOG4J_CONFIG);
} else {
System.out.println("Using alternate log4j configuration at: " + altLog4jConfigLocation);
}
Properties p = new Properties();
p.load(log4jConfigResource.getInputStream());
PropertyConfigurator.configure(p);
}
@Before
public void setUp() {
this.context = new StaticWebApplicationContext();
this.context.setServletContext(new MockServletContext(new FileSystemResourceLoader()));
this.context.registerSingleton("controller", TestController.class);
this.context.registerSingleton("userController", UserController.class);
this.config = new TestWebMvcConfigurationSupport();
this.config.setApplicationContext(this.context);
this.config.setServletContext(this.context.getServletContext());
}
public static void main(String[] args) throws IOException {
// 读取当前 package 对应的所有的 .java 文件
// *.java
String currentPackagePath = "/" + System.getProperty("user.dir") + "/thinking-in-spring/resource/src/main/java/org/geekbang/thinking/in/spring/resource/";
String locationPattern = currentPackagePath + "*.java";
PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(new FileSystemResourceLoader());
resourcePatternResolver.setPathMatcher(new JavaFilePathMatcher());
Resource[] resources = resourcePatternResolver.getResources(locationPattern);
Stream.of(resources).map(ResourceUtils::getContent).forEach(System.out::println);
}
public static void main(String[] args) throws IOException {
String currentJavaFilePath = "/" + System.getProperty("user.dir") + "/thinking-in-spring/resource/src/main/java/org/geekbang/thinking/in/spring/resource/EncodedFileSystemResourceLoaderDemo.java";
// 新建一个 FileSystemResourceLoader 对象
FileSystemResourceLoader resourceLoader = new FileSystemResourceLoader();
// FileSystemResource => WritableResource => Resource
Resource resource = resourceLoader.getResource(currentJavaFilePath);
EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
// 字符输入流
try (Reader reader = encodedResource.getReader()) {
System.out.println(IOUtils.toString(reader));
}
}
@Before
public void setUp() {
this.context = new StaticWebApplicationContext();
this.context.setServletContext(new MockServletContext(new FileSystemResourceLoader()));
this.context.registerSingleton("controller", TestController.class);
this.context.registerSingleton("userController", UserController.class);
this.config = new TestWebMvcConfigurationSupport();
this.config.setApplicationContext(this.context);
this.config.setServletContext(this.context.getServletContext());
}
@Bean
public ResourceLoader resourceLoader() {
MavenProperties mavenProperties = new MavenProperties();
mavenProperties.setRemoteRepositories(new HashMap<>(Collections.singletonMap("springRepo",
new MavenProperties.RemoteRepository("https://repo.spring.io/libs-snapshot"))));
Map<String, ResourceLoader> resourceLoaders = new HashMap<>();
resourceLoaders.put("maven", new MavenResourceLoader(mavenProperties));
resourceLoaders.put("file", new FileSystemResourceLoader());
DelegatingResourceLoader delegatingResourceLoader = new DelegatingResourceLoader(resourceLoaders);
return delegatingResourceLoader;
}
@Before
public void setUp() {
this.context = new StaticWebApplicationContext();
this.context.setServletContext(new MockServletContext(new FileSystemResourceLoader()));
this.context.registerSingleton("controller", TestController.class);
this.config = new TestWebMvcConfigurationSupport();
this.config.setApplicationContext(this.context);
this.config.setServletContext(this.context.getServletContext());
}
private void initLogging(String location, boolean refreshInterval) {
MockServletContext sc = new MockServletContext("", new FileSystemResourceLoader());
sc.addInitParameter(Log4jWebConfigurer.CONFIG_LOCATION_PARAM, location);
if (refreshInterval) {
sc.addInitParameter(Log4jWebConfigurer.REFRESH_INTERVAL_PARAM, "10");
}
Log4jWebConfigurer.initLogging(sc);
try {
assertLogOutput();
} finally {
Log4jWebConfigurer.shutdownLogging(sc);
}
assertTrue(MockLog4jAppender.closeCalled);
}
@Test
public void testLog4jConfigListener() {
Log4jConfigListener listener = new Log4jConfigListener();
MockServletContext sc = new MockServletContext("", new FileSystemResourceLoader());
sc.addInitParameter(Log4jWebConfigurer.CONFIG_LOCATION_PARAM, RELATIVE_PATH);
listener.contextInitialized(new ServletContextEvent(sc));
try {
assertLogOutput();
} finally {
listener.contextDestroyed(new ServletContextEvent(sc));
}
assertTrue(MockLog4jAppender.closeCalled);
}
@Bean
public DelegatingResourceLoader resourceLoader(MavenProperties mavenProperties) {
Map<String, ResourceLoader> resourceLoaders = new HashMap<>();
resourceLoaders.put("maven", new MavenResourceLoader(mavenProperties));
resourceLoaders.put("file", new FileSystemResourceLoader());
DelegatingResourceLoader delegatingResourceLoader = new DelegatingResourceLoader(resourceLoaders);
return delegatingResourceLoader;
}
@Before
public void setUp() throws Exception {
MockServletContext servletContext = new MockServletContext("", new FileSystemResourceLoader());
new ConfigTestUtils().createApplicationContext(servletContext);
MockitoAnnotations.initMocks(this);
this.userProfileManager.setEntityClassName(className);
this.userProfileManager.setConfigItemName(configItemName);
this.userProfileManager.setBeanName(this.beanName);
}
@Override
protected void setUp() throws Exception {
try {
super.setUp();
ServletContext srvCtx = new MockServletContext("", new FileSystemResourceLoader());
ApplicationContext applicationContext = this.getConfigUtils().createApplicationContext(srvCtx);
this.setApplicationContext(applicationContext);
RequestContext reqCtx = createRequestContext(applicationContext, srvCtx);
this.setRequestContext(reqCtx);
this.setUserOnSession("guest");
} catch (Exception e) {
throw e;
}
}
@Override
protected void setUp() throws Exception {
boolean refresh = false;
if (null == applicationContext) {
// Link the servlet context and the Spring context
servletContext = new MockServletContext("", new FileSystemResourceLoader());
applicationContext = this.getConfigUtils().createApplicationContext(servletContext);
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
} else {
refresh = true;
}
RequestContext reqCtx = BaseTestCase.createRequestContext(applicationContext, servletContext);
this.request = new MockHttpServletRequest();
this.request.setAttribute(RequestContext.REQCTX, reqCtx);
this.response = new MockHttpServletResponse();
this.request.setSession(new MockHttpSession(servletContext));
if (refresh) {
try {
ApsWebApplicationUtils.executeSystemRefresh(this.request);
this.waitNotifyingThread();
} catch (Throwable e) {
}
}
// Use spring as the object factory for Struts
StrutsSpringObjectFactory ssf = new StrutsSpringObjectFactory(null, null, null, null, servletContext, null, this.createContainer());
ssf.setApplicationContext(applicationContext);
// Dispatcher is the guy that actually handles all requests. Pass in
// an empty Map as the parameters but if you want to change stuff like
// what config files to read, you need to specify them here
// (see Dispatcher's source code)
java.net.URL url = ClassLoader.getSystemResource("struts.properties");
Properties props = new Properties();
props.load(url.openStream());
this.setInitParameters(props);
Map params = new HashMap(props);
this.dispatcher = new Dispatcher(servletContext, params);
this.dispatcher.init();
Dispatcher.setInstance(this.dispatcher);
}
/**
* This method consolidates config properties.
* @param properties
* @return Properties
* @throws IOException
*/
protected Properties transformProperties(Properties properties) throws IOException {
String cfgLocation = System.getProperty(CFG_LOCATION_PROPERTY);
if (cfgLocation != null) {
Resource resource = new FileSystemResourceLoader().getResource(cfgLocation);
if (resource != null && resource.exists()) {
new DefaultPropertiesPersister().load(properties, resource.getInputStream());
}
}
return properties;
}
@Before
public void initializeWorkspaceInitializer() {
workspaceInitializer.setServletContext(new MockServletContext(WAR_BASE_PATH, new FileSystemResourceLoader()));
workspaceInitializer.setMigrations(Arrays.<LiveRepositoryUpdate>asList(pageRepositoryLiveUpdate, widgetRepositoryLiveUpdate));
}
@Before
public void setup() {
fileMonitorConfiguration.setResourceLoader(new FileSystemResourceLoader());
}
public GenericWebContextLoader(String warRootDir, boolean isClasspathRelative) {
ResourceLoader resourceLoader = isClasspathRelative ? new DefaultResourceLoader() : new FileSystemResourceLoader();
this.servletContext = initServletContext(warRootDir, resourceLoader);
}
public GenericWebContextLoader(String warRootDir, boolean isClasspathRelative) {
ResourceLoader resourceLoader = isClasspathRelative ? new DefaultResourceLoader() : new FileSystemResourceLoader();
this.servletContext = initServletContext(warRootDir, resourceLoader);
}
public GenericWebContextLoader(String warRootDir, boolean isClasspathRelative) {
ResourceLoader resourceLoader = isClasspathRelative ? new DefaultResourceLoader() : new FileSystemResourceLoader();
this.servletContext = initServletContext(warRootDir, resourceLoader);
}
private ResourceLoader createResourceLoader() {
return new FileSystemResourceLoader();
}