How to inject the DomainWrapperFactory in an API class ?

Hi guys,

I am trying to inject DomainWrapperFactory (to then retrieve a VisitDomainWrapper).

When I try to get it using annotations, domainWrapperFactory is null:

@Qualifier("domainWrapperFactory")
@Autowired
protected DomainWrapperFactory domainWrapperFactory;

Though using the Context to get the component works just fine:

domainWrapperFactory = Context.getRegisteredComponent("domainWrapperFactory", DomainWrapperFactory.class);

(Q) What should I add to make annotation based injection work in my class?


See the complete class:

public class BeforeEndVisitAdvice implements MethodBeforeAdvice{

	protected static final Log log = LogFactory.getLog(BeforeEndVisitAdvice.class);
	
	protected DomainWrapperFactory domainWrapperFactory;

	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {

		if (method.getName().equals("endVisit") && (target instanceof VisitService)) {
			
			// Using the Context to retrieve a component instead of @Autowired
			domainWrapperFactory = Context.getRegisteredComponent("domainWrapperFactory", DomainWrapperFactory.class);
			
			Visit visit = (Visit) args[0];

			VisitDomainWrapper visitDomainWrapper = domainWrapperFactory.newVisitDomainWrapper(visit);
			visitDomainWrapper.setVisit(visit);
			List<Diagnosis> diags = visitDomainWrapper.getUniqueDiagnoses(true, false);

			if (diags.isEmpty()) {
				log.error("Unable to save the visit. Visit (" + visit.getId() + ") must have at lease one primary diagnosis.");
				throw new APIException();	
			}
		}
		return;
	}
	
}

If the class from which you are auto wiring is not spring managed, then you need Context.getRegisteredComponent()

OK thanks @dkayiwa,

Does this mean that I should register my advice as a Spring bean (in the moduleApplicationContext.xml) in order to use @Autowired in this class? Do you have an example or a doc article to do this?

Do you think it is a good practice to not register my class as a bean and just retrieve the member from the Context