Hi, my idea is simple, I want to @inject a component instance and "decorate" it with custom qualifier:
The @Model annotation is like this:Code:@Service @Transactional 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.. }
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:Code:@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.CONSTRUCTOR}) @Documented @javax.inject.Qualifier public @interface Model { public Class<?> value(); }
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?Code:public class ModelAnnotationProccessor extends CustomAutowireConfigurer { private Logger logger = LoggerFactory.getLogger(ModelAnnotationProccessor.class); public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { try { this.proccessModelAnnotation(beanFactory); } catch (Exception e) { e.printStackTrace(); } super.postProcessBeanFactory(beanFactory); } 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(); logger.info(">>>>> 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 }; logger.info(">>>>> Constructor: {}, parameter: {}, and descriptor: {}", log); beanFactory.resolveDependency(descriptor, field.getName()); } } } } }
Thanks,
xsalefter


Reply With Quote
