在项目中使用Spring MVC,分别用到了@Controller @Service @Repository,例如:
@Controller @RequestMapping("/demo") public class DemoController { @Autowired private DemoService demoService; @RequestMapping(value = "/greet") public String greetWorld( @RequestParam(value = "name", required = false, defaultValue = "World") String name, Model model) { String showCase = demoService.demoShow(); model.addAttribute("msg", "Greet " + name + " " + showCase); return "demo/hello"; }
@Service public class DemoServiceImpl implements DemoService { @Autowired private DemoManager demoManager; public String demoShow() { return demoManager.showCase(); } }
@Repository public class DemoManagerImpl implements DemoManager { @Autowired private DemoDao demoDao; public String showCase() { return demoDao.showCase(); } }
然后我想在Servlet上下文初始化的过程中获取所有的@Service,代码如下:
public class DemoListener implements ServletContextListener { public void contextInitialized(ServletContextEvent servletContextEvent) { try { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.scan(new String[] { "demo.service","demo.manager","demo.dao" }); ctx.refresh(); Map<String, Object> services = ctx.getBeansWithAnnotation(Job.class); ...
当执行到ctx.refresh()的时候就出现了错误,一直没有找到问题,非常感谢!
没用过spring ,不过有更详细一点的错误信息么?
多谢天芒,已自己解决
@treerain: good!