Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...-integration-spring/src/main/java/org/nutz/integration/spring/SpringResourceLoaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.nutz.integration.spring; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
import org.nutz.resource.NutResource; | ||
import org.nutz.resource.impl.ResourceLocation; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.core.io.Resource; | ||
|
||
/** | ||
* 用法 Scans.me().add(new SpringResourceLoaction(ctx)); | ||
* | ||
* @author wendal | ||
* | ||
*/ | ||
public class SpringResourceLoaction extends ResourceLocation implements ApplicationContextAware { | ||
|
||
protected ApplicationContext applicationContext; | ||
|
||
public String id() { | ||
return "spring"; | ||
} | ||
|
||
@Override | ||
public void scan(String base, Pattern pattern, List<NutResource> list) { | ||
try { | ||
Resource[] tmp = applicationContext.getResources("classpath*:" + base + "*"); | ||
for (Resource resource : tmp) { | ||
if (resource.getFilename() == null) | ||
continue; | ||
if (pattern != null && !pattern.matcher(resource.getFilename()).find()) { | ||
continue; | ||
} | ||
SpringResource sr = new SpringResource(); | ||
sr.resource = resource; | ||
sr.setName(resource.getFilename()); | ||
sr.setSource("spring"); | ||
list.add(sr); | ||
} | ||
} | ||
catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void setApplicationContext(ApplicationContext applicationContext) { | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
public class SpringResource extends NutResource { | ||
|
||
protected Resource resource; | ||
|
||
public InputStream getInputStream() throws IOException { | ||
return resource.getInputStream(); | ||
} | ||
|
||
} | ||
} |