关键词:
【中文标题】Spring-boot,无法自动装配类。未找到默认构造函数引发异常【英文标题】:Spring-boot, unable to autowire a class.No default constructor found Exception is raised 【发布时间】:2013-12-24 22:41:46 【问题描述】:我是弹簧靴的新手。在我将一个类移动到不同的包(另一个包含“应用程序”)后,无法实例化 bean 类:未找到默认构造函数引发异常。
之前(可行的代码)
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);
来自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();
当“Application”类和“Franchise”类位于同一个包中时,我可以启动服务器。但是,当我将类“特许经营”移动到另一个包中时,如下所示,我遇到了这个异常:无法实例化 bean 类:未找到默认构造函数引发异常。
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();
如果我想将这个类移到不同的包中,我该如何解决这个问题?
谢谢!
编辑:我找到了解决方案 当我删除以下标签时,我可以将类放入单独的包中。 @配置 @Profile( "默认" )
但我不知道为什么......
【问题讨论】:
@Configuration
需要一个代理,准确地说是一个 cglib 代理,这反过来又需要一个类有一个默认的构造函数。你的@Controller
不应该是@Configuration
(恕我直言,这违反了单一责任规则)。你的 Application
类也是如此,它不是一个控制器,所以为什么它有一个 @Controller
注释。另请注意,@Profile("default")
是多余的,因为这是默认设置。
@M.Deinum 感谢您的解释!
【参考方案1】:
在我看来,您的 Franchise 类是包私有的(java 类的默认可见性)。这将解释一切(不需要涉及 Spring 或编译器以外的任何东西)。要修复它,只需将您的类声明为“public”。
Marten 也是正确的,@Configuration 可能不希望您对特许经营权意味着什么(但在这种情况下它是无害的)。
【讨论】:
非常感谢您的解释!~在 Spring Boot 应用程序中自动装配 ObjectMapper
...发布时间】:2018-07-2809:41:54【问题描述】:我需要在我的Spring-boot应用程序中使用默认的ObjectMapper作为单例实例。我可以在我的应用程序中简单地@autowireObjectMapper(Spring-boot应用程序中默认创建的实例 查看详情
由于未绑定的 RestTemplate,Spring-Boot RestClientTest 无法正确自动配置 MockRestServiceServer
】由于未绑定的RestTemplate,Spring-BootRestClientTest无法正确自动配置MockRestServiceServer【英文标题】:Spring-BootRestClientTestnotcorrectlyauto-configuringMockRestServiceServerduetounboundRestTemplate【发布时间】:2016-12-2811:32:23【问题描述】:编辑:这个... 查看详情
spring-boot 基本 JSP 404 未找到
】spring-boot基本JSP404未找到【英文标题】:spring-bootbasicJSP404NotFound【发布时间】:2015-05-0910:37:53【问题描述】:无法使用spring-boot加载一个非常简单的JSP页面,得到404NotFound。src/main/java/SampleWebJspApplication.java@Configuration@EnableAutoConfi... 查看详情
无法自动装配服务:参数引用类但不存在此类服务
】无法自动装配服务:参数引用类但不存在此类服务【英文标题】:Cannotautowireservice:Argumentreferencesclassbutnosuchserviceexists【发布时间】:2018-06-0923:46:46【问题描述】:我正在将一个项目从Symfony3升级到Symfony4(https://github.com/symfony/sym... 查看详情
未找到依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:
...未找到依赖项:预计至少有1个bean有资格作为此依赖项的自动装配候选者。依赖注解:【英文标题】:Nofoundfordependency:expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotations:【发布时间】:2014-11-2314:36:41【问题描... 查看详情
springboot自动装配原理@enableautoconfiguration
参考技术A1、springboot启动会加载大量的自动配置类:(在下面的spring.factories文件中)2、通过@ConditionalOnXXX判断我们是否导入了相关的功能(就是pom文件中的starter),如果导入了,就会自动配置。4、给容器中添加自动配置类的时... 查看详情
使用 @DataJpaTest 的 Spring 测试无法使用 @Repository 自动装配类(但使用接口存储库可以工作!)
】使用@DataJpaTest的Spring测试无法使用@Repository自动装配类(但使用接口存储库可以工作!)【英文标题】:Springtestwith@DataJpaTestcan\'tautowireclasswith@Repository(butwithinterfacerepositoryworks!)【发布时间】:2018-06-2901:09:17【问题描述】:我试... 查看详情
无法在 Spring Boot 的组件类中自动装配推土机 Mapper
】无法在SpringBoot的组件类中自动装配推土机Mapper【英文标题】:UnabletoautowiredozerMapperincomponentclassinspringboot【发布时间】:2014-11-1120:46:16【问题描述】:我是SpringBoot的新手。我正在尝试使用neo4j数据库在springbootmvc中开发小型应用... 查看详情
休眠弹簧连接无法自动装配
】休眠弹簧连接无法自动装配【英文标题】:HibernateSpringconnectionCouldnotautowire【发布时间】:2017-11-1109:15:37【问题描述】:我正在尝试使用Hibernate和Spring在OracleDB上执行一些粗略的操作。我用HibernateTools生成了这个类。我创建一个... 查看详情
自动装配[@autowired]的歧义性
...口有多个实现类,那么自动装配就会出现错误,因为Spring无法判断到底要装配哪个实现类实例(bean)。1.可以使用@Qualifier("beanName")明确指定要注入的是哪个bean@Autowired@Qualifier("beanName")privateMyBeanmyBean;----------------------------- 查看详情
Thymeleaf-Spring4 无法自动装配 TemplateEngine
】Thymeleaf-Spring4无法自动装配TemplateEngine【英文标题】:Thymeleaf-Spring4unabletoautowireTemplateEngine【发布时间】:2014-07-2113:29:58【问题描述】:我正在尝试将Thymeleaf电子邮件模板添加到工作中的SpringMVC+Thymeleaf应用程序中,如here所述。... 查看详情
未找到 Composer 自动加载类
】未找到Composer自动加载类【英文标题】:ComposerAutoloadingclassesnotfound【发布时间】:2017-02-2819:36:48【问题描述】:我的文件夹结构如下:includes/libraries/Classes/Contact/Contact.phpContactController.phpadmin/controllers/contact/edit.phpContact.php是我... 查看详情
无法识别启动活动,未找到默认活动启动活动时出错
】无法识别启动活动,未找到默认活动启动活动时出错【英文标题】:Couldnotidentifylaunchactivity,DefaultActivitynotfoundErrorwhileLaunchingactivity【发布时间】:2017-01-2809:02:50【问题描述】:启动我的活动时似乎出现错误;未找到默认活动。... 查看详情
自动化装配bean(代码片段)
一、Spring装配-自动化装配@Component和@ComponentScan通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组件扫描默认是不启动的,需要显式的配置Spring,从而命令Spring去寻找带有(@Component)注解... 查看详情
使用@SpringBootTest时如何在测试类中自动装配bean
...启动完整的应用程序上下文并让我执行我的测试。但是我无法将@Autowiredbean放入测试类本身。相反,我得到一个错误:没有 查看详情
Spring-Boot MVC 模板未加载(未找到 404)
】Spring-BootMVC模板未加载(未找到404)【英文标题】:Spring-BootMVCTemplateNotLoading(404NotFound)【发布时间】:2015-01-0320:05:11【问题描述】:我在这里有一个非常简单的Spring-BootMVC应用程序,但它不工作。一个控制器,一个页面未加载... 查看详情
Azure - 无法自动缩放,因为未找到监控数据
】Azure-无法自动缩放,因为未找到监控数据【英文标题】:Azure-Couldnotautomaticallyscalebecausemonitoringdatawasnotfound【发布时间】:2013-11-1221:05:15【问题描述】:我们在Azure中使用自动缩放预览,但在过去几个小时内,门户一直报告以下... 查看详情
@autowired和@resouce注入顺序
...时优先选择此bean,但优先级不如@Qualifier指定的bean若还是无法匹配上则会抛出BeanCreationException异常 查看详情