Announcement Announcement Module
Collapse
No announcement yet.
Filter and WebApplicationContextUtils Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Filter and WebApplicationContextUtils

    Hello, i have a filter:
    Code:
    ...............
        private WebApplicationContext wac;
    ...............
        public void init(FilterConfig filterConfig) throws ServletException {
    ...............
            wac = WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext());
    ...............
        }
    
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
                throws IOException, ServletException {
    ...............
                UserBean user = sessionMgr.getSessionUser(session);
                if (user != null) {
                    UserBeanDao userBeanDao = (UserBeanDao) wac.getBean("myUserBeanDao");
                    userBeanDao.reattach(user);
    ...............
                }
    ...............
        }
    ...............

    but when i use it:

    Code:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myUserBeanDao' is defined: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [myHibernate,myDatabase,dataSource,sessionFactory,transactionManager,localeResolver,messageSource]; Root of BeanFactory hierarchy
    	org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:242)
    	org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:498)
    	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:143)
    	org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:399)
    	com.jff.filters.RoleFilter.doFilter(RoleFilter.java:81)
    	com.jff.filters.AuthenticationFilter.doFilter(AuthenticationFilter.java:78)

    those beans are from applicationContext.xml, how can i get myUserBeanDao bean from application-servlet.xml ?

  • #2
    move

    Oh, it shoud probably be in web forum, can you move it?

    Comment


    • #3
      Filter and WebApplicationContextUtils

      Probably the simplest way would be to add a servlet context parameter in your web deployment descriptor

      From the Reference Guide:
      Code:
      <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
      </context-param>
      This will create a WebApplicationContext with beans from both the files.

      If your dao's xml file is in a jar, you could prepend the path to the file in the jar with 'classpath:'
      Code:
      <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath&#58;com/foo/dao/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
      </context-param>
      You could also have your filter create its own ApplicationContext with the WebApplicationContext as the parent.

      Comment


      • #4
        Re: Filter and WebApplicationContextUtils

        Originally posted by james.estes
        Probably the simplest way would be to add a servlet context parameter in your web deployment descriptor

        From the Reference Guide:
        Code:
        <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
        </context-param>
        This will create a WebApplicationContext with beans from both the files.

        You could also have your filter create its own ApplicationContext with the WebApplicationContext as the parent.
        Then I add a servlet context parameter
        Code:
        <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/applicationContext.xml</param-value>
         <param-value>/WEB-INF/jForumFusion-servlet.xml</param-value>
        </context-param>
        or
        <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/applicationContext.xml /WEB-INF/jForumFusion-servlet.xml</param-value>
        </context-param>
        I recive an exception that tells that sessionFactory can't be set (it looks like it only parses jForumFusion-servlet.xml.


        when i try to create ApplicationContext with the WebApplicationContext as the parent
        Code:
            protected ConfigurableApplicationContext createContext&#40;ServletContext sc&#41; &#123;
                WebApplicationContext root = WebApplicationContextUtils.getRequiredWebApplicationContext&#40;sc&#41;;
                ConfigurableWebApplicationContext wac = new XmlWebApplicationContext&#40;&#41;;
                wac.setParent&#40;root&#41;;
                wac.setServletContext&#40;sc&#41;;
                wac.setNamespace&#40;"jForumFusion-servlet"&#41;;
                wac.setConfigLocations&#40;new String&#91;&#93;&#123;"/WEB-INF/jForumFusion-servlet.xml"&#125;&#41;;
                System.out.println&#40;wac.getBeanDefinitionNames&#40;&#41;&#41;;
                wac.refresh&#40;&#41;;
                return wac;
            &#125;
        i get error:

        Code:
        java.lang.NullPointerException
                at org.springframework.context.support.AbstractApplicationContext.getBeanDefinitionNames&#40;AbstractApplicationContext.java&#58;428&#41;
                at com.jff.filters.RoleFilter.createContext&#40;RoleFilter.java&#58;121&#41;
                at com.jff.filters.RoleFilter.init&#40;RoleFilter.java&#58;69&#41;
                at org.apache.catalina.core.ApplicationFilterConfig.getFilter&#40;ApplicationFilterConfig.java&#58;225&#41;
                at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef&#40;ApplicationFilterConfig.java&#58;308&#41;
                at org.apache.catalina.core.ApplicationFilterConfig.<init>&#40;ApplicationFilterConfig.java&#58;79&#41;
                at org.apache.catalina.core.StandardContext.filterStart&#40;StandardContext.java&#58;3676&#41;
                at org.apache.catalina.core.StandardContext.start&#40;StandardContext.java&#58;4327&#41;
                at org.apache.catalina.core.StandardHostDeployer.start&#40;StandardHostDeployer.java&#58;830&#41;
                at org.apache.catalina.core.StandardHost.start&#40;StandardHost.java&#58;991&#41;
                at org.apache.catalina.manager.ManagerServlet.start&#40;ManagerServlet.java&#58;1322&#41;
                at org.apache.catalina.manager.HTMLManagerServlet.start&#40;HTMLManagerServlet.java&#58;529&#41;
                at org.apache.catalina.manager.HTMLManagerServlet.doGet&#40;HTMLManagerServlet.java&#58;104&#41;
                at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;697&#41;

        Comment


        • #5
          Code:
          <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>/WEB-INF/applicationContext.xml /WEB-INF/jForumFusion-servlet.xml</param-value>
          </context-param>
          Should work just fine. Make sure that where you are referencing your session factory, you are using <ref bean="sessionFactory"/> instead of <ref local="sessionFactory"/>.

          More generally, if a bean property in one xml file references a bean in another xml file, you need to use <ref bean="other"/> instead of <ref local="other"/>.

          As for creating your own ApplicationContext with the current WebApplictationContext as the parent, I was thinking something more like overriding the init method of your Filter to do this (off top of my head...not sure if this will compile...but you get the idea):
          Code:
          private ApplicationContext myContext;
          public void init&#40;FilterConfig config&#41;&#123;
             ServletContext servletContext = config.getServletContext&#40;&#41;;
             WebApplicationContext parentContext = WebApplicationContextUtils.getCurrentWebApplicationContext&#40;servletContext&#41;;
             String xmlPath = servletContext.getRealPath&#40;"/WEB-INF/jForumFusion-servlet.xml"&#41;;
             myContext = new FileSystemXmlApplicationContext&#40;new String&#91;&#93;&#123;xmlPath&#125;, parentContext&#41;;
          &#125;

          Comment


          • #6
            Originally posted by james.estes
            Code:
            <context-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/applicationContext.xml /WEB-INF/jForumFusion-servlet.xml</param-value>
            </context-param>
            Should work just fine. Make sure that where you are referencing your session factory, you are using <ref bean="sessionFactory"/> instead of <ref local="sessionFactory"/>.

            More generally, if a bean property in one xml file references a bean in another xml file, you need to use <ref bean="other"/> instead of <ref local="other"/>.

            I'm quite sure that i set everything correctly.

            jForumFusion-servlet.xml
            Code:
            <bean id="myUserBeanDao" class="com.jff.service.dao.hibernate.UserBeanDaoImpl">
                <property name="sessionFactory"><ref bean="sessionFactory"/></property>
                <property name="transactionManager"><ref bean="transactionManager"/></property>
            </bean>
            applicationContext.xml
            Code:
            <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
                <property name="configLocation"><value>classpath&#58;hibernate.cfg.xml</value></property>
                <property name="hibernateProperties"><ref local="myHibernate"/></property>
                <property name="dataSource"><ref local="dataSource"/></property>
            </bean>
            Full source here repository

            On the other hand, i really don't care how to do it but i'm stuck.
            So if anyone know how to get bean "myUserBeanDao" from filter,
            please give me the some working code ;-).

            Comment

            Working...
            X