Announcement Announcement Module
Collapse
No announcement yet.
Autowiring Generic components Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring Generic components

    Assume I have a base class similar to the one below
    http://pastebin.com/m556a89ea

    Code:
    public abstract class AbstractEntityCrudAction<T> {
    
    	/*
    	 * This is just an example class.
    	 * _service needs to injected for each subclass. Depending on the generic type
    	 * eg. for class UserCrudAction extends AbstractEntityCrudAction<User>
    	 * it should inject UserService implements GenericService<User>
    	 * 
    	 */
    	/*
    
            // I want to autowire this
    	private GenericService<T> _service;
    	
    	public void setService(GenericService<T> service) {
    		_service = service;
    	}
    	
    	public void save(T entity) {		
    		_service.save(entity);
    	}
    	
    	public void delete(T entity) {
    		_service.delete(Entity);
    	}
    }
    Subclasses override this class and provide specific implementations.

    Say

    Code:
    public class UserCrudAction extends AbstractEntityCrudAction<User> {
    
    @override
    public void save(User entity) {
    //do some thing
    }
    
    }
    I have class UserGenericService extends GenericService<User>
    The service is defined in Application context with other services like ProfileService etc.. All extends GenericService<T>

    How can I have spring correctly autowire _service for every sublass based on the generic type !

    I.E for UserCrudAction, generic type is User, so it should call setService() With UserGenericService

    Thanks

  • #2
    Any idea !!

    Comment

    Working...
    X