Skip to content

Commit

Permalink
add: 让spring boot下的Scans基本可用
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed Mar 14, 2017
1 parent fdb2465 commit eaf93fd
Showing 1 changed file with 63 additions and 0 deletions.
@@ -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();
}

}
}

0 comments on commit eaf93fd

Please sign in to comment.