简体   繁体   English

配置类中的Spring MVC Default或no-arg构造函数

[英]Spring MVC Default or no-arg constructor in configuration classes

@Configuration
public class ApplicationConfig {

   private DataSource dataSource;

  @Autowired
  public ApplicationConfig(DataSource dataSource) {
    this.dataSource = dataSource;
  }

  @Bean(name="clientRepository")
  ClientRepository jpaClientRepository() {
    return new JpaClientRepository();
   }
}

I just found a Core Spring 4.2 Certification Mock Exam and regarding this class it says that "Default or no-arg constructor is mandatory." 我刚刚发现了一个Core Spring 4.2认证模拟考试,关于这个类,它说"Default or no-arg constructor is mandatory."
I tried this class in a test I made and it works perfectly without Default or no-arg constructor (?) , SO I think there is no need of a Default or no-arg constructor 我在我做的测试中尝试了这个类,它没有默认或无参数构造函数(?)就完美地运行了,所以我认为不需要Default或no-arg构造函数

If your Spring version is <= 4.2 then that statement is true. 如果您的Spring版本<= 4.2,则该语句为真。 However for Spring >= 4.3 it is allowed to have a @Configuration class without a no-args constructor. 但是对于Spring> = 4.3,允许使用没有no-args构造函数的@Configuration类。

See also the Core Container Improvements section in the reference guide about this. 另请参阅参考指南中的核心容器改进部分。 It has been implemented with issue SPR-13471 in Spring 4.3 RC1. 它已在Spring 4.3 RC1中的SPR-13471中实现。

Pro Tip: if you only have a single constructor you don't need @Autowired on the constructor anymore. 专业提示:如果您只有一个构造函数,则不再需要在构造函数上使用@Autowired (See SPR-12278 ) for that). (见SPR-12278 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM