Spring MVC Default or no-arg Constructor in config 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 the Core Spring 4.2 Certification Mock core exam and regarding this class, it says that "Default or no-arg constructor is mandatory."


I tried this class in a test I did and it works fine without the Default or no-arg (?) Constructor, SO I think that there is no need for a Default or no-arg constructor

+3


source to share


1 answer


If your Spring version is <= 4.2, then this statement is true. However, for Spring> = 4.3 it is allowed to have a class @Configuration

without a no-args constructor.

See also the Major improvements to containers section of the reference guide on this subject. It was implemented with the release of SPR-13471 in Spring 4.3 RC1.



Pro Tip: If you only have one constructor, you don't need @Autowired

a constructor anymore . (See SPR-12278 for this).

+5


source







All Articles