在web.xml中定义了以下内容
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:abc/xml/spring/applicationContext.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>   
  <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>   
</listener>
如果将上面内容中的<context-param>...</context-param>这个去掉JBOSS5启动时会提示找不到applicationContext.xml,加上则没事,由此是不是可以说加上这段内容applicationContext.xml已被加载,如果已被加载可为什么在用getBean获取bean时却提示找不到这个bean定义了一个servlet,在这个servlet的init()中获取bean
public void init(){
    //getBean("bean名称");
    //提示找不到这个bean
}请各位高手帮忙解决一下!!

解决方案 »

  1.   

    你应该先得到一个spring的实例,在通过这个实例根据spring的配置的bean的id才能得到这个bean
      

  2.   

    <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>这句是加载spring的配置文件
      

  3.   

    applicationContext.xml就是配置spring的,里面包含dao   service   db的配置信息
      

  4.   

    好像是要通过bean的id才能找到的,你看你的这个bean在配置文件里配的是id还是name
      

  5.   

    getBean("bean名称"); 
    bean名称要取<bean id="beanID" />中的id名称,及beanID
      

  6.   

    servlet中这么写
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    ctx.getBean("beanID");
      

  7.   

    按各位的方法都试了,可就是不对,总是说找不到BEAN
    怎么办啊?
    我用的是JBOSS5 spring3 hibernate3    不知道是不是和这些有关系是不是JBOSS5对applicationContext位置有要求,必须要放在WEB-INF下
      

  8.   

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:abc/xml/spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>   
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>这是在web.xml中的定义,如果将这些去掉,在启动JBOSS时会提示必须要添加
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    如果将<context-param>...</context-param>去掉则提示找不到applicationContext.xml
    都试过了,一直提示找不到bean,真怪了!!!!
      

  9.   

    现在将<param-value>classpath:abc/xml/spring/applicationContext-*.xml </param-value>这句中的classpath改成/WEB-INF/classes/..后也现这个错误Could not resolve placeholder 'org.hibernate.cache.EhCacheProvider'
    各位帮忙看下,用的是JBOSS5顺便说下,<param-value>classpath:abc/xml/spring/applicationContext-*.xml </param-value>这个配置在JBOSS4下直接就出现Could not resolve placeholder 'org.hibernate.cache.EhCacheProvider'这个错误
      

  10.   

    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    applicationContext.xml文件一般放在WEB-INF目录下
      

  11.   

    如果还不行把
    <listener> 
      <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> 
    </listener> 
    换成
    <servlet-class>
    org.springframework.web.context.ContextLoaderServlet
    </servlet-class>我以前出现这个问题,改了这个就好了
      

  12.   

    谢谢各位,问题解决了,通过这个问题得到以下结论
    在Jboss 5 CR2下写成classpath:abc/cde/applicationContext.xml虽然不报错,但根据BEAN名得不到BEAN,不知道是不是不支持这种写法还是需要在代码中动态中加载XML文件关于Could not resolve placeholder 'org.hibernate.cache.EhCacheProvider'这个错误是在配置缓存时一个类名写错了谢谢各位
      

  13.   


    JBOSS5,不支持通配符*,
    写法要改成:    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    <!-- JBOSS5不支持通配符*.XML的写法,需要写成下面那样  rocklee@20110610 -->
    classpath*:/spring/applicationContext.xml
    classpath*:/spring/applicationContext-aop.xml
    classpath*:/spring/applicationContext-dao.xml
    classpath*:/spring/applicationContext-service.xml
    </param-value>
    </context-param>上面的写法试过可行,折腾我几天了!