Injecting Environment Variable into Groovy / Spring - spring

I have a groovy class that uses spring injection to get a hostname (previously defined in application.properties). It works fine, the code looks like this:
import org.springframework.beans.factory.annotation.Value
... ...
#Value('${mycontext.var1}')
private String serverHost
Now, instead of using application.properties, I'd like to inject from an environment variable named SERVER_HOSTNAME. I tried the following, but it does not work.
#Value('${System.getenv("SERVER_HOSTNAME")}')
private String serverHost

It seems that the following piece of code should work:
#Value("#{environment.SERVER_HOSTNAME}")
private String serverHost

Related

How to autowire 2 symfony services, which use the same interface and addtional arguments

I have two services, which both use the same interface and one is injected in the other.
With this configuration in the service.yaml everything worked well:
# fix autowiring for 2 services using the same interface
App\Domain\ListService: ~
App\Domain\SapService\SapListService: ~
App\Domain\ListService $sapListService: '#App\Domain\SapService\SapListService'
App\Domain\ListServiceInterface: '#App\Domain\ListService'
following the official documentation found here.
Now one of my services needs the information in which environment the class is currently running.
In a simple service configuration I would write it like this:
App\Service\FooService:
arguments:
$env: '%env(APP_ENV)%'
But how do I add the environment information in my more complex situation?
I tried this:
App\Domain\ListService: ~
App\Domain\SapService\SapListService: ~
App\Domain\ListService $sapListService: '#App\Domain\SapService\SapListService'
arguments:
$env: '%env(APP_ENV)%'
App\Domain\ListServiceInterface: '#App\Domain\ListService'
which throws this error:
The file "/var/www/src/../config/services.yaml" does not contain valid YAML: Unable to parse at line 52 (near " arguments:").
What is the proper formatting to parse the environment information into my service?
I tried manual wiring like this:
public function __construct(
ListServiceInterface $sapListService,
#[Autowire('%env(APP_ENV)%')]
string $env
) {
$this->sapListService = $sapListService;
$this->env = $env;
}
which gives me the error:
In DefinitionErrorExceptionPass.php line 54:
Cannot autowire service "App\Domain\ListService": argument "$env" of method "__construct()" is type-hinted "string", you should configure its
value explicitly.
Looks like the Autowire annotation is only available with symfony 6
You should use the namespace in the redis config to separate the different environments and not use the APP_ENV var to create the keys to store.
I guess the best solution is to prefix the cache key with the environment via the cache configuration in the cache.yaml file.
The solution is described here

How to get Env specific yaml file inside java class in Mule 4

Hi I Have enviornment specific yaml files in my Mule Application . I need to read these file in my Java Class . My Yaml File name will be formed as
properties/${mule.env}/config-${mule.env}.yaml
So for each env it will load speicfic file. I need to get the respective env file in my java class . How i can do that . I tried like below in Java Class but its coming as null
#Value("${rixml.VersionID}")
private String version;
You can form the YAML filepath in your mule flow and then pass it as an argument to your java class.In your java class you can read the YAML file(from the file path passed to it), parse it and extract the value that you want.

Spring boot config client properties

//This is the value of the property which needs to be fetched via config server from git repo
ConfigClientController(#Value("**${foo:test}"**) String value) {
this.value = value;
}
The above code is a spring cloud config client(spring.application.name=foo) that gets the config properties from the git config repo.
Foo is the property in foo.properties. Here I am little confused about the :test param in the code above. I wanted to know what is the significance of this param in getting the property.
If foo parameter is not present meaning it cannot be obtained from the config test string will be used instead. Meaning - what's after the : is a default value for the #Value annotated param.

Could not resolve placeholder 'XXX' in string value "${XXX}"

In my spring boot example, I'd like to read environment variables from my local machine(Win 7).
#Data
#Component
public class EnvironmentVariableSystemProperties {
#Value("${java.home}")
private String javaHome;
#Value("${DATASTORE.DATASET}")
private String datastoreDataset;
#Value("${SPRINGA}")
private String springConfigName;
}
It works well when reading java.home and DATASTORE.DATASET which have been added in my OS environment variables before.
I just added a new variable SPRINGA. When running spring boot example, I got the error:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'SPRINGA' in string value "${SPRINGA}".
Should I restart up my machine to make the variable work?
I just restarted Eclipse, but it didn't work.
Restarting Eclipse with the Restart in the Menu doesn't work.
Exiting Eclipse and Running it again works.
How weird it is!

#PropertySource working with spel (systemEnvironment and systemProperties)

I have in src/main/resources the following files
bpp-dev.properties
bpp-prod.properties
bpp-test.properties
Through my STS I can define the key envB, it in two places
how a VM argument such as -DenvB=dev
how an Environment such as Variable envB and Value prod
If in a Configuration class I have the following.
#Configuration
#PropertySource("classpath:/com/manuel/jordan/properties/bpp-${envB}.properties")
public class PropertiesConfiguration {
It works fine but always has preference the System Properties over Environment Variables, it is the default behaviour. I have no problem here.
But if I want work explicitly with Environment Variables, the following fails
#Configuration
#PropertySource("classpath:/com/manuel/jordan/properties/bpp-#{systemEnvironment['envB']}.properties")
public class PropertiesConfiguration {
Always I receive:
Caused by: java.io.FileNotFoundException:
class path resource
[com/manuel/jordan/properties/bpp-#{systemEnvironment['envB']}.properties]
cannot be opened because it does not exist
How I can fix this?
If I use the functional #PropertySource and just playing in the same #Configuration class I work with the following:
#Value("#{systemProperties['envB']}")
private String propertiesEnvB;
#Value("#{systemEnvironment['envB']}")
private String environmentEnvB;
to be printed later, both works fine.

Resources