Java Champion / JavaOne Rockstar Adam Bien (adam-bien.com) is a self-employed consultant, lecturer, software architect, developer, and author in the enterprise Java sector. He is also the author of several books and articles on Java and Java EE technology, as well as distributed Java programming. adam has posted 59 posts at DZone. View Full User Profile

Debugging Field Injection With NetBeans - Another Reason To Delete Setters

01.25.2011
| 16079 views |
  • submit to reddit

Java EE fully supports field injection:

@Stateless
public class LegacyDataSourceConsumer {
@Inject @Legacy
DataSource ds;
}

You can inject resources, EJBs or CDI managed beans directly into fields since Java EE 5 and so for about 5 years. Setters or specific "injector" methods are optional. With NetBeans you can easily debug the injection with a field breakpoint. You only have to click on the grey bar left the editor to enable a field breakpoint. You should see a triangle:

By clicking on the triangle and choosing Field Breakpoint -> Properties you can setup additional behavior. The default behavior "Stop On: Field Access Or Modification" is perfect for Dependency Injection debugging. The current thread will stop (probably in a business method) at injection time. So: delete your setters and enjoy Java EE :-).

From http://www.adam-bien.com/roller/abien/entry/debugging_field_injection_with_netbeans

Published at DZone with permission of its author, adam bien.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Sirikant Noori replied on Fri, 2012/03/30 - 1:04pm

Hi Adam

I agree that setters are the boilerplate code. But without setters it is hard to provide mocks in your tests :-( How do you set mocks in the tests when the setters are missing?

Thanks

Java Exam

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.