Quantcast
Channel: SpringBoot application doesn't autowire field - Stack Overflow
Viewing all articles
Browse latest Browse all 7

Answer by Matheus Cirillo for SpringBoot application doesn't autowire field

$
0
0

If you want to declare ServiceAImpl as a Spring bean in your Java Configuration file, you should remove the @Service annotation from the class declaration. These annotations doesn't work well together.

ServiceAImpl.java

import org.springframework.beans.factory.annotation.Autowired;

public class ServiceAImpl implements ServiceA {

    private final String fieldA;

    @Autowired
    public ServiceAImpl(String fieldA) {
        this.fieldA = fieldA;
    }

    @Override
    public boolean isFieldA(String text) {
        return fieldA.equals(text);
    }
}

Application.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {
    @Value("${fieldA}")
    private String fieldA;

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

    @Bean
    public ServiceA serviceA() {
        return new ServiceAImpl(fieldA);
    }
}

Your application.properties

fieldA=value

Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>