LakiGeri
LakiGeri

Reputation: 2105

How define the dependency injections in spring-boot?

I try to create a spring web application with spring-boot. My first problem that the dependency injection does not work for me. This is the article what I followed.

I created an Application class:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

then I made a controller:

@RestController
public class GreetingController {

    @Autowired
    WfExampleDao wfExampleDao;

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        wfStartDao.insert(null);
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

my exampledao:

public interface WfExampleDao {
    public void insert(WfExample wfExample);
}

and my implementation of interface:

@Component
public class WfExampleDaoImpl extends JdbcDaoSupport implements WfExampleDao {

    private Logger logger;

    public WfExampleDaoImpl() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    @Override
    public void insert(WfExample wfExample) {
        logger.info("Insert is not implemented yet.");
    }
}

my gradle file:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

war {
    baseName = 'gs-rest-service'
    version =  '0.1.0'
}

jar {
    baseName = 'gs-rest-service'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile group: 'org.springframework', name: 'spring-context', version: '4.3.11.RELEASE'
    compile 'org.springframework:spring-jdbc:4.3.11.RELEASE'

    compile 'commons-dbcp:commons-dbcp:1.4'
    compile 'commons-pool:commons-pool:1.6'


    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.6.RELEASE'
    providedRuntime group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: '1.5.6.RELEASE'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.6.RELEASE'
} 

What I expect: When I open the /greeting page, log will appear, but I got this in the begining of the gradle bootRun:

2017-09-12 10:47:56.058 WARN 7545 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'greetingController': Unsatisfied dependency expressed through field 'wfExampleDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'hu.example.dao.WfExampleDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I don't know why can not find the dependency. As I read, I don't have to create an applicationContext.xml because the bootRun figures out the dependencies.

Thanks for the advices in advance!

Upvotes: 0

Views: 773

Answers (1)

cuongnguyen
cuongnguyen

Reputation: 96

At default, springboot will scan all components in child packages of Application class. You didn't specify the component scan so make sure all classes are in same package or child packages of Application.

Upvotes: 1

Related Questions