• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

autowire or not to autowire



I was reading folowing lines and I did not understand clearly.

 To autowire or not to autowire
Although autowiring seems to be a powerful way to cut down on the amount of
manual configuration required when writing the bean wiring file, it may lead to
some problems.
For example, suppose that the studentService bean is set to be autowired
using byName. As a result, its studentDao property will automatically be set to the
bean in the container whose name is studentDao. Lets say that you decide that
you want to refactor the studentDao property, renaming it as studentData. After
refactoring, the container will try to autowire by looking for a bean named
studentData. Unless you have changed the bean XML file, it wont find a bean by
that name and will leave the property unwired. When the studentService tries to use the studentData property, youll get a NullPointerException.wired to the

studentData property, Depending on the type of the student-
Data bean, Spring may quietly wire in the unwanted bean, resulting in unexpected
application behavior.
Autowiring is a powerful tool

Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
0
gudii9
Asked:
gudii9
1 Solution
 
for_yanCommented:
Well, this outlines common situations which happen with autowiring, which is
certainly a convenient practice as it saves a lot of manual
work when creating bean-wiring file but as in many cases,
automation can go wrong. One common problem has to do with autowiring by type.
As was mentioned in the response to autowire byName and byType
question, this case implies that you may have only one bean of a given
type. If more than one bean of this type is encountered the problem
will arise.

Another common problem may happen when the name of the bean which is refrred by name
is being changed later without making adequate change in the
bean-wiring file - of course that would lead to a problem, and
even worse when it happens that the change of the name
results in the wiring to not to non-existing, but to unwanted
bean, that would cause a problem that would be more difficult to
discern and debug.

Another issue about autowiring is that it does not look so transparent - when you
wire propety explicitly you see it much more obviously
in the XML file. So when autowiring is used, when you read XML
file you need sometimes to perform autowiring yourself in your mind,
finding the bean which is referred to
and understanding how the wiring is done.
Well, for any automation on program level you pay with the necessity
to spend more thinking when you need to understand, say, what
went wrong.
So the bottomline is that there are good things and bad things aabout autowirining.

 


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now