bkrish
bkrish

Reputation: 637

@Autowired fails in Spring

Folks,
I am trying to setup the project with Spring DI and MVC (ver 3.2.x ) . I try to inject service implementation but that fails at some point. No exceptions are thrown. Just the service object is null where it is expected. Below is the code.

package com.test.domain.web.controller;
.........
@Controller
@RequestMapping(value = "/system/**")
public class InformationController {
@RequestMapping(value = "/classing/{id}", method = RequestMethod.GET)
public ModelAndView getBaleClassing(@PathVariable String id, Model model, HttpServletRequest request) {
    LOG.info("In the system classing...");
    InformationModel informationModel = new InformationModel();
    model.addAttribute("informationModelVO",informationModel.getResult(id));
    return new ModelAndView(SYSTEM_CLASSING_TILE);
}

}


This is the model class for the business. I also tried to annotate @Autowired at setter method. But no luck.

package com.test.domain.classing.model;
.........
public class InformationModel  {

@Autowired
InformationService informationService;

public  InformationVO getResult(String id) {
    InformationVO informationVO = null;
    try {
        informationVO = informationService.getInformationById(id); //informationService is null here
    } catch (ServiceException e) {
        LOG.error("Exception: ", e);
    }
    return informationVO;
}

public InformationService getInformationService() {
    return informationService;
}

public void setInformationService(InformationService informationService) {
    this.informationService = informationService;
}
}


This is my service implementation, the only qualifying candidate. I depend on Spring to instantiate and I do NOT instantiate service or DAO implementation anywhere by myself.

package com.test.domain.classing.service;
.......
public class InformationServiceImpl implements InformationService {

    @Autowired
    private InformationDAO informationDAO;

    public InformationServiceImpl() {
        LOG.debug("In the InformationServiceImpl default constructor....");
    }

    public InformationVO getResult(String id) throws ServiceException {
        InformationVO informationVO = informationDAO.getResult(id);
        return informationVO;
    }

    public InformationDAO getInformationDAO() {
        return informationDAO;
    }

    public void setInformationDAO(InformationDAO informationDAO) {
        this.informationDAO = informationDAO;
    }
}

And, this is spring configuration (contains service and dao) . I do NOT add InformationModel in spring config as I need new instance every time.

applicationContext.xml


..........
<context:annotation-config />
<context:component-scan base-package="com.test.domain, com.test.domain.classing.model, com.test.domain.classing.service, com.test.domain.web.controller" />

<bean name="com.test.domain.classing.service.InformationService"  class="com.test.domain.classing.service.InformationServiceImpl">
</bean>

<bean name="com.test.domain.classing.dao.InformationDAO" class="com.test.domain.classing.dao.InformationDAOImpl">
</bean>
..........

mvcContext.xml


<context:component-scan base-package="gov.usda.fsa.pscao.clg.cops.web.controller" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />


From log, I could say implementations for DAO and service are instantiated. But none of the autowiring happens. i.e. injecting DAO into service or injecting service into model. Is there a way to debug while spring trying to inject (if it does, in my case) ?

Any pointers are greatly appreciated. Thanks in advance.

Upvotes: 0

Views: 3137

Answers (3)

RPaul
RPaul

Reputation: 946

Spring @Autowired injection will work only in spring managed components.Your InformationModel is not a spring managed component,you were trying to instantiate using new, so it failed.If you would like to go with annotation based configuration then You can simply annotate service using @Service and Daos using @Repository or @Component instead of bean definition in xml configuration .

Upvotes: 2

acurci
acurci

Reputation: 36

RPaul are right,

Try this:

@Controller
@RequestMapping(value = "/system/**")
public class InformationController {

@Autowired
InformationModel informationModel;

@RequestMapping(value = "/classing/{id}", method = RequestMethod.GET)
public ModelAndView getBaleClassing(@PathVariable String id, Model model, HttpServletRequest request) {
    LOG.info("In the system classing...");
    model.addAttribute("informationModelVO",informationModel.getResult(id));
    return new ModelAndView(SYSTEM_CLASSING_TILE);
}

}

and,

@Component
public class InformationModel  {
....
} 

and this

@Service
public class InformationServiceImpl implements InformationService {
...
}

Good luck!

Upvotes: 2

dimoniy
dimoniy

Reputation: 5995

No exceptions mean that there was no Autowire attempt made at all, which in turn means that class where you try to Autowire is not managed by Spring. Simple rule is that if a bean is not created by Spring, none of the Spring stuff (including autowiring) is going to work. If for some reason you need a new instance of InformationModel every time, then you better not rely on Spring for that and I'd just pass the required parameters to InformationModel's constructor when you create it.

Upvotes: 0

Related Questions