Announcement Announcement Module
Collapse
No announcement yet.
How to access bean loaded by Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to access bean loaded by Spring

    Hi,

    I am newbee to spring. I have a Manager bean loaded by Spring that I would like to access by a class/servlet/Action that has not been loaded by spring.

    Can some one show me an example of how to do it?
    What is the proper way of accessing class loaded by Spring from struts/tile based application?


    Thanks,
    Jiggy.

  • #2
    Originally posted by jmalkan View Post
    Hi,
    I am newbee to spring. I have a Manager bean loaded by Spring that I would like to access by a class/servlet/Action that has not been loaded by spring.
    You can pass a ServletContext reference to one of the static members of WebApplicationContextUtils and use a getBean from there on...

    Comment


    • #3
      WebApplicationContextUtils will work fine. There are other ways of integrating it with view technologies to make this easier. I found this article useful.
      http://www-128.ibm.com/developerwork...ary/j-sr2.html
      http://www.springframework.org/docs/...textUtils.html

      Comment


      • #4
        Thanks Guys. It was very helpful. I am able to read the bean. I am getting another problem. I think I am missing a some configuration. Hope you guys can help me out.

        I am building a small test app as a prototype to integrate spring into our struts/tiles based application.

        For now I am keeping this very small and simple. My goal is to create an sample to demonstrate method interception with spring.

        Here is what I got so far.
        2 JSPs. One with 3 buttons. On cllick, it goes to the 2nd JSP that calls Manager classes method.

        I have following entries in

        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
        
        <!--
          - Application context definition for PetClinic on JDBC.
        -->
        <beans>
          <!-- ========================= RESOURCE DEFINITIONS ========================= -->
          <!-- JNDI DataSource for J2EE environments -->
          <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/DRM Pool Reference" />
          </bean>
        
          <!-- Transaction manager for a single JDBC DataSource (alternative to JTA) -->
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
          </bean>
        
          <!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
        
          <!-- CaseManager JDBC implementation (only initialized when referenced) -->
          <bean id="caseMapper" class="com.axeda.drm.cases.CaseMapper" lazy-init="true" />
        
          <!-- CaseManager (only initialized when referenced) -->
          <bean id="caseManager" class="com.axeda.drm.cases.CaseManager" lazy-init="true">
            <property name="mapper" ref="caseMapper" />
            <property name="dataSource" ref="dataSource" />
          </bean>
        
          <!-- AutoProxies -->
          <bean name="beanAutoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="beanNames">
              <value>caseManager</value>
            </property>
            <property name="interceptorNames">
              <list>
                <value>securityMonitor</value>
              </list>
            </property>
          </bean>
        
        
          <!--
            - Transactional proxy for PetClinic's central data access facade.
            - Implements the CachingClinic management interface.
            -
            - Note that in a real-life app with multiple transaction proxies,
            - you will probably want to use parent and child bean definitions
            - as described in the manual, to reduce duplication.
          -->
          <bean id="drmTxn" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager" ref="transactionManager" />
            <property name="target" ref="caseManager" />
            <property name="transactionAttributes">
              <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
              </props>
            </property>
            <!-- property name="preInterceptors" ref="securityMonitor"/-->
          </bean>
        
        
          <!-- Call Security interceptor that checks users permission -->
          <bean id="securityMonitor" class="com.axeda.common.interceptors.SecurityInterceptor" />
        
        </beans>
        I was expacting that System.out that I have in SecurityManager would print. Am I doing anything wrong?


        Thanks,
        Jiggy.
        Last edited by jmalkan; Feb 13th, 2007, 10:12 AM.

        Comment


        • #5
          I'm slightly confused. What was supposed to create the message? Is it possible to see the code you were running to try and cause the message to be seen. It would be good if you wrap code in [ code] [ /code] tags as well, sooo much easier to read.

          Comment


          • #6
            I appoligize for the confusion. I wasn't aware of <code> tags.

            I was expacting that 2nd JSP that makes a call to CaseManager beans create method be Intercepted by SecurityInterceptor bean as defined in beanAutoProxy bean defination.

            I am getting following error:

            Code:
            <Feb 13, 2007 4:03:50 PM GMT> <Error> <HTTP> <BEA-101020> <[weblogic.servlet.internal.WebAppServletContext@18dea20 - appName: 'SecurityInterception', name: 'SecurityInterception', context-path: '/SecurityInterception'] Servlet failed with Exception
            java.lang.ClassCastException: $Proxy33
            	at jsp_servlet.__caseformaction._jspService(__caseformaction.java:112)
            	at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
            	at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:225)
            	at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:127)
            	at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)

            Thanks,
            Jiggy.
            Last edited by jmalkan; Feb 13th, 2007, 10:12 AM.

            Comment


            • #7
              This is due to proxying. If you aren't using interfaces you need to set proxyTargetClass="true" to forice CGLIB proxies.
              http://www.springframework.org/docs/...on-spring-defn
              http://www.springframework.org/docs/...Class(boolean)
              http://www.springframework.org/docs/...Class(boolean)

              Comment


              • #8
                I created Manager Interface which CaseManager object implements. However I still got the same error.

                Then I tried less desirable CGLIB JAR/config and it works fine.

                This is good for my test. I deally I would like not to use CGLIB.


                Thanks for the help.
                Jiggy.

                Comment


                • #9
                  Ok, good atleast you've got something working. As for the non-CGLIB option, it's not as simple as implementing an interface. If CaseManager implements Manager you need to ensure the class has a setter which accepts the interface and not the class. All the references need to be the interface for this to work. If you are still having problems, post the stacktrace and the applicationContext.

                  Comment

                  Working...
                  X