My application context XML is simple:
<context:component-scan base-package="com.depressio.spring" />
In that package, I have my configuration:
package com.depressio.spring
@Configuration
@ComponentScan(basePackages = "com.depressio")
public class DepressioConfiguration
{
@Inject private ApplicationContext context;
}
Within com.depressio
, there's a repository (DAO):
package com.depressio.dao;
@Repository
public class ParameterDAO
{
public Parameter getParameter(long ID) { ... }
}
... and a service where injection is working just fine (no NPE when parameterDAO is used):
package com.depressio.resource;
@Service
@Path("/depressio/parameters")
public class ParameterResource
{
@Inject private ParameterDAO parameterDAO;
@Path("{id}")
public Response getParameter(long parameterID)
{
return Response.ok(parameterDAO.getParameter(parameterID).legacyFormat()).build();
}
}
However, the legacyFormat()
method call there constructs another object. Within that object, I have to inject a different DAO (also annotated with @Repository
, though). That injection isn't working.
So, we have the original Parameter
object:
package com.depressio.domain;
public class Parameter
{
...
public LegacyParameter legacyFormat()
{
return new LegacyParameter(this);
}
}
... and the LegacyParameter
where the injection isn't working:
package com.depressio.domain.legacy;
public class LegacyParameter
{
@Inject private LegacyDAO legacyDAO;
....
public LegacyParameter(Parameter newParameter)
{
// NullPointerException when using the injected legacyDAO.
}
}
I've tried a few things, including:
Using an no-args constructor for
LegacyParameter
, then calling apopulate
method so I'm not using the injected DAO until after the object is constructed. This didn't work.Injecting the
LegacyDAO
into theParameterResource
and passing it in. This worked, but isn't ideal since I have to pass it around a whole lot (which injection should help avoid, no?). It did prove thatLegacyDAO
is injectible... just not intoLegacyParameter
apparently.Adding a
@Service
,@Component
, or@Named
annotation onLegacyParameter
. All end up with the NullPointerException on the line I try to reference the injectedlegacyDAO
.
What am I missing?