Announcement Announcement Module
No announcement yet.
Custom Qualifier Annotation To Inject a Constructor Parameter Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom Qualifier Annotation To Inject a Constructor Parameter

    Hi, my idea is simple, I want to @inject a component instance and "decorate" it with custom qualifier:

    public class OrganizationServiceImpl implements OrganizationService {
      @Inject @Model(Organization.class)
      private JavaPersistenceDataAccess<Organization> organizationDAO;
      @Inject @Model(Department.class)
      private JavaPersistenceDataAccess<Department> departmentDAO;
      public List<Organization> findAllOrganization() {
        return this.organizationDAO.findAll();
      public List<Department> findDepartmentsByOrganizationName(String name) {
        Map<String, Object> organizationParam = new HashMap<String, Object();
        organizationParam.put("organizationName", name);
        Organization org = organizationDAO.getBy(organizationParam);
        Map<String, Object> departmentParam = new HashMap<String, Object>();
        departmentParam.put("organization", org);
        return departmentDAO.findBy(departmentParam);
      // Other business methods..
    The @Model annotation is like this:

    @Target({ElementType.FIELD, ElementType.CONSTRUCTOR})
    public @interface Model {
      public Class<?> value();
    Note that the JavaPersistenceDataAccess is a generic java interface, and I want to have only one implementation, which is DefaultJavaPersistenceDataAccess class. My question is, how to make this work? I try to create class that extends CustomAutowireConfigurer:

    public class ModelAnnotationProccessor extends CustomAutowireConfigurer {
      private Logger logger = LoggerFactory.getLogger(ModelAnnotationProccessor.class);
      public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        try {
        } catch (Exception e) { 
      private void proccessModelAnnotation(ConfigurableListableBeanFactory beanFactory) 
      throws SecurityException, NoSuchMethodException {
        Map<String, Object> daos = beanFactory.getBeansWithAnnotation(Service.class);
        for (String key : daos.keySet()) {
          Object service = daos.get(key);
          for (Field field : service.getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(Model.class)) {
              Class<?> model = field.getAnnotation(Model.class).value();
    ">>>>> Model Annotation Exist: {} from Bean: {}", model, service);
              Constructor<?> constructor = DefaultJavaPersistenceDataAccess.class.getDeclaredConstructor(Class.class);
              MethodParameter parameter = new MethodParameter(constructor, 0);
              DependencyDescriptor descriptor = new DependencyDescriptor(parameter, true);
              Object[] log = new Object[] {
                constructor, parameter, descriptor
    ">>>>> Constructor: {}, parameter: {}, and descriptor: {}", log);
              beanFactory.resolveDependency(descriptor, field.getName());
    But still could not resolve my problem. I'm stuck in how do I create an instance of component between the object is initialized by the (spring) container. Any suggestion for this? Is what I tried to do is possible by using spring?


  • #2
    [REPLY]: Custom Qualifier Annotation To Inject a Constructor Parameter

    I'm try to still googling about it more, and found this thread in stackoverflow. Is it true that I could not use this approach by using spring framework?