spring dependency injection using java config

@Configuration
@ComponentScan(basePackages={"com.xyz.abc"})
public class SimpleTest {

Open in new window


when we prefer spring dependency injection using java config as above vs regular manual dependency injection. Can you please provide some simple examples on both above approaches with pros and cons.
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
configuration over convention. I've mentioned this on another post you have posted.
It all boils down to personal preference. There is no right or wrong way.
Jim CakalicSenior Developer/ArchitectCommented:
Borrowed unabashedly from Why Use the Spring Framework?

Dependency Injection is a form of push configuration; the container "pushes" dependencies into application objects at runtime. This is the opposite of traditional pull configuration, in which the application object "pulls" dependencies from its environment. Thus, Dependency Injection objects never load custom properties or go to a database to load configuration — the framework is wholly responsible for actually reading configuration.

Push configuration has a number of compelling advantages over traditional pull configuration. For example, it means that:

Application classes are self-documenting, and dependencies are explicit. It's merely necessary to look at the constructors and other methods on a class to see its configuration requirements. There's no danger that the class will expect its own undocumented properties or other formats.

For the same reason, documentation of dependencies is always up-to-date.

There's little or no lock-in to a particular framework, or proprietary code, for configuration management. It's all done through the Java language itself.

As the framework is wholly responsible for reading configuration, it's possible to switch where configuration comes from without breaking application code. For example, the same application classes could be configured from XML files, properties files, or a database without needing to be changed.

As the framework is wholly responsible for reading configuration, there is usually greater consistency in configuration management. Gone are the days when each developer will approach configuration management differently.

Code in application classes focuses on the relevant business responsibilities. There's no need to waste time writing configuration management code, and configuration management code won't obscure business logic. A key piece of application plumbing is kept out of the developer's way.

Developers who try Dependency Injection rapidly become hooked. These advantages are even more apparent in practice than they sound in theory.

I'd add to this list that delegating object instantiation and configuration to a container enables aspect-oriented programming.

Spring now supports configuration using XML, annotations or Java configuration as in your example. It really depends on what you (or the development team) is most comfortable with. Some people simply detest XML. Or they haven't done a good job of breaking up XML configuration in the past which can lead to a mess. Or they don't like separating the configuration from the implementation. The annotations do provide some benefit in my experience over the XML. Java configuration is the newcomer and I can't really see the point other than to satisfy some sort of feature checklist (framework X has it so we need it too) or for people who seem to think that everything should be done in Java. I don't personally care for Java configuration and try to avoid using it. I'm willing to change my mind on that if somebody can make a good case for it.

Regards,
Jim

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.