0

I have a maven dependency jar that requires access to -DmyProperty=foo. When I start my application my main project can access the myProperty value. However the classes within the dependency jar (also a maven project) is unable to access these using System.getProperty() and returns null value. I was under the impression that every class on the class path had access to these resources. Is there a good way to get these values passed back to the dependency jar? If it makes a difference, both projects are built on spring.

The dependency jar uses:

import org.springframework.core.env.Environment;

@Autowired
private Environment environment;

public class myClass{
  ...
  public void myFunction() throws Throwable {
        System.out.println("System Env: " + environment.getProperty("myProperty"));
}

The main project imports the above project and creates a new instance of the above class and runs myFunction but blows up with a null pointer on environmnt.getProperty().

Any help is appreciated.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224

1 Answers1

0

You can use spring @Autowired annotation as below, but make sure you annotate your class with @Component or similar ones.

@Autowired
Environment env;

Also make sure your bean is visible for scanning, Component scanning should be enabled like this

<context:component-scan base-package="<YOUR_PACKAGE>"/>

or

@ComponentScan(basePackages = {"<YOUR_PACKAGE>"} )
Dinesh
  • 1,046
  • 1
  • 8
  • 17
  • Ya i have a @ComponentScan in the dependency jar and if i run the dependency jar alone it can access the -DmyProperty. but when i provide -DmyProperty to my service (which imports the dependency jar)... the dependency jar cannot read the -DmyProperty. – Billy Labrum Apr 15 '20 at 17:45