Java 当@PreAuthorize或@PostAuthorize在类中的方法级别上使用时,Class.getAnnotations()不会返回类级别注释的完整列表
假设我有一个带注释的下层类-Java 当@PreAuthorize或@PostAuthorize在类中的方法级别上使用时,Class.getAnnotations()不会返回类级别注释的完整列表,java,xml,spring,annotations,Java,Xml,Spring,Annotations,假设我有一个带注释的下层类- @Component @Path("/somepath/") @Api(value = "/somepath", description = "Operations") public class SomeResourceClass { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @ApiOperation(valu
@Component
@Path("/somepath/")
@Api(value = "/somepath", description = "Operations")
public class SomeResourceClass {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create new Resource",
response = SomeResource.class)
@ApiResponses(value = { @ApiResponse(code = 401, message = "Some Message") })
public SomeResource createResource(@ApiParam(value="someResource", required = true)SomeResource resource) {
}
}
我正在尝试使用下面的代码访问在类级别以上使用的注释列表
List<ClassResourceInfo> classResourceInfos = serverFactoryBean.getServiceFactory().getClassResourceInfo();
for (ClassResourceInfo classResourceInfo : classResourceInfos) {
for (Annotation annotation : classResourceInfo.getResourceClass().getAnnotations()) {
// work with each annotation
}
}
因此,这里我得到了在类级别定义的注释的完整列表
但是现在如果我使用@PreAuthorize on方法,我会得到不完整的列表
@Component
@Path("/somepath/")
@Api(value = "/somepath", description = "Operations")
public class SomeResourceClass {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create new Resource",
response = SomeResource.class)
@ApiResponses(value = { @ApiResponse(code = 401, message = "Some Message") })
@PreAuthorize("hasRole('ADMIN')")
public SomeResource createResource(@ApiParam(value="someResource", required = true)SomeResource resource) {
}
}
在这个场景中,我在类级别上定义的注释列表是-
[@io.swagger.annotations.Api(basePath=, hidden=false, authorizations=[@io.swagger.annotations.Authorization(scopes=[@io.swagger.annotations.AuthorizationScope(scope=, description=)], value=)], produces=, description=Operations, position=0, protocols=, value=/somepath, tags=[], consumes=)]
我已经在web上下文xml文件中定义了SomeResourceClass
<jaxrs:server id="service-rest" address="/">
<jaxrs:serviceBeans>
<ref bean="someResourceClass"/>
</jaxrs:serviceBeans>
</jaxrs:server>
你能帮我指出为什么我在一个方法上使用@PreAuthorize annotation时会得到不完整的类级注释列表吗?看起来这是因为自从你使用@PreAuthorize annotation之后,Spring为你的控制器创建了代理。请参阅文章 仍然可以访问目标类注释:
public class MetricSuppliersLoader implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(SomeAnnotationClass.class);
beans.entrySet().forEach(e -> System.out.println("Bean name: "+e.getKey()));
}
}
公共类度量值供应商Loader实现ApplicationContextAware{
@凌驾
public void setApplicationContext(ApplicationContext ApplicationContext)抛出BeansException{
Map beans=applicationContext.getBeansWithAnnotation(SomeAnnotationClass.class);
Bean.entrySet().forEach(e->System.out.println(“Bean名称:+e.getKey()));
}
}
看起来这是因为Spring为您的控制器创建了代理,因为您使用了@PreAuthorize annotation。请参阅文章
仍然可以访问目标类注释:
public class MetricSuppliersLoader implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(SomeAnnotationClass.class);
beans.entrySet().forEach(e -> System.out.println("Bean name: "+e.getKey()));
}
}
公共类度量值供应商Loader实现ApplicationContextAware{
@凌驾
public void setApplicationContext(ApplicationContext ApplicationContext)抛出BeansException{
Map beans=applicationContext.getBeansWithAnnotation(SomeAnnotationClass.class);
Bean.entrySet().forEach(e->System.out.println(“Bean名称:+e.getKey()));
}
}