如题所述,得不到bean
具体的操作如下,我的前端是用DWR的
在tomcat服务器启动的时候自动加载spring配置文件
web.xml的配置如下:
<!-- 载入Spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext_hibernate.xml</param-value> <!-- 自动载入的spring配置 --> </context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
spring配置文件中有要获取的这个ID的bean
我在action中要获取applicationContex实例来获取spring配置文件中的bean,写法如下:ServletContext servletContext = WebContextFactory.get().getSession().getServletContext();
ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext); iaduserInfoService=(IaduserInfoService)ctx.getBean("aduserInfoService");
运行的时候问题出来了,页面提示“No bean named 'aduserInfoService' is defined”即aduserInfoService这个bean没有被找到。。可明明spring配置文件也被加载了,然后里面也有个 aduserInfoService为ID的bean,为什么加载不了呢?
我自己的初步预料可能会是因为ServletContext servletContext = WebContextFactory.get().getSession().getServletContext();
ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);ctx对象没有被获取到,因为我前端程序是直接用DWR来调用服务器端的action的,action不在spring容器的管理范围内。不知道是不是这样的,各位大虾有遇到这个问题的帮我解答下了。。谢谢
具体的操作如下,我的前端是用DWR的
在tomcat服务器启动的时候自动加载spring配置文件
web.xml的配置如下:
<!-- 载入Spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext_hibernate.xml</param-value> <!-- 自动载入的spring配置 --> </context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
spring配置文件中有要获取的这个ID的bean
我在action中要获取applicationContex实例来获取spring配置文件中的bean,写法如下:ServletContext servletContext = WebContextFactory.get().getSession().getServletContext();
ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext); iaduserInfoService=(IaduserInfoService)ctx.getBean("aduserInfoService");
运行的时候问题出来了,页面提示“No bean named 'aduserInfoService' is defined”即aduserInfoService这个bean没有被找到。。可明明spring配置文件也被加载了,然后里面也有个 aduserInfoService为ID的bean,为什么加载不了呢?
我自己的初步预料可能会是因为ServletContext servletContext = WebContextFactory.get().getSession().getServletContext();
ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);ctx对象没有被获取到,因为我前端程序是直接用DWR来调用服务器端的action的,action不在spring容器的管理范围内。不知道是不是这样的,各位大虾有遇到这个问题的帮我解答下了。。谢谢
如果在类路径下,classpath*:/applicationContext_hibernate.xml中的“/”不要加
但用服务器自动加载的方式。。加载的过程没有出错。。就是getBean的时候页面显示取不到这个bean所以应该不是spring配置文件路径的问题
我同时用手动加载aplicationContext的方式测试了下。。这时候都可以取得到"aduserInfoService"这个bean。。
还有我现在的applicationContext_hibernate.xml是放在src目录下的一个包里的。。我重新测了下把它改到WEB-INFO/目录下。。即如下的代码:
<context-param> <param-name>contextConfigLocation </param-name> <param-value>/WEB-INFO/applicationContext_hibernate.xml </param-value> <!-- 自动载入的spring配置 --> </context-param>
tomcat服务器启动的时候却报出了无法获取actionFactory这个bean的错。。这又是为什么呢???
这个问题搞的我很头大啊。。有遇到类似问题的大虾们帮忙解答下哦。。小女子万分感激!!!!!
另外建议你把文件都贴出来,还有错误信息,不然怎么看呢
UserBean ub = (UserBean)ctx.getBean("userBean");
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext_hibernate.xml</param-value> <!-- 自动载入的spring配置 --> </context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
classpath*:/applicationContext_hibernate.xml如果是把spring配置文件放在src目录下的某个包中,得把包名全写出来才行,直接这样配置是读不到的。。
还有第二个问题如下描述:还有我现在的applicationContext_hibernate.xml是放在src目录下的一个包里的。。我重新测了下把它改到WEB-INFO/目录下。。即如下的代码:
<context-param> <param-name>contextConfigLocation </param-name> <param-value>/WEB-INFO/applicationContext_hibernate.xml </param-value> <!-- 自动载入的spring配置 --> </context-param>
tomcat服务器启动的时候却报出了无法获取actionFactory这个bean的错。。这又是为什么呢???
这个问题是因为我自己的疏忽引起的,原因是我的项目中已经有引入了spring 的包,现在又引入srping2.5的包,由此照成了冲突,所以会报出了无法获取sessionFactory的错。。还有一个项目里只能进行一次sessionFactory的创建才行,原本我项目是只用hibernate的,现在引入了spring管理,原来hibernate在服务器启动时自动创建sessionFactory,现在spring在服务器启动时又再次创建sessionFactory,所以也会引起冲突