Declaration of beans in applicationContext.xml

旧街凉风 提交于 2019-12-05 16:01:27

In applicationContext.xml do we need to specify all the classes from the application?

No. Declaring model classes like your net.test.model.Employees is pointless unless you need a prototype to work with, something like initializing its values, but you can do this directly in the class and just instantiate it.

So if I have multiple entity, service and dao classes do I need to mention all those classes in applicationContext.xml?

As I explained before, entity classes no. Services and DAOs are ok because most of the time you need DAOs injected to the Services (and that's the point of DI). But of course, if you create 3 DAOs and you want them to be injected in your 3 Services, then mention them in your Spring XML Bean Definition file (what you call applicationContext.xml).

But one thing, you may want to use package scanning autodetection and annotation based config to avoid writing everything in your Bean Definition File.

The bean declaration in the application context is to register the bean in the application container.

If the bean is not registered, the container wouldn't be able to dependency inject any instance of that class, or apply interceptors to the object of the class.

So unless the reference of bean is not required for any task like intercepting it or inject it, or create default singleton object of it, there is no need to declare it in the applicationContext.xml

Hope this helps.

Ideally yes, another way can be using Spring Annotations so that you don't to add multiple entries in xml.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!