Does Spring require all beans to have a default constructor?

后端 未结 3 1635
旧时难觅i
旧时难觅i 2020-12-06 09:09

I don\'t want to create a default constructor for my auditRecord class.

But Spring seems to insist on it:

org.springframework.beans.fact         


        
3条回答
  •  [愿得一人]
    2020-12-06 10:03

    No, you are not required to use default (no arg) constructors.

    How did you define your bean? It sounds like you may have told Spring to instantiate your bean something like one of these:

    
    
    
      
    
    

    Where you did not provide a constructor argument. The previous will use default (or no arg) constructors. If you want to use a constructor that takes in arguments, you need to specify them with the constructor-arg element like so:

    
      
    
    

    If you want to reference another bean in your application context, you can do it using the ref attribute of the constructor-arg element rather than the val attribute.

    
      
    
    
    
    

提交回复
热议问题