可能是个老生常谈的问题,无奈网上的解决办法完全不好使,无奈提问!
项目中需要在Servlet中调用Service,由于项目框架全面使用了Annotation,以前使用的从ApplicationContext中getBean的方法也不好使了(也不愿意使,否则又得在xml中定义一堆的bean)。几天来度娘、Google了无数solution,解决方式五花八门,但就是没有一个好使的!
1.
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
testService = (TestService)ctx.getBean("testService");
结果:传统的xml配置bean方式可以,Annotation方式失败,testService为空!2.
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
结果:testService为空!3.web.xml中TestServlet申明中servlet-class的值改为org.springframework.web.context.support.HttpRequestHandlerServlet,TestServlet
结果:tomcat启动报错找不到TestServlet项目结构非常简单,一共3个类TestServlet,TestService,TestController,
TestService类加了@Service注释
TestController和TestService中注释申明了TestServcei:
@Resource(换成@Autowire也一样没用)
private TestService testService;
spring-servlet.xml中配置了:
<mvc:annotation-driven />
<context:component-scan base-package="com.test" />TestController可以成功调用TestService,但TestServlet死活不行敢问哪位大哥实际在Servlet中用注解方式成功注入过bean的,网上五花八门的办法说的有鼻子有眼的,不像是忽悠呀,为毛到我这就不好使了???Servlet Spring 注入
项目中需要在Servlet中调用Service,由于项目框架全面使用了Annotation,以前使用的从ApplicationContext中getBean的方法也不好使了(也不愿意使,否则又得在xml中定义一堆的bean)。几天来度娘、Google了无数solution,解决方式五花八门,但就是没有一个好使的!
1.
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
testService = (TestService)ctx.getBean("testService");
结果:传统的xml配置bean方式可以,Annotation方式失败,testService为空!2.
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
结果:testService为空!3.web.xml中TestServlet申明中servlet-class的值改为org.springframework.web.context.support.HttpRequestHandlerServlet,TestServlet
结果:tomcat启动报错找不到TestServlet项目结构非常简单,一共3个类TestServlet,TestService,TestController,
TestService类加了@Service注释
TestController和TestService中注释申明了TestServcei:
@Resource(换成@Autowire也一样没用)
private TestService testService;
spring-servlet.xml中配置了:
<mvc:annotation-driven />
<context:component-scan base-package="com.test" />TestController可以成功调用TestService,但TestServlet死活不行敢问哪位大哥实际在Servlet中用注解方式成功注入过bean的,网上五花八门的办法说的有鼻子有眼的,不像是忽悠呀,为毛到我这就不好使了???Servlet Spring 注入
用action把servlet替了吧,要不看的都头疼
用action把servlet替了吧,要不看的都头疼
=================================
看我回复,我用servlet纯粹是拿它的init方法来加载一些数据,俺还不至于变态到用springmvc又用servlet来处理请求的地步
你自己遍历一下ctx中的bean吧~里面应该有sessionfactory!然后你自己new 一个dao,手动把sessionfactory放进去,这样你的dao就可以用了。目前我就是用这个方法初始化数据的,直接跳过service层吧!