Announcement Announcement Module
Collapse
No announcement yet.
i need application context from my bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • i need application context from my bean

    i am creating a business class but i need the class to have access to application context, how can i inject it to my business class or what class should i extend?

    thanks

  • #2
    You need to implement ApplicationContextAware.
    Hope this helps.

    Comment


    • #3
      ok i tried that but why I am having

      org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'utilities' is defined:

      the bean utilities is defined in another xml, which I imported on the top xml.

      my billing-servlet.xml
      Code:
             <beans>
      	<import resource="xml/utility.xml"/>
              ....
      	<bean id="ratesMeterController" class="billing.mock.RatesMeterController">
      		<property name="methodNameResolver" ref="ratesMeterControllerResolver"/>
      		<property name="billing" ref="billingImpl"/>
      	</bean>
              ...
      my applicationContext-jdbc.xml
      Code:
              ...
       	<bean id="billingImpl" class="com.controller.BillingImpl" lazy-init="true">
      		<property name="dataSource" ref="dataSource"/>
      	</bean>
             ....
      my billingImpl class looks like this
      Code:
      public class BillingImpl implements Billing,ApplicationContextAware {
      
      	private ApplicationContext applicationContext;
      	
      	public void setApplicationContext(ApplicationContext applicationContext){
      		this.applicationContext=applicationContext;
      	}
      	
      	public ApplicationContext getApplicationContext(){
      		return applicationContext;
      	}


      if i get that bean from my controller it's ok, but if i get it from my business class that implemented ApplicationContextAware, it says no bean defined.

      what gives?

      any ideas?

      thanks again...

      Comment


      • #4
        if i get that bean from my controller it's ok, but if i get it from my business class that implemented ApplicationContextAware, it says no bean defined.
        Could you post the surce code that you use to get "that" bean?

        Comment


        • #5
          I guess you are using ApplicationContextListener to load applicationContext-jdbc.xml and DispatchServlet to load billing-servlet.xml, right?
          Spring, actually, will create two ApplicationContexts, a root ApplicationContext loaded by the listener and a child one loaded by the DisplacthServlet. Beans in the root AC are visible to beans in the child AC. the inverse is not correct. If my original assumption is true, you can access "that" bean from the controller because it is defined in the xml/utility.xml file (included in billing-servlet.xml).
          To solve this issue you may remove the <import resource ...> from billing-servlet.xml and put it in applicationContext-jdbc.xml (this depends on what beans are declared in that file)
          Hope this helps

          Comment


          • #6
            ic..

            thanks a lot for the help...

            more power...

            Comment

            Working...
            X