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 3979582
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:11:16+00:00 2026-05-20T05:11:16+00:00

I have a static method I have no control over: MyUtils.getServiceURL() which returns a

  • 0

I have a static method I have no control over: MyUtils.getServiceURL() which returns a URL. I need to extract just the port from this URL to set a property on a bean:

<bean id="myBean>
   <property name="defaultPort" value="?????"/>
</bean>

I can get the string value of the URL as a bean like this:

<bean id="serviceURL" class="MyUtils" factory-method="getServiceURL" />

I’m probably just going to write a method to parse the URL in myBean and extract the port. I was wondering if there was a way to avoid having to do this, and configure this directly in the spring context?

  • 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-05-20T05:11:16+00:00Added an answer on May 20, 2026 at 5:11 am

    No need for custom classes, or parsers. Remember, a bean is just a class with get/is/set methods. A java.net.URL class qualifies as such.

    Solution: Use a java.net.URL as a Spring-configurable bean and call it’s getPort method.

    Remember, in Spring, you can wire anything as a bean very easily so long as it sports methods using the get/is/set Bean convention. The java.net.URL class does such a thing.

    ** warning ** I’m just typing this out of my a$$, you’ll have to check for any syntax shenanigans that I might be introducing in this Spring-config pseudo-code. One thing that is certain is that the concept will work with Spring 2.5 and that it relies on the util schema.

    If you are using an older version of Spring, you’ll have to use a PropertyPathFactoryBean. If you are using Spring 3.0, you’ll want to use Spring expression language (EL), but I can’t comment on the later since I’m not familiar with Spring 3.0.

    In java:

    int port = (new URL(MyUtils.getServiceURL()).getPort()
    

    In Spring:

    <bean id="serviceURL" class="MyUtils" factory-method="getServiceURL" />
    
    <bean id="myURL" class="java.net.URL">
      <constructor-arg>
        <ref-bean="serviceURL"/>
      </constructor-arg>
    </bean>
    
    <util:property-path id="defaultPort" path="myURL.port"/>
    
    <bean id="myBean>
        <property name="defaultPort" ref="defaultPort"/>
    </bean>
    

    There might be a way to consolidate all these four expressions into three or less, don’t know if that will improve readability, though. The concept remains the same, though. Treat a java.net.URL instance as a bean, set its URL via its constructor, and get a hold (via Spring) of its getPort() method.


    ** edit **:
    If you are using Spring 2.5 you can create an inline groovy bean that does all that for you. I’ve seen people doing that as integration glue to get rid of multitude of temp Spring bean place holders. That’s the best choice IMO when you need to define a class (when it’s simpler than just using Spring tags) and when you know such a class won’t be used outside of Spring wiring of things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have static method which returns me as it's name says data from domain
I have a static method that takes a parameter and returns a class. the
I have a static method which sets a variable: static String[] playersNames; public static
I have created a static method to display a popup on my website from
I have written an extension method which finds a control depending on the Type
Why can we have static final members but cant have static method in an
We have a static method in a utility class that will download a file
I have a static method in my code that I would like somehow to
When we have a static method in a class it access only static members
So I basically want to have a static method in my AR class to

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.