autowire or not to autowire

Posted on 2011-05-04
Last Modified: 2012-06-21

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.
Question by:gudii9
    1 Comment
    LVL 47

    Accepted Solution

    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.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now