com.github.hengboy.job.schedule.http.jersey.ScheduleJerseyResourceConfig Maven / Gradle / Ivy
/*
* Copyright [2019] [恒宇少年 - 于起宇]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.hengboy.job.schedule.http.jersey;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.ClassUtils;
import javax.ws.rs.Path;
import java.util.HashSet;
import java.util.Set;
/**
* jersey资源配置
* 该配置类会自动调用无参数构造函数实例化
*
* @author:恒宇少年 - 于起宇
*
* DateTime:2019-02-11 14:33
* Blog:http://blog.yuqiyu.com
* WebSite:http://www.jianshu.com/u/092df3f77bca
* Gitee:https://gitee.com/hengboy
* GitHub:https://github.com/hengyuboy
*/
public class ScheduleJerseyResourceConfig extends ResourceConfig {
/**
* 包含任务注册中心所需的Jersey资源的包列表
*/
private static final String[] SCHEDULE_IMPL_PACKAGES = new String[]{"com.github.hengboy.job.schedule.http.resource"};
/**
* 构造函数初始化注册资源class
* 反射扫描指定package下的所有符合条件的类列表进行注册
*/
public ScheduleJerseyResourceConfig() {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
false);
// Filter to include only classes that have a particular annotation.
//
provider.addIncludeFilter(new AnnotationTypeFilter(Path.class));
// Find classes in Registry packages (or subpackages)
//
Set> classes = new HashSet<>();
for (String basePackage : SCHEDULE_IMPL_PACKAGES) {
Set beans = provider.findCandidateComponents(basePackage);
for (BeanDefinition bd : beans) {
Class> cls = ClassUtils.resolveClassName(bd.getBeanClassName(),
this.getClass().getClassLoader());
classes.add(cls);
}
}
registerClasses(classes);
}
}