Search in sources :

Example 1 with Repository

use of org.springframework.stereotype.Repository in project ff4j by ff4j.

the class FeatureAdvisor method getExecutedBeanName.

/**
 * Find bean name related to current method invocation.
 * @param pMInvoc
 *      current method invocation
 * @return
 *      bean name related to this method
 */
protected String getExecutedBeanName(MethodInvocation mi) {
    Class<?> targetClass = getExecutedClass(mi);
    Component component = targetClass.getAnnotation(Component.class);
    if (component != null) {
        return component.value();
    }
    Service service = targetClass.getAnnotation(Service.class);
    if (service != null) {
        return service.value();
    }
    Repository repo = targetClass.getAnnotation(Repository.class);
    if (repo != null) {
        return repo.value();
    }
    // There is no annotation on the bean, still be declared in applicationContext.xml
    try {
        // Use BeanDefinition names to loop on each bean and fetch target if proxified
        for (String beanName : appCtx.getBeanDefinitionNames()) {
            Object bean = appCtx.getBean(beanName);
            if (AopUtils.isJdkDynamicProxy(bean)) {
                bean = ((Advised) bean).getTargetSource().getTarget();
            }
            if (bean != null && bean.getClass().isAssignableFrom(targetClass)) {
                return beanName;
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("ff4j-aop: Cannot read bheind proxy target", e);
    }
    throw new IllegalArgumentException("ff4j-aop: Feature bean must be annotated as a Service or a Component");
}
Also used : Repository(org.springframework.stereotype.Repository) Advised(org.springframework.aop.framework.Advised) Service(org.springframework.stereotype.Service) Component(org.springframework.stereotype.Component) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with Repository

use of org.springframework.stereotype.Repository in project tephra by heisedebaise.

the class ClassReloaderImpl method getBeanName.

private String getBeanName(Class<?> clazz) {
    Component component = clazz.getAnnotation(Component.class);
    if (component != null)
        return component.value();
    Repository repository = clazz.getAnnotation(Repository.class);
    if (repository != null)
        return repository.value();
    Service service = clazz.getAnnotation(Service.class);
    if (service != null)
        return service.value();
    Controller controller = clazz.getAnnotation(Controller.class);
    if (controller != null)
        return controller.value();
    return null;
}
Also used : Repository(org.springframework.stereotype.Repository) Service(org.springframework.stereotype.Service) Component(org.springframework.stereotype.Component) Controller(org.springframework.stereotype.Controller)

Aggregations

Component (org.springframework.stereotype.Component)2 Repository (org.springframework.stereotype.Repository)2 Service (org.springframework.stereotype.Service)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Advised (org.springframework.aop.framework.Advised)1 Controller (org.springframework.stereotype.Controller)1