Spring-boot, unable to autowire a class.No default constructor found Exception is raised

Elvin

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...

Dave Syer

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring boot No default constructor found on @SpringBootApplication class

Spring boot test unable to autowire service class

How to autowire default XmlMapper in Spring Boot application

Spring MVC no default constructor found?

Spring boot Constructor Autowired Exception

Spring @Autowired constructor gives No default constructor found

Unable to Autowire through constructor

Spring Boot: Handle Raised Exception from Database Trigger

Spring Boot : Load property file in constructor and use as autowire annotation

No default constructor found; nested exception is java.lang.NoSuchMethodException with Spring MVC?

No primary or default constructor found for interface java.util.List Rest API Spring boot

Spring-Boot Cloug Config Client unable to Autowire

Exception not raised when file not found

Spring Boot Tomcat Class Not Found

Spring boot error : Constructor threw exception

Spring Boot + LocalDate: "No primary or default constructor"

Trying to call a class with constructor but @Autowired var is throwing Null Pointer Exception in Kotlin/Spring Boot

Spring Boot, @Autowire into an unmanaged class using @Configurable and load time weaving

Spring Boot can't autowire map bean in @configuration class

I can't autowire Service class in Spring Boot Test

How to autowire a bean in other class in a Spring Boot application?

Autowire class with arguments in constructor fails

Spring Boot - throw exception or indicate item is not found

Spring @Autowire on Properties vs Constructor

spring boot override default REST exception handler

Class path issue in Spring: File Not Found Exception

Spring Boot could not autowire and run

Could not autowire SessionRegistry in spring boot

How to Autowire conditionally in spring boot?