-1

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.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
mangusbrother
  • 3,988
  • 11
  • 51
  • 103
  • It will always be null. Your `MainClass` isn't a spring managed bean and as such will not have `@Autowired` processed. Just retrieve the bean from the context. Also why aren't you just using springs scheduling/executing support to start threads instead of hacking together your own. I would remove the `@Compoment` and add a `@Bean` declaration for the `MyComponent` class and add a `init-method` to the `@Bean` method which would call the `start` method for you. Saves you code and is easier. – M. Deinum Aug 13 '14 at 06:46
  • just added some layout restrictions to the question. I can't make beans with MyComponent in the Configuration as i'd have to add a dependency across projects. – mangusbrother Aug 13 '14 at 06:53
  • updated question to add LocalConfiguration in Local Application. Still having issues with Environment however – mangusbrother Aug 13 '14 at 07:07
  • As I stated in my initial comment > It will always be null. Your `MainClass` isn't a spring managed bean and as such will not have @Autowired processed. As I also stated you really shouldn't be starting your own threads this should be left to executors and delegated to a threadpool (especially when running in a JEE container). – M. Deinum Aug 13 '14 at 08:21
  • the `@Autowired` component isn't in the `MainClass` any more after the last change however. The `@Autowired Environment` is located in the configuration class which is Spring Managed. – mangusbrother Aug 13 '14 at 08:40
  • 1
    Yuo cannot inject `static` fields. – M. Deinum Aug 13 '14 at 08:43
  • removed static and it worked (Y). Post it as an answer and i'll expect it @M.Deinum – mangusbrother Aug 13 '14 at 08:54

1 Answers1

1

There are 2 problems with your configuration

  1. @Autowired will only work for Spring Managed beans, your MainClass isn't spring managed so no autowiring is going to happen
  2. @Autowired will not work on static fields
M. Deinum
  • 115,695
  • 22
  • 220
  • 224