Does Spring require all beans to have a default constructor?

后端 未结 3 1638
旧时难觅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 09:53

    nicholas' answer is right on the money for XML configuration. I'd just like to point out that when using annotations to configure your beans, it's not only simpler to do constructor injection, it's a much more natural way to do it:

    class Foo {
        private SomeDependency someDependency;
        private OtherDependency otherDependency;
    
        @Autowired
        public Foo(SomeDependency someDependency, OtherDependency otherDependency) {
            this.someDependency = someDependency;
            this.otherDependency = otherDependency;
        }
    }
    

提交回复
热议问题