1

I have loaded "myspring.xml" in web.xml using context-param

in "myspring.xml" I have written bean to which I have passed arguments as constructor argument

<bean id="abc" class="com.Hello">
     <constructor-arg ref="dataSource"/>
     <constructor-arg value= “dummy data”/>
</bean>

in Hello bean I have initialized constructor as ,

  public class Hello{
     public Hello(datasource,dummydata){
     }

     public void methodFromHelloBean(){
       // use here dummydata from constructor
     }
 }

Here , 'Hello' bean is getting initialized at server startup, as I defined in web.xml and it is working fine.

My question is -

I am working on exisitng applciation. I want to call methodFromHelloBean() inside my another class say MyService class. How I can call the method in MyService class.

One way i know is using applicationContext. But in my existing application I have not seen any bean loaded using application-context path.

what is other way , how I can initialize 'Hello' bean from 'MyService' class. Do I need to pass parameters to constructors while initializing & how.

Thanks in advance.

Arthur Noseda
  • 2,534
  • 19
  • 28
uday k
  • 11
  • 1
  • 2
  • How about injecting Hello bean in MyService? Is MyService defined as a Spring bean? – Arthur Noseda Jul 30 '16 at 12:43
  • yes, in exisitnng application beans has been auto wired. But If I think to use auto wire , public class MyService { @AutoWired Hello. Now here how I can call methodFromHelloBean and pass constructor arguments ? – uday k Jul 30 '16 at 12:47
  • You can't. Constructor args have to be given at design time in the Spring context. If you have to give parameters to your Hello bean, it has to be through the method you want to call. – Arthur Noseda Jul 30 '16 at 12:57
  • Thanks Arthur.So if I want to call methodFromHelloBean in MyService class , how i can call it, ? by creating object as public class MyService{ @Autowired Helllo hello; public void testMethod() { hello.methodFromHelloBean()} will it work ? i m doubtful since Hello has parameterized constructor which takes datasource and another argument.And methodFromHelloBean using parameter from constructor. – uday k Jul 30 '16 at 13:10
  • Let me write an answer so we can clearly see what's going on. – Arthur Noseda Jul 30 '16 at 13:16
  • https://stackoverflow.com/questions/9885203/initializing-spring-bean-from-static-method-from-another-class – Smart Coder Jan 22 '20 at 21:41

1 Answers1

2

Let's suppose we have MyService a class whose bean instance consumes some method methodFromHelloBean from abc, the Hello bean.

public class Hello {

    private boolean cacheInitialized;

    public void methodFromHelloBean(Object param) {
        if (!cacheInitialized) {
             initializeCache(param);
             cacheInitialized = true;
        }
        // do whatever you please with cache.
    }

    private void initializeCache(Object param) {
        // TODO
    }

}

public class MyService {

     @Autowired
     private Hello abc;

     public void someMethod() {
          // determine which parameters to pass to abc
          Object param = ...
          abc.methodFromHelloBean(param);
     }

}
Arthur Noseda
  • 2,534
  • 19
  • 28
  • Thanks Arthur. But one doubt still remain is, since Hello has constructor which takes parameter 'datasource' (DB thing) from "myspring.xml" which is available there. How I can pass that datasource parameter from MyService class . – uday k Jul 30 '16 at 13:25
  • Actually I want to implement cache like functionality, by loading "myspring.xml" at server startup which loads Hello class. Hello class populates data using datasource and other parameters in methodFromHelloBean method. And I want to call populated data method ie 'methodFromHelloBean' whenever required in my application say MyService class. So there is no need to pass datasource and other parameters and I can use already populated method with data without DB hit. – uday k Jul 30 '16 at 13:29
  • That can work. You can use some kind of lazy pattern here. I'll edit the answer to reflect this. – Arthur Noseda Jul 30 '16 at 13:32
  • Thanks. I think this may be helpful. I will try this. – uday k Jul 30 '16 at 13:46
  • I am editing your code , plz let me know if it can work. – uday k Jul 30 '16 at 14:11
  • I am hoping this line will work and will return data .Map returnedMap =abc.methodFromHelloBean(); It will not show any null data . Since I am not passing any arguments to initialize Hello constructor. – uday k Jul 30 '16 at 14:21