Question

I am new to spring-boot. After i moved a class to different package (other the one contains 'Application'), Could not instantiate bean class: No default constructor found Exception is raised.

Before (workable code)

package com.server;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Controller;

@Configuration
@ComponentScan(basePackages = {"com.server" })
@EnableAutoConfiguration
@Profile({ "default" })
@Controller
public class Application  {

    private static Log logger = LogFactory.getLog(Application.class);

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

}

A piece of code from http://bitwiseor.com/2013/09/20/creating-test-services-with-spring-boot/

package com.server;

import java.util.Collections;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Configuration
@Controller
@Profile({ "default" })
class Franchise {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public Franchise(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @ResponseBody
    @RequestMapping("/api/franchise/{id}")
    String franchiseId(@PathVariable Long id) {
        try {
            return jdbcTemplate.queryForMap("SELECT id, title FROM franchises WHERE id=?", id).toString();
        } catch(EmptyResultDataAccessException ex) {
            return Collections.EMPTY_MAP.toString();
        }
    }

    @ResponseBody
    @RequestMapping("/api/franchise")
    String franchises() {
        try {
            return jdbcTemplate.queryForList("SELECT id, title FROM franchises").toString();
        } catch(EmptyResultDataAccessException ex) {
            return Collections.EMPTY_MAP.toString();
        }
    }
}

I am able to bring up the server when class 'Application' and 'Franchise' are located in the same package. However, when I moved the class 'Franchise' into another package as shown below, I've got this exception: Could not instantiate bean class: No default constructor found Exception is raised.

package com.server.api;

import java.util.Collections;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Configuration
@Controller
@Profile({ "default" })
class Franchise {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public Franchise(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @ResponseBody
    @RequestMapping("/api/franchise/{id}")
    String franchiseId(@PathVariable Long id) {
        try {
            return jdbcTemplate.queryForMap("SELECT id, title FROM franchises WHERE id=?", id).toString();
        } catch(EmptyResultDataAccessException ex) {
            return Collections.EMPTY_MAP.toString();
        }
    }

    @ResponseBody
    @RequestMapping("/api/franchise")
    String franchises() {
        try {
            return jdbcTemplate.queryForList("SELECT id, title FROM franchises").toString();
        } catch(EmptyResultDataAccessException ex) {
            return Collections.EMPTY_MAP.toString();
        }
    }
}

How can I solve this problem if I wanted to move this class into a different package?

Thanks!


Edit: I found a solution When I removed the following tag, I am able to put the class into separate package. @Configuration @Profile({ "default" })

But I have no idea why...

Was it helpful?

Solution

It looks to me like your Franchise class is package private (the default visibility for a java class). That would explain everything (and no need to involve Spring or anything other than a compiler). To fix it just declare your class to be "public".

Marten is also correct that @Configuration is probably not want you mean for the Franchise (but in this case it's harmless).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top