为什么我必须在由@Constructor注释annoted Spring配置类的默认构造函数? [英] Why I must have a default constructor in a Spring configuration class annoted by the @Constructor annotation?

查看:647
本文介绍了为什么我必须在由@Constructor注释annoted Spring配置类的默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习的春天核心认证,所提供的学习的东西,我有这个问题,但我无法给出答案吧。

I am studying for Spring Core certification and, on the provided study stuff, I have this question but I can't give an answer to it.

为什么你一定要在你的@Configuration默认构造函数
  注解的类?

Why must you have to have a default constructor in your @Configuration annotated class?

我不声明任何构造成在 @Configuration 注释annoted我的配置类。默认构造函数是由超类继承的人吗?或者是什么?为什么我必须有一个默认的构造函数,我不能覆盖它?

I don't declare any constructor into my configuration classes annoted by the @Configuration annotation. The default constructor is the one inherited by the super class? or what? Why I must have a default constructor and I can't override it?

TNX

推荐答案

据<一个href=\"http://docs.spring.io/spring-framework/docs/3.2.0.M2/api/org/springframework/context/annotation/Configuration.html\">official春天的javadoc ,春天@Configuration注解的类都要求有默认的无参数的构造函数

According to official spring javadoc, spring @Configuration annotated classes are required to have default no-arg constructor

@Configuration类都必须有一个缺省的/无参数的构造函数,可以不使用@Autowired构造函数的参数。任何嵌套配置类必须是静态

@Configuration classes must have a default/no-arg constructor and may not use @Autowired constructor parameters. Any nested configuration classes must be static

原因是,Spring使用 CGLIB代理@Configuration 类和有限制的春天,<一个href=\"http://blog.$c$cleak.pl/2014/07/spring-4-cglib-based-proxy-classes-with-no-default-ctor.html\">that与CGLIB 4版之前的代理类都要求有默认的无参数的构造。

The reason is that spring uses CGLIB to proxy @Configuration classes and there is limitation in Spring, that classes proxied with CGLIB prior to version 4 are required to have default no-args constructor.

在Spring 4,基于CGLIB的代理类需要一个默认的构造函数之前。而这是不CGLIB库的限制,但是Spring本身。幸运的是,弹簧4这不再是一个问题。基于CGLIB的代理类不再需要默认的构造函数。

Prior to Spring 4, CGLIB-based proxy classes require a default constructor. And this is not the limitation of CGLIB library, but Spring itself. Fortunately, as of Spring 4 this is no longer an issue. CGLIB-based proxy classes no longer require a default constructor.

这篇关于为什么我必须在由@Constructor注释annoted Spring配置类的默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆