Results 1 to 6 of 6

Thread: i need application context from my bean

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    You need to implement ApplicationContextAware.
    Hope this helps.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3

    Default

    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...

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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?
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  6. #6

    Default

    ic..

    thanks a lot for the help...

    more power...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •