I have 2 applications, one using Spring inside a web-app, and another local application using Spring Boot. These 2 share a configuration class between them.
I cannot figure out how to configure the local application correctly.
Here is the basic layout of classes I am using:
The main class
@EnableAutoConfiguration
class MainClass{
@Autowired
private static MyComponent component;
public static void main(String args[]){
// code
SpringApplication.run(MyConfiguration.class, args);
component.start();
}
}
The configuration
@Configuration
@EnableConfigurationProperties
@PropertySource("classpath:/path/to/queue.properties")
public class MyConfiguration {
@Autowired
public static Environment env;
// more beans
@Bean
@Qualifier("qualifier1")
public static String getName(){ //made String and simple to match the Component's Autowired
return env.getProperty("property.name");
}
}
The component
@Component
@EnableAutoConfiguration
public class MyComponent extends Thread {
@Autowired
@Qualifier("qualifier1")
private String template; // this isn't actually String, but a springAMQP class. Should have the same effect though.
@Override
public void run(){
//code
template.charAt(0); // just something that fails if it was not autowired
//code
}
}
If .run is given MyConfiguration.class
i get a null pointer within the autowired Environment in MyConfiguration
. If it is given MainClass.class
the autowired MyComponent
is still null.
As for some layout restrictions,
the Main Class and MyComponent only exist in the local application. The Configuration is in a shared package between the local application and the web application. This prevents me from simply creating the Bean with MyComponent in the Configuration due to dependencies.
If I remove the MyComponent from the MainClass and add the following configuration within the Local application:
@Configuration
public class MyLocalConfiguration extends MyConfiguration {
private MyComponent listener;
@Bean
public MyComponent getListener(){
if(listener == null){
listener = new MyComponent();
listener.start();
}
return listener;
}
}
I still have the issue of the Environment being null in MyConfiguration, preventing the other beans from being set up.