Reputation: 1133
I'm developing a Single Page Application in Angular that will connect with external services. However, I don't any warranties that path for those services will remain the same. My goal is to save those settings in an external settings file so I could update them on the fly without having to recompile and redeploy my app every time a path changes.
In Java using Spring framework using .properties
files it would be something like
public class RemoteService{
@Value("${removeservice.address}")
private String address;
// Class logic
}
How could I do the same thing in Java?
Upvotes: 4
Views: 9810
Reputation: 524
[OLD]
In angular you have "environment" files which let you store some datas like URLs of your APIs , see :
use like this example : in your src/environments/environment.dev.ts file you can expose a const like :
export const environment = {
apiUrl: 'http://my-api.domain.com'
}
which can be used like
import { environment } from '../environments/environment';
export class MyService{
private url = environment.apiUrl;
}
ps: you have to tell angular your environment files in the angular-cli.json
"apps": [
***
"environmentSource": "environments/environment.ts",
"environments":{
"dev": "environments/environment.dev.ts"
}
***
}
and if you really want to keep your Java syntaxe you can look for create javascript "decorator"
[EDITED]
I don't think there is a real solution, you can do it by hand, to avoid rebuild, create another config file (not in environments ), and load this config file datas into a service before the app to be initialized by using the "APP_INITIALIZER" token when you provide the service.
you can find an example to try here : https://stackblitz.com/edit/angular-config-at-runtime
Tell me if it helped you.
The solution came from a co-worker of mine Jonathan Antoine, not my code, you can find a blog post about it here (warning it's in french but you can see code ): https://blogs.infinitesquare.com/posts/web/angular-ajouter-une-configuration-chargee-au-runtime-post-build-plutot-que-pendant-la-build
Upvotes: 2