1

I am building a UI into a JAR for Spring Server. I have a bunch of Angular JS pages. I want to pass in a command line argument to my jar that tells it where the API server is like so:

java -jar application.jar --api=http://ip:9000

So my application.properties file has:

url=${api:http://localhost:9000}

The way I am currently doing is it just having a hardocoded js config file and on each of my .html pages:

<script src="../js/appName/config.angular.js"></script>

Which contains:

var configData = {
  url:"http://localhost:9000"
};

And called in each file:

$scope.apiUrl  = configData.url;

How do I tap into the applications.properties file that I can override with my JAR command line parameter during runtime vs. the way it has been coded now.

4
  • 1
    Are you using maven? Then you can use profiles, they provide you parameter value selection for each environment (test, prod) you can check-out from this link maven.apache.org/guides/introduction/… Commented Nov 23, 2016 at 7:10
  • Because I have multiple different API servers depending on what type of data, access, etc, and the IP's rotate time to time so I need a way at the startup of my UI to pass in the API servers address and port. Commented Nov 24, 2016 at 17:28
  • Do you need to know how to override a value in the properties file, or do you need to know how to access it in your js files? Commented Nov 30, 2016 at 0:34
  • How to access it in the JS files. Commented Nov 30, 2016 at 21:39

2 Answers 2

1

When you pass a value from command line and the same property name is present in properties file then spring boot overrides the value from command line. So to achieve what you want do something like this

In application.properties

#this is default value
app.url=localhost:8080

Create a class to map the properties value or you can use existing class or something else based on your project structure.

@Component
public class Sample {

    @Value("${app.url}")
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }   
}

Now when your execute a jar with argument --app.url="someserver:9090" the value will be overriden and you can use this value anywhere.

Note it will also work if you try to access the properties value directly in jsp using expression.

Try it, it works. I have used the same thing in my latest project which is a composite microservices and each component need each others url.

[Edit] Reference : http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Sign up to request clarification or add additional context in comments.

1 Comment

will give this a shot.
1

Am I getting it right: The client part is delivered by the application? So the part of the last sentence 'during runtime' has more the meaning of 'bootstrap/initial loading', right? One (old school) approach is to provide the entry html (e.g. index.html) through the application (a simple template engine) and provide the needed information with a setter in a JS config object:

// pseudo js code with thymeleaf
<script th:inline="javascript">
/*<![CDATA[*/

  myConfig.url = [[${#httpServletRequest.remoteHost}]];

/*]]>*/
</script>

This is just a sample that will only set the remote host name but I think you get the idea.

Side note: I still don't really get why do you have to set this. If the application contains the client code, why do you work with absolute URLs for remote calls? (Disclaimer: I have only experience in Angular(2) and not with AngularJS)

2 Comments

I need to set this because this UI is going to be like a notebook. I can distribute it to users and they can connect to a few different API servers depending on the type of data they want to see/have access to. So instead of making a bunch of JAR's. Also the IP is not static, we have it rotating every so often.
I dont think your solution works for me because of the reason I mentioned above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.