当明确定义构造函数时,没有在类中定义了0个参数的构造函数

Eri*_*ang 3 java spring constructor

我不明白...我实际上定义了构造函数,但是

No constructor with 0 arguments defined in class

@Component
public class CustomMessageSource extends ReloadableResourceBundleMessageSource {

public CustomMessageSource(Locale locale){

    this.propertiesHolder = getMergedProperties(locale);        

    this.properties = propertiesHolder.getProperties();
}
//... other setting, getters
Run Code Online (Sandbox Code Playgroud)

这就是我实例化的方式

CustomMessageSource customMessage = new CustomMessageSource(locale);

这是我的堆栈跟踪

Caused by: java.lang.NoSuchMethodException: com.app.service.CustomMessageSource.<init>()
at java.lang.Class.getConstructor0(Class.java:3074)
at java.lang.Class.getDeclaredConstructor(Class.java:2170)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
... 38 more
Run Code Online (Sandbox Code Playgroud)

dav*_*xxx 5

默认情况下,Spring希望通过反射实例化no arg构造函数:

 com.app.service.CustomMessageSource.<init>()
Run Code Online (Sandbox Code Playgroud)

通过@Autowired在构造函数中指定,它应该可以工作:

@Autowired
public CustomMessageSource(Locale locale){
Run Code Online (Sandbox Code Playgroud)

如果使用Spring 4.3或更高版本,则使用单个构造函数声明的bean不需要指定@Autowired注释。来源:https : //spring.io/blog/2016/03/04/core-container-refinements-in-spring-framework-4-3