Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8047811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:13:20+00:00 2026-06-05T06:13:20+00:00

I’m using an API that exposes services in the form of XXXLocalServiceUtil classes which

  • 0

I’m using an API that exposes services in the form of XXXLocalServiceUtil classes which are static wrappers of singleton objects. Instead of using the static XXXLocalServiceUtil methods I want to inject the XXXLocalService objects themselves to use them directly in my code, e.g.:

@Named
public class MyMailingService {        
    @Inject UserLocalService userService;

    public String mailUser(String email) {
       User user = userService.getUser(email);
       emailUser(user);
    }
}

And configure my applicationContext.xml like so:

<beans ...>
    <bean class="x.y.z.UserLocalServiceUtil" factory-method="getService"/>
    <bean class="x.y.z.CompanyLocalServiceUtil" factory-method="getService"/>
    ...
</beans>

This works perfectly. Now, this API I’m talking about has about 100 of these XXXLocalServiceUtil classes, each with their own static getService method which returns the actual service. Instead of listing all those services in my applicationContext.xml I would like to let Spring do the magic of finding the right XXXLocalServiceUtil class for each XXXLocalService I inject. So what I need is some kind of dynamic bean factory that will do the work for me, on a lazy-loading basis of course.

Anybody knows how this can be achieved easily?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:13:22+00:00Added an answer on June 5, 2026 at 6:13 am

    You can use a GenericApplicationContext to dynamically load the beans to an applicationContext along with the rest of your spring beans declared in xml. Here is an example implemented using the Reflections library…

    private static final Pattern SERVICE_UTIL_PATTERN = Pattern.compile(".*LocalServiceUtil.*");
    
    public static void main(String[] args) {
        ConfigurationBuilder builder = new ConfigurationBuilder().addUrls(
                ClasspathHelper.forPackage("x.y.z"))
                .setScanners(new SubTypesScanner(false));
        Reflections reflections = new Reflections(builder);
        GenericApplicationContext applicationContext = new GenericApplicationContext();
        Set<Class<? extends Object>> classes = reflections.getSubTypesOf(Object.class);
    
        for (Class<? extends Object> serviceUtilClass : classes) {
            String className = serviceUtilClass.getName();
    
            if (SERVICE_UTIL_PATTERN.matcher(className).matches()) {
                GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
                beanDefinition.setBeanClassName(className);
                beanDefinition.setFactoryMethodName("getService");
                beanDefinition.setLazyInit(true);
    
                String beanName = StringUtils.uncapitalize(serviceClass.getSimpleName().replace("Util", ""));
                applicationContext.registerBeanDefinition(beanName, beanDefinition);
            }
        }
    
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(applicationContext);
        reader.loadBeanDefinitions("classpath:/applicationContext.xml");
        applicationContext.refresh();
    }
    

    Update: To use this in a web application, you can simply extend Spring’s XmlWebApplicationContext and override the initBeanDefinitionReader method as follows…

    private static final Pattern SERVICE_UTIL_PATTERN = Pattern.compile(".*LocalServiceUtil.*");
    
    @Override
    protected void initBeanDefinitionReader(
            XmlBeanDefinitionReader beanDefinitionReader) {
        ConfigurationBuilder builder = new ConfigurationBuilder().addUrls(
                ClasspathHelper.forPackage("x.y.z"))
                .setScanners(new SubTypesScanner(false));
        Reflections reflections = new Reflections(builder);
        Set<Class<? extends Object>> classes = reflections.getSubTypesOf(Object.class);
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
    
        for (Class<? extends Object> serviceClass : classes) {
            String className = serviceClass.getName();
    
            if (SERVICE_UTIL_PATTERN.matcher(className).matches()) {
                GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
                beanDefinition.setBeanClassName(className);
                beanDefinition.setFactoryMethodName("getService");
                beanDefinition.setLazyInit(true);
                String beanName = StringUtils.uncapitalize(serviceClass
                        .getSimpleName().replace("Util", ""));
                registry.registerBeanDefinition(beanName, beanDefinition);
            }
        }
    }
    

    }

    and add the following context-param to your web.xml…

    <context-param>
      <param-name>contextClass</param-name>
      <param-value>x.y.z.MyXmlWebApplicationContext</param-value>
    </context-param>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.