use of org.springframework.core.io.support.ResourcePatternResolver in project tomee by apache.
the class SpringClasspathScanner method findClassesInternal.
@Override
protected Map<Class<? extends Annotation>, Collection<Class<?>>> findClassesInternal(Collection<String> basePackages, List<Class<? extends Annotation>> annotations, ClassLoader loader) throws IOException, ClassNotFoundException {
ResourcePatternResolver resolver = getResolver(loader);
MetadataReaderFactory factory = new CachingMetadataReaderFactory(resolver);
final Map<Class<? extends Annotation>, Collection<Class<?>>> classes = new HashMap<>();
final Map<Class<? extends Annotation>, Collection<String>> matchingInterfaces = new HashMap<>();
final Map<String, String[]> nonMatchingClasses = new HashMap<>();
for (Class<? extends Annotation> annotation : annotations) {
classes.put(annotation, new HashSet<>());
matchingInterfaces.put(annotation, new HashSet<>());
}
if (basePackages == null || basePackages.isEmpty()) {
return classes;
}
for (final String basePackage : basePackages) {
final boolean scanAllPackages = basePackage.equals(WILDCARD);
final String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + (scanAllPackages ? "" : ClassUtils.convertClassNameToResourcePath(basePackage)) + ALL_CLASS_FILES;
final Resource[] resources = resolver.getResources(packageSearchPath);
for (final Resource resource : resources) {
final MetadataReader reader = factory.getMetadataReader(resource);
final AnnotationMetadata metadata = reader.getAnnotationMetadata();
if (scanAllPackages && shouldSkip(metadata.getClassName())) {
continue;
}
for (Class<? extends Annotation> annotation : annotations) {
boolean concreteClass = !metadata.isInterface() && !metadata.isAbstract();
if (metadata.isAnnotated(annotation.getName())) {
if (concreteClass) {
classes.get(annotation).add(loadClass(metadata.getClassName(), loader));
} else {
matchingInterfaces.get(annotation).add(metadata.getClassName());
}
} else if (concreteClass && metadata.getInterfaceNames().length > 0) {
nonMatchingClasses.put(metadata.getClassName(), metadata.getInterfaceNames());
}
}
}
}
if (!nonMatchingClasses.isEmpty()) {
for (Map.Entry<Class<? extends Annotation>, Collection<String>> e1 : matchingInterfaces.entrySet()) {
for (Map.Entry<String, String[]> e2 : nonMatchingClasses.entrySet()) {
for (String intName : e2.getValue()) {
if (e1.getValue().contains(intName)) {
classes.get(e1.getKey()).add(loadClass(e2.getKey(), loader));
break;
}
}
}
}
}
for (Map.Entry<Class<? extends Annotation>, Collection<String>> e : matchingInterfaces.entrySet()) {
if (classes.get(e.getKey()).isEmpty()) {
for (String intName : e.getValue()) {
classes.get(e.getKey()).add(loadClass(intName, loader));
}
}
}
return classes;
}
use of org.springframework.core.io.support.ResourcePatternResolver in project Asqatasun by Asqatasun.
the class WebAppConfiguration method reloadableResourceBundleMessageSource.
// /**
// * Redirect '/' to login page
// * @param registry
// */
// @Override
// public void addViewControllers(ViewControllerRegistry registry) {
// registry.addViewController("").setViewName("login");
// registry.addViewController("/").setViewName("login");
// }
/**
* Load all i18n file from the classpath
* @return
*/
@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource() {
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = new Resource[0];
// load dynamically all i18 files. They have to be in the classpath under an i18n folder.
try {
resources = resolver.getResources("classpath*:i18n/*I18N.properties");
} catch (IOException e) {
e.printStackTrace();
}
List<String> baseNames = new ArrayList<>();
for (Resource resource : resources) {
baseNames.add("classpath:i18n/" + StringUtils.remove(resource.getFilename(), ".properties"));
}
ReloadableResourceBundleMessageSource bundleMessageSource = new ReloadableResourceBundleMessageSource();
bundleMessageSource.setDefaultEncoding("UTF-8");
bundleMessageSource.setBasenames(baseNames.toArray(new String[baseNames.size()]));
return bundleMessageSource;
}
use of org.springframework.core.io.support.ResourcePatternResolver in project mybatis.flying by limeng32.
the class SpringBootVFS method list.
@Override
protected List<String> list(URL url, String path) throws IOException {
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = resolver.getResources(path + "/**/*.class");
List<Resource> resources1 = Arrays.asList(resources);
List<String> resourcePaths = new ArrayList<String>();
for (Resource resource : resources1) {
resourcePaths.add(preserveSubpackageName(resource.getURI(), path));
}
return resourcePaths;
}
use of org.springframework.core.io.support.ResourcePatternResolver in project cxf by apache.
the class SpringClasspathScanner method findClassesInternal.
@Override
protected Map<Class<? extends Annotation>, Collection<Class<?>>> findClassesInternal(Collection<String> basePackages, List<Class<? extends Annotation>> annotations, ClassLoader loader) throws IOException, ClassNotFoundException {
ResourcePatternResolver resolver = getResolver(loader);
MetadataReaderFactory factory = new CachingMetadataReaderFactory(resolver);
final Map<Class<? extends Annotation>, Collection<Class<?>>> classes = new HashMap<>();
final Map<Class<? extends Annotation>, Collection<String>> matchingInterfaces = new HashMap<>();
final Map<String, String[]> nonMatchingClasses = new HashMap<>();
for (Class<? extends Annotation> annotation : annotations) {
classes.put(annotation, new HashSet<>());
matchingInterfaces.put(annotation, new HashSet<>());
}
if (basePackages == null || basePackages.isEmpty()) {
return classes;
}
for (final String basePackage : basePackages) {
final boolean scanAllPackages = basePackage.equals(WILDCARD);
final String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + (scanAllPackages ? "" : ClassUtils.convertClassNameToResourcePath(basePackage)) + ALL_CLASS_FILES;
final Resource[] resources = resolver.getResources(packageSearchPath);
for (final Resource resource : resources) {
final MetadataReader reader = factory.getMetadataReader(resource);
final AnnotationMetadata metadata = reader.getAnnotationMetadata();
if (scanAllPackages && shouldSkip(metadata.getClassName())) {
continue;
}
for (Class<? extends Annotation> annotation : annotations) {
boolean concreteClass = !metadata.isInterface() && !metadata.isAbstract();
if (metadata.isAnnotated(annotation.getName())) {
if (concreteClass) {
classes.get(annotation).add(loadClass(metadata.getClassName(), loader));
} else {
matchingInterfaces.get(annotation).add(metadata.getClassName());
}
} else if (concreteClass && metadata.getInterfaceNames().length > 0) {
nonMatchingClasses.put(metadata.getClassName(), metadata.getInterfaceNames());
}
}
}
}
if (!nonMatchingClasses.isEmpty()) {
for (Map.Entry<Class<? extends Annotation>, Collection<String>> e1 : matchingInterfaces.entrySet()) {
for (Map.Entry<String, String[]> e2 : nonMatchingClasses.entrySet()) {
for (String intName : e2.getValue()) {
if (e1.getValue().contains(intName)) {
classes.get(e1.getKey()).add(loadClass(e2.getKey(), loader));
break;
}
}
}
}
}
for (Map.Entry<Class<? extends Annotation>, Collection<String>> e : matchingInterfaces.entrySet()) {
if (classes.get(e.getKey()).isEmpty()) {
for (String intName : e.getValue()) {
classes.get(e.getKey()).add(loadClass(intName, loader));
}
}
}
return classes;
}
use of org.springframework.core.io.support.ResourcePatternResolver in project sinsim by WilsonHu.
the class MybatisConfig method sqlSessionFactoryBean.
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setTypeAliasesPackage(MODEL_PACKAGE + "/*");
// 配置分页插件,详情请查阅官方文档
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
// 分页尺寸为0时查询所有纪录不再执行分页
properties.setProperty("pageSizeZero", "true");
// 页码<=0 查询第一页,页码>=总页数查询最后一页
properties.setProperty("reasonable", "true");
// 支持通过 Mapper 接口参数来传递分页参数
properties.setProperty("supportMethodsArguments", "true");
pageHelper.setProperties(properties);
// 添加插件
factory.setPlugins(new Interceptor[] { pageHelper });
// 添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
// /通过Java配置的方式是通过setConfiguration/setConfigLocation,而setConfigurationProperties只是
// 配置文件中的一个property。所以mybatis的配置,要不通过XML的location方式,要不就是setConfiguration后
// 加上property的方式Properties
// mybatisProperties = new Properties();
// mybatisProperties.setProperty("mapUnderscoreToCamelCase", "true");
// factory.setConfigurationProperties(mybatisProperties);
factory.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
return factory.getObject();
}
Aggregations