DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > The most common mistake when using dependency injection frameworks

The most common mistake when using dependency injection frameworks

Bozhidar Bozhanov user avatar by
Bozhidar Bozhanov
·
Sep. 09, 11 · Java Zone · Interview
Like (0)
Save
Tweet
7.77K Views

Join the DZone community and get the full member experience.

Join For Free
I’ve been answering spring and CDI questions on stackoverflow quite a lot, and what I notice as a recurring mistake (or misconception) is that people aren’t aware that they should not instantiate the objects in which they want their dependencies injected.

They assume that when they do Foo foo = new Foo(), the dependencies of Foo will be injected by spring. No, they won’t be. Here’s an excerpt from an answer of mine to such a question:

First, and most important – all spring beans are managed – they “live” inside a container, called “application context”.

Second, each application has an entry point to that context. Web applications have a Servlet, JSF uses a el-resolver, etc. Also, there is a place where the application context is bootstrapped and all beans – autowired. In web applications this can be a startup listener.

Autowiring happens by placing an instance of one bean into the desired field in an instance of another bean. Both classes should be beans, i.e. they should be defined to live in the application context.

What is “living” in the application context? This means that the context instantiates the objects, not you. I.e. – you never make new UserServiceImpl() – the container finds each injection point and sets an instance there.

So do not instantiate objects yourself. Configure them to be instantiated by spring (via xml, annotations or java config).

(FYI: there is a way of getting dependencies injected into objects instantiated by the user rather than by the container. It uses AspectJ weaving, in short: it makes bytecode modifications so that whenever an object is instantiated it gets managed by the container. But this is “black magic” and is not “on” by default. And it certainly isn’t supported in many DI frameworks)

 

From http://techblog.bozho.net/?p=423

Dependency injection

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Progressive Delivery With Argo Rollouts: Blue-Green Deployment
  • Demystifying Cloud-Native Data Management: Layers of Operation
  • Data Mesh — Graduating Your Data to Next Level
  • How Java Apps Litter Beyond the Heap

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo