本文整理汇总了Java中org.springframework.core.annotation.AnnotationUtils类的典型用法代码示例。如果您正苦于以下问题:Java AnnotationUtils类的具体用法?Java AnnotationUtils怎么用?Java AnnotationUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AnnotationUtils类属于org.springframework.core.annotation包,在下文中一共展示了AnnotationUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processException
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorVM> processException(Exception ex) {
log.error("An unexpected error occurred: {}", ex.getMessage(), ex);
BodyBuilder builder;
ErrorVM errorVM;
ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);
if (responseStatus != null) {
builder = ResponseEntity.status(responseStatus.value());
errorVM = new ErrorVM(ERROR_PREFIX + responseStatus.value().value(),
translate(ERROR_PREFIX + responseStatus.value().value()));
} else {
builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
errorVM = new ErrorVM(ErrorConstants.ERR_INTERNAL_SERVER_ERROR,
translate(ErrorConstants.ERR_INTERNAL_SERVER_ERROR));
}
return builder.body(errorVM);
}
示例2: processException
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorVM> processException(Exception ex) {
if (log.isDebugEnabled()) {
log.debug("An unexpected error occurred: {}", ex.getMessage(), ex);
} else {
log.error("An unexpected error occurred: {}", ex.getMessage());
}
BodyBuilder builder;
ErrorVM errorVM;
ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);
if (responseStatus != null) {
builder = ResponseEntity.status(responseStatus.value());
errorVM = new ErrorVM("error." + responseStatus.value().value(), responseStatus.reason());
} else {
builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
errorVM = new ErrorVM(ErrorConstants.ERR_INTERNAL_SERVER_ERROR, "Internal server error");
}
return builder.body(errorVM);
}
示例3: injectServicesViaAnnotatedFields
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
private void injectServicesViaAnnotatedFields(final Object bean, final String beanName) {
ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
public void doWith(Field field) {
ServiceReference s = AnnotationUtils.getAnnotation(field, ServiceReference.class);
if (s != null && !Modifier.isStatic(field.getModifiers()) && !Modifier.isFinal(field.getModifiers())) {
try {
if (logger.isDebugEnabled())
logger.debug("Processing annotation [" + s + "] for [" + field + "] on bean [" + beanName + "]");
if (!field.isAccessible()) {
field.setAccessible(true);
}
ReflectionUtils.setField(field, bean, getServiceImporter(s, field.getType(), beanName).getObject());
}
catch (Exception e) {
throw new IllegalArgumentException("Error processing service annotation", e);
}
}
}
});
}
示例4: processException
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorVM> processException(Exception ex) {
if (log.isDebugEnabled()) {
log.debug("An unexpected error occured: {}", ex.getMessage(), ex);
} else {
log.error("An unexpected error occured: {}", ex.getMessage());
}
BodyBuilder builder;
ErrorVM errorVM;
ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);
if (responseStatus != null) {
builder = ResponseEntity.status(responseStatus.value());
errorVM = new ErrorVM("error." + responseStatus.value().value(), responseStatus.reason());
} else {
builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
errorVM = new ErrorVM(ErrorConstants.ERR_INTERNAL_SERVER_ERROR, "Internal server error");
}
return builder.body(errorVM);
}
示例5: processRuntimeException
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorVM> processRuntimeException(Exception ex) {
BodyBuilder builder;
ErrorVM errorVM;
ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);
if (responseStatus != null) {
builder = ResponseEntity.status(responseStatus.value());
errorVM = new ErrorVM("error." + responseStatus.value().value(), responseStatus.reason());
} else {
builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
errorVM = new ErrorVM(ErrorConstants.ERR_INTERNAL_SERVER_ERROR, "Internal server error");
}
return builder.body(errorVM);
}
示例6: collectParameters
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
private static void collectParameters(Collection<Parameters> parameters, Parameter parameter, Annotation a,
boolean isPathVariable) {
if (a != null) {
String typeStr = parameter.getType().getSimpleName();
Type type = parameter.getParameterizedType();
if (type instanceof ParameterizedType) {
typeStr = ((Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]).getSimpleName();
}
parameters.add(new Parameters((boolean) AnnotationUtils.getValue(a, "required"),
(String) (AnnotationUtils.getValue(a).equals("") ? parameter.getName()
: AnnotationUtils.getValue(a)),
typeStr));
} else if (Pageable.class.isAssignableFrom(parameter.getType()) && !isPathVariable) {
try {
for (PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(parameter.getType())
.getPropertyDescriptors()) {
parameters.add(new Parameters(false, propertyDescriptor.getName(),
propertyDescriptor.getPropertyType().getSimpleName()));
}
} catch (IntrospectionException e) {
LOGGER.error("Problemas al obtener el Pageable: {}", parameter, e);
}
}
}
示例7: intercept
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
@Around("interceptDao()")
public Object intercept(ProceedingJoinPoint joinPoint) throws Throwable {
Object result = null;
try {
Class clazz = MethodSignature.class.cast(joinPoint.getSignature()).getDeclaringType();
DynamicDS targetDataSource = AnnotationUtils.findAnnotation(clazz, DynamicDS.class);
if (targetDataSource != null) {
DataSourceContextHolder.setTargetDataSource(DataSourceEnum.valueOf(targetDataSource.value()));
} else {
DataSourceContextHolder.resetDefaultDataSource();
}
result = joinPoint.proceed();
return result;
} catch (Throwable ex) {
throw new RuntimeException(ex);
}
}
示例8: defaultErrorHandler
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
logger.error("[URL] : {}", req.getRequestURL(), e);
// If the exception is annotated with @ResponseStatus rethrow it and let
// the framework handle it - like the OrderNotFoundException example
// at the start of this post.
// AnnotationUtils is a Spring Framework utility class.
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)
throw e;
// Otherwise setup and send the user to a default error-view.
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("url", req.getRequestURL());
mav.setViewName(DEFAULT_ERROR_VIEW);
return mav;
}
示例9: registerFactoryMethodForConfigBean
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
/**
* 为@ConfigBean的bean注册它的Factory Method,通过静态Factory Method来创建@ConfigBean实例
*
* @param registry
* @param beanName
* @param beanDefinition
*/
private void registerFactoryMethodForConfigBean(BeanDefinitionRegistry registry, String beanName, BeanDefinition beanDefinition) {
String beanClassName = beanDefinition.getBeanClassName();
if (beanClassName == null) { // 通过注解@Bean声明的bean,beanClassName=null
return;
}
Class<?> beanClass = ClassUtils.resolveClassName(beanClassName, beanFactory.getBeanClassLoader());
ConfigBean config = AnnotationUtils.findAnnotation(beanClass, ConfigBean.class);
if (config == null) {
return;
}
// 为配置bean设置factory method
String propertyName = config.value();
ConfigBeanConfigUtils.setConfigBeanFactoryMethod(registry,
beanName, beanDefinition, propertyName, config.converter());
}
示例10: findMethodsByAnnotation
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
/**
* Finds methods for the given annotation
*
* It first finds all public member methods of the class or interface represented by objClass,
* including those inherited from superclasses and superinterfaces.
*
* It then loops through these methods searching for a single Annotation of annotationType,
* traversing its super methods if no annotation can be found on the given method itself.
*
* @param objClass - the class
* @param annotationType - the annotation to find
* @return - the List of Method or an empty List
*/
@SuppressWarnings("rawtypes")
public static List<Method> findMethodsByAnnotation(Class objClass, Class<? extends Annotation> annotationType)
{
List<Method> annotatedMethods = new ArrayList<Method>();
Method[] methods = objClass.getMethods();
for (Method method : methods)
{
Annotation annot = AnnotationUtils.findAnnotation(method, annotationType);
if (annot != null) {
//Just to be sure, lets make sure its not a Bridged (Generic) Method
Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
annotatedMethods.add(resolvedMethod);
}
}
return annotatedMethods;
}
示例11: postProcessBeanFactory
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
logger.info("Look for Errai Service definitions");
String[] beans = beanFactory.getBeanDefinitionNames();
for (String beanName : beans) {
Class<?> beanType = beanFactory.getType(beanName);
Service service = AnnotationUtils.findAnnotation(beanType, Service.class);
if (service != null) {
try {
ServiceTypeParser serviceTypeParser = new ServiceTypeParser(beanType);
services.add(new ServiceImplementation(serviceTypeParser, beanName));
logger.debug("Found Errai Service definition: beanName=" + beanName + ", beanType=" + beanType);
} catch (NotAService e) {
logger.warn("Service annotation present but threw NotAServiceException", e);
}
}
}
}
示例12: injectServicesViaAnnotatedSetterMethods
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
private void injectServicesViaAnnotatedSetterMethods(final Object bean, final String beanName) {
ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
public void doWith(Method method) {
ServiceReference s = AnnotationUtils.getAnnotation(method, ServiceReference.class);
if (s != null && method.getParameterTypes().length == 1) {
try {
if (logger.isDebugEnabled())
logger.debug("Processing annotation [" + s + "] for [" + bean.getClass().getName() + "."
+ method.getName() + "()] on bean [" + beanName + "]");
method.invoke(bean, getServiceImporter(s, method, beanName).getObject());
}
catch (Exception e) {
throw new IllegalArgumentException("Error processing service annotation", e);
}
}
}
});
}
示例13: tstGetServicePropertySetters
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
/**
* Disabled since it doesn't work as we can't proxy final classes.
*/
public void tstGetServicePropertySetters() throws Exception {
OsgiServiceProxyFactoryBean pfb = new OsgiServiceProxyFactoryBean();
Method setter = AnnotatedBean.class.getMethod("setStringType", new Class<?>[] { String.class });
ServiceReference ref = AnnotationUtils.getAnnotation(setter, ServiceReference.class);
processor.getServiceProperty(pfb, ref, setter.getParameterTypes()[0], null);
Class<?>[] intfs = (Class[]) getPrivateProperty(pfb, "serviceTypes");
assertEquals(intfs[0], String.class);
setter = AnnotatedBean.class.getMethod("setIntType", new Class<?>[] { Integer.TYPE });
ref = AnnotationUtils.getAnnotation(setter, ServiceReference.class);
pfb = new OsgiServiceProxyFactoryBean();
processor.getServiceProperty(pfb, ref, setter.getParameterTypes()[0], null);
intfs = (Class[]) getPrivateProperty(pfb, "serviceTypes");
assertEquals(intfs[0], Integer.TYPE);
}
示例14: testProperMultiCardinality
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
public void testProperMultiCardinality() throws Exception {
OsgiServiceCollectionProxyFactoryBean pfb = new OsgiServiceCollectionProxyFactoryBean();
Method setter = AnnotatedBean.class.getMethod("setAnnotatedBeanTypeWithCardinality0_N",
new Class<?>[] { List.class });
ServiceReference ref = AnnotationUtils.getAnnotation(setter, ServiceReference.class);
processor.getServiceProperty(pfb, ref, setter.getParameterTypes()[0], null);
assertFalse(pfb.getAvailability() == Availability.MANDATORY);
setter = AnnotatedBean.class.getMethod("setAnnotatedBeanTypeWithCardinality1_N",
new Class<?>[] { SortedSet.class });
ref = AnnotationUtils.getAnnotation(setter, ServiceReference.class);
pfb = new OsgiServiceCollectionProxyFactoryBean();
processor.getServiceProperty(pfb, ref, setter.getParameterTypes()[0], null);
assertTrue(pfb.getAvailability() == Availability.MANDATORY);
}
示例15: testGetServicePropertyClassloader
import org.springframework.core.annotation.AnnotationUtils; //导入依赖的package包/类
public void testGetServicePropertyClassloader() throws Exception {
OsgiServiceProxyFactoryBean pfb = new OsgiServiceProxyFactoryBean();
Method setter = AnnotatedBean.class.getMethod("setAnnotatedBeanTypeWithClassLoaderClient",
new Class<?>[] { AnnotatedBean.class });
ServiceReference ref = AnnotationUtils.getAnnotation(setter, ServiceReference.class);
processor.getServiceProperty(pfb, ref, setter.getParameterTypes()[0], null);
assertEquals(pfb.getImportContextClassLoader(), ImportContextClassLoaderEnum.CLIENT);
pfb = new OsgiServiceProxyFactoryBean();
setter = AnnotatedBean.class.getMethod("setAnnotatedBeanTypeWithClassLoaderUmanaged",
new Class<?>[] { AnnotatedBean.class });
ref = AnnotationUtils.getAnnotation(setter, ServiceReference.class);
processor.getServiceProperty(pfb, ref, setter.getParameterTypes()[0], null);
assertEquals(pfb.getImportContextClassLoader(), ImportContextClassLoaderEnum.UNMANAGED);
pfb = new OsgiServiceProxyFactoryBean();
setter = AnnotatedBean.class.getMethod("setAnnotatedBeanTypeWithClassLoaderServiceProvider",
new Class<?>[] { AnnotatedBean.class });
ref = AnnotationUtils.getAnnotation(setter, ServiceReference.class);
processor.getServiceProperty(pfb, ref, setter.getParameterTypes()[0], null);
assertEquals(pfb.getImportContextClassLoader(), ImportContextClassLoaderEnum.SERVICE_PROVIDER);
}