Call Link by line

In my code, I have an HPContentModule:

@Resource(name = "HPContentModule") private HPContentModule hpContentModule; 

I am wondering if it is possible to get a String such that:

 String myString = "hpContentModule"; 

so that I can then call hpContentModule with myString , doing something like myString.init(); where init() is a method in HPContentMethod ?

Or

If I have a bean:

 <beans:bean id="myBean" class="com.app.search.HPContent" /> 

Is it possible to call this bean on String in the controller?

+4
source share
1 answer

There are several ways to do this.

One of the easiest is Spring ApplicationContext . There are several ways you can access ApplicationContext . When you have a copy of ApplicationContext , you can call the getBean() method as follows:

 HPContentModule hpContentModule = (HPContentModule) appContext.getBean( "myBean" ); 
0
source

Source: https://habr.com/ru/post/1434290/


All Articles