Search in sources :

Example 6 with Component

use of org.springframework.stereotype.Component in project spring-framework by spring-projects.

the class AnnotationUtilsTests method findClassAnnotationOnAnnotatedClassWithMissingTargetMetaAnnotation.

@Test
public void findClassAnnotationOnAnnotatedClassWithMissingTargetMetaAnnotation() {
    // TransactionalClass is NOT annotated or meta-annotated with @Component
    Component component = findAnnotation(TransactionalClass.class, Component.class);
    assertNull("Should not find @Component on TransactionalClass", component);
}
Also used : Component(org.springframework.stereotype.Component) Test(org.junit.Test)

Example 7 with Component

use of org.springframework.stereotype.Component in project spring-framework by spring-projects.

the class AnnotationUtilsTests method getAnnotationAttributesWithoutAttributeAliases.

@Test
public void getAnnotationAttributesWithoutAttributeAliases() {
    Component component = WebController.class.getAnnotation(Component.class);
    assertNotNull(component);
    AnnotationAttributes attributes = (AnnotationAttributes) getAnnotationAttributes(component);
    assertNotNull(attributes);
    assertEquals("value attribute: ", "webController", attributes.getString(VALUE));
    assertEquals(Component.class, attributes.annotationType());
}
Also used : Component(org.springframework.stereotype.Component) Test(org.junit.Test)

Example 8 with Component

use of org.springframework.stereotype.Component in project spring-framework by spring-projects.

the class MetaAnnotationUtilsTests method assertAtComponentOnComposedAnnotationForMultipleCandidateTypes.

@SuppressWarnings("unchecked")
private void assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(Class<?> startClass, Class<?> rootDeclaringClass, Class<?> declaringClass, String name, Class<? extends Annotation> composedAnnotationType) {
    Class<Component> annotationType = Component.class;
    UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(startClass, Service.class, annotationType, Order.class, Transactional.class);
    assertNotNull("UntypedAnnotationDescriptor should not be null", descriptor);
    assertEquals("rootDeclaringClass", rootDeclaringClass, descriptor.getRootDeclaringClass());
    assertEquals("declaringClass", declaringClass, descriptor.getDeclaringClass());
    assertEquals("annotationType", annotationType, descriptor.getAnnotationType());
    assertEquals("component name", name, ((Component) descriptor.getAnnotation()).value());
    assertNotNull("composedAnnotation should not be null", descriptor.getComposedAnnotation());
    assertEquals("composedAnnotationType", composedAnnotationType, descriptor.getComposedAnnotationType());
}
Also used : Component(org.springframework.stereotype.Component) UntypedAnnotationDescriptor(org.springframework.test.util.MetaAnnotationUtils.UntypedAnnotationDescriptor)

Example 9 with Component

use of org.springframework.stereotype.Component in project spring-framework by spring-projects.

the class SpringConfigurator method getEndpointInstance.

@SuppressWarnings("unchecked")
@Override
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
    if (wac == null) {
        String message = "Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?";
        logger.error(message);
        throw new IllegalStateException(message);
    }
    String beanName = ClassUtils.getShortNameAsProperty(endpointClass);
    if (wac.containsBean(beanName)) {
        T endpoint = wac.getBean(beanName, endpointClass);
        if (logger.isTraceEnabled()) {
            logger.trace("Using @ServerEndpoint singleton " + endpoint);
        }
        return endpoint;
    }
    Component ann = AnnotationUtils.findAnnotation(endpointClass, Component.class);
    if (ann != null && wac.containsBean(ann.value())) {
        T endpoint = wac.getBean(ann.value(), endpointClass);
        if (logger.isTraceEnabled()) {
            logger.trace("Using @ServerEndpoint singleton " + endpoint);
        }
        return endpoint;
    }
    beanName = getBeanNameByType(wac, endpointClass);
    if (beanName != null) {
        return (T) wac.getBean(beanName);
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Creating new @ServerEndpoint instance of type " + endpointClass);
    }
    return wac.getAutowireCapableBeanFactory().createBean(endpointClass);
}
Also used : Component(org.springframework.stereotype.Component) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 10 with Component

use of org.springframework.stereotype.Component in project spring-framework by spring-projects.

the class AnnotationUtilsTests method synthesizeAnnotationFromMapWithoutAttributeAliases.

@Test
public void synthesizeAnnotationFromMapWithoutAttributeAliases() throws Exception {
    Component component = WebController.class.getAnnotation(Component.class);
    assertNotNull(component);
    Map<String, Object> map = Collections.singletonMap(VALUE, "webController");
    Component synthesizedComponent = synthesizeAnnotation(map, Component.class, WebController.class);
    assertNotNull(synthesizedComponent);
    assertNotSame(component, synthesizedComponent);
    assertEquals("value from component: ", "webController", component.value());
    assertEquals("value from synthesized component: ", "webController", synthesizedComponent.value());
}
Also used : Component(org.springframework.stereotype.Component) Test(org.junit.Test)

Aggregations

Component (org.springframework.stereotype.Component)18 Test (org.junit.Test)14 UntypedAnnotationDescriptor (org.springframework.test.util.MetaAnnotationUtils.UntypedAnnotationDescriptor)3 Method (java.lang.reflect.Method)1 Binding (org.glassfish.jersey.internal.inject.Binding)1 Order (org.springframework.core.annotation.Order)1 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)1 Service (org.springframework.stereotype.Service)1 Transactional (org.springframework.transaction.annotation.Transactional)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1