Search code examples
javaspringdozer

Spring autowiring in dozer's destination class


So, I was banging my head against the monitor for the last 4 hours and can't figure it out. I am using Dozer for mapping and it works fine. However, I need one of my DAOs in the destination class and autowiring returns null. Here is a snippet of the class:

 @Component
 public class Address
 {
    @XmlElement(name = "street", required = true)
    protected String street;
    @XmlElement(name = "city", required = true)
    protected String city;
    @XmlElement(name = "zip", required = true)
    protected zip;


    @Autowired
    private CityDao cityDao;

    // Getters/setters 
  }

the cityDao is always null. I am fairly new to both Spring and Dozer, but the Dozer docs say that the destination classes are created with default constructors and as far as I understand the Spring should not have any problems with it. The cityDao is null though. Please help!


Solution

  • As has been mentioned in the comments, you should not be injecting a DAO into a DTO!

    However if you absolutely need to do that for some reason, check out Spring's @Configurable support. Here is the Javadoc and also some more information here and here.

    If setup correctly, it allows objects that are not explicitly managed by Spring, to benefit from features like auto-wiring dependencies.

    In you example code, Address although being annotated with @Component, is not managed by Spring since it's being created using Dozer. That's why you would need @Configurable to inject CityDao into Address