Frühjahr Autowiring eine Eigenschaft innerhalb einer autowired bean

Ich bin ein Neuling auf den Frühling. Ich bin mit einem problem konfrontiert, mit Spring-Boot. Ich bin versucht zu autowire ein Feld aus einer externen config-Datei in ein autowired bean. Ich habe die folgenden Klassen

App.java

public class App {

@Autowired
private Service service;

public static void main(String[] args) {

    final SpringApplication app = new SpringApplication(App.class);
    //app.setShowBanner(false);
    app.run();
}

@PostConstruct
public void foo() {
    System.out.println("Instantiated service name = " + service.serviceName);
}
}

AppConfig.java

@Configuration
@ConfigurationProperties
public class AppConfig {

@Bean
public Service service()    {
    return new Service1();
}
}

Service-Schnittstelle

public interface Service {
    public String serviceName ="";
    public void getHistory(int days , Location location );
    public void getForecast(int days , Location location );
}

Service1

@Configurable
@ConfigurationProperties
public class Service1 implements Service {

@Autowired
@Value("${serviceName}") 
public String serviceName;
//Available in external configuration file.
//This autowiring is not reflected in the main method of the application.



public void getHistory(int days , Location location)
{
    //history code
}

public void getForecast(int days , Location location )
{
    //forecast code
}
}

Ich bin nicht in der Lage, um die Anzeige der service-name-variable in der postconstruct-Methode der App-Klasse. Tue ich das richtige?

InformationsquelleAutor anset | 2015-05-06
Schreibe einen Kommentar