首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

关于SPRING注入的bean等于null的有关问题,高手帮忙了

2012-03-18 
关于SPRING注入的bean等于null的问题,高手帮忙了一个简单的SPRING配置,一个简单的SERVLET,配置好了SPRING

关于SPRING注入的bean等于null的问题,高手帮忙了
一个简单的SPRING配置,一个简单的SERVLET, 
配置好了SPRING的service等,然后给SERVLET注入一个bean,发现能注入(TOMCAT启动的时候发现能SET进去),但是调用的一直是NULL,不知道是什么原因,望高手指点.下面是相关代码。 

SERVLET代码 

Java code
public   class   Javaservice   extends   HttpServlet   { private   UserService   userservice; public   void   doGet(HttpServletRequest   request,   HttpServletResponse   response)                 throws   ServletException,                         IOException   {                 System.out.print( "f ");                 userservice.getaallStudent();(这一句在运行中发现是null)                 response.setContentType( "text/html ");                 PrintWriter   out   =   response.getWriter(); }         public   UserService   getUserservice()   {                 System.out.println( "get------------------------------------ ");                 return   userservice;         }         public   void   setUserservice(UserService   userservice)   {                 System.out.println( "set------------------------------------ ");(这一句在TOMCAT启动时发现正常,能注入值)                 this.userservice   =   userservice;         } 


SPRING_APPLICATION.XML 
Java code
<bean   id= "userservice "   parent= "txProxyTemplate "> <property   name= "target "> <ref   bean= "userserviceImpl "/> </property> <property   name= "proxyInterfaces "> <value> com.sos.service.UserService </value> </property> </bean> <bean   id= "userserviceImpl "   class= "com.sos.service.UserServiceImpl "> <property   name= "userdao "> <ref   bean= "userdao "/> </property> </bean> <bean   id= "userdao "   class= "com.sos.hibernate.UserDAO "> <property   name= "sessionFactory "> <ref   bean= "sessionFactory "/> </property> </bean> <bean   id= "Javaservice "   class= "com.sos.action.Javaservice "> <property   name= "userservice "> <ref   bean= "userservice "/> </property> </bean> 



就是这个问题,不知道为什么用的时候是NULL,同样的方法在SPRINGmvc中就不是NULL

[解决办法]
// HttpServletRequest request;
// ServletContext servletContext = request.getSession().getServletContext();
// ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
// UserService userservice =(UserService) ctx.getBean("userservice")


我们以前是用这个获取的

热点排行