Reflection & Spring
От: Аноним  
Дата: 13.06.11 09:49
Оценка:
Подскажите как сделать, чтобы зарабобтало..

Вообщем есть контроллер в него приходят два параметра, один отвечает за то какую стратегию подгрузить. Через рефлешн подгружаю нужную стратегию, но CustomerDao остаеться не проинициализированным и как следствие NPE.

Мне необходим доступ к базе в каждой реализации стратегии, наборы данных будут различны, посему доступ к базе хочеться вынести в стратегию.

///////////////////////////////////////////////////////////////////////////
@Controller
@RequestMapping("/json/{customerId}")
public class TestJsonController {

    @Autowired
    private ReportService reportService;

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody
    Map<String, String> view(@PathVariable Integer customerId, @RequestParam("reportId") Integer reportId)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {

        Report report = reportService.getReference(reportId);

        Class clazz = Thread.currentThread().getContextClassLoader().loadClass(report.getKlass());
        IStrategy strategy = (IStrategy)clazz.newInstance();

        return strategy.execute(customerId);
    }

}

///////////////////////////////////////////////////////////////////////////

@Service
public class IncomeStrategy implements IStrategy {
    
    @Autowired
    CustomerDao customerDao;

    @Transactional
    public Map<String, String> execute(Integer customerId) {
        Customer customer = customerDao.getReference(customerId);
        
        List<TurnoverSheet> turnoverSheets = customer.getTurnoverSheets();

        ...        
        //fill map

        ...
        return map;
    }
}



Спасибо.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.