2017-06-02 2 views
3

Ich verwende springboot 1.3.8 und ich habe ein @Autowired auf einem Konstruktor mit Parametern aber ich habe den Fehler: Kein Standardkonstruktors gefunden ...Springboot Verwendung auf Konstruktor mit Parametern @Autowired

@SpringBootApplication 
public class App implements CommandLineRunner { 

    private ApplicationContext context; 
    private CLIHelper cliHelper; 

    @Autowired 
    public App(ApplicationContext context, CLIHelper cliHelper) { 
    this.context = context; 
    this.cliHelper = cliHelper; 
    } 

    public static void main(String[] args) { 
    SpringApplication.run(App.class, args); 
    } 
} 
+0

Die Antwort ist in Ihrer Nachricht. Kein Standardkonstruktor, d. H. Öffentliche App() {...} verfügbar. Warum verwenden Sie nicht @Autowired für Felder? – lrother

+0

Ist CLIhelper eine Bohne? –

+0

Ja, CLIHelp ist eine Bean, sie ist als @Component –

Antwort

7

Ihre Klasse ist mit @SpringBootApplication annotiert, die auch ist. Und @Configuration sollte einen Standard-args-Konstruktor haben. Von javadoc:

@Configuration classes must have a default/no-arg constructor and may not use @Autowired constructor parameters.

Seit dem Frühjahr Version 4.3 können Sie Konstruktor Injektion für @Configuration Klasse. Getestet auf Spring Boot Version 1.5.3 und es funktioniert gut.

Here sind die Versionshinweise für Spring 4.3. Und hier ist die Funktion, die Sie brauchen:

@Configuration classes support constructor injection.

Verwandte Themen