patterns & best practices for cdi · pdf filesession 20181 patterns & best practices...

50
SESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza Rahman Resin Developer, Java EE/EJB/JMS JCP expert, Author EJB 3 in Action

Upload: nguyenquynh

Post on 20-Mar-2018

218 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

S E S S I O N 2 0 1 8 1

PATTERNS & BEST PRACTICES FOR CDI

Ryan Cuprak

e-Formulation Analyst, Author, Connecticut Java Users Group President

Reza Rahman Resin Developer, Java EE/EJB/JMS JCP expert, Author EJB 3 in Action

Page 2: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

AGENDA

• CDI Overview •  XML Configuration vs.

Annotations • Qualifiers • Naming •  Scoping •  Stereotypes •  Inceptors &

Decorators • Dependency vs.

Resource Injection

• CDI Integration •  EJB •  JSF •  JPA

•  Extensions • Conclusion • Q&A

Page 3: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

OVERVIEW

•  What is CDI? •  Dependency injection standard for Java EE. •  Based on Seam 2, Google Guice, and Spring. •  Defined in JSR-299.

•  Why CDI? •  Improve Java EE dependency injection support. •  Provide a standard DI for both enterprise and web

applications. •  Provide a Java-centric type safety injection using

annotations.

•  As adoption grows, it is important to understand how to leverage CDI effectively.

Page 4: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

OVERVIEW

•  Basic dependency injection: •  @Inject, @Qualifier, @Stereotype, @Alternative, @All, @Any,

@New

•  Component naming: •  @Named

•  Custom object factories: •  @Produces, @Disposes, InjectionPoint

•  Context management: •  @Dependent, @RequestScoped, @SessionScoped, •  @ConversationScoped, @ApplicationScoped, @Scope

Page 5: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

OVERVIEW

•  Lightweight events: Event, @Observes

•  Interceptors/Decorators @Interceptor, @InterceptorBinding, @AroundInvoke, InvocationContext, @Decorator, @Delegate

•  Portable extensions SPI!

Page 6: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

CONFIGURATION

•  CDI is annotation centric: •  Annotations are ideal for type-safe dependency injection. •  Compact, semi-permanent, and semantically close to

code, tools-friendly.

•  CDI does support XML configuration (beans.xml): •  Override annotation configuration. •  Perform DI using JRE classes or third-party libraries. •  Plug-ins provide enhanced configuration and behavior

support: •  Seam 3 XML •  CanDI XML

•  Some developers prefer XML over annotations.

Page 7: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

CONFIGURATION

Let’s look at a simple example leveraging XML configuration. •  Pieces: •  DefaultAccountService – EJB stateless session bean. •  DefaultAccountDAO – performs basic CRUD operations •  MockAccountDAO – replaces DefaultAccountDAO for unit

testing. •  @Audited – interceptor used for auditing. •  @Profiled – interceptor collecting profiling data

•  Deployments types: •  Unit Testing, Integration Testing, Production

Page 8: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

XML DEPLOYMENT EXAMPLE

Page 9: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

UNIT TESTING

META-INF/beans.xml

Page 10: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

INTEGRATION TESTING

META-INF/beans.xml

Page 11: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

PRODUCTION

META-INF/beans.xml

Page 12: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

XML CONFIGURATION

Using the beans-test.xml:

Page 13: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

OVERRIDE JAVA/ANNOTATION DEFAULTS

Override default values in a bean via XML:

Page 14: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

WIRING THIRD-PARTY EXTENSIONS

Configure a JRE class for injection, support multiple instances:

new DecimalFormat(“$0.00”);

Page 15: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

XML OBJECT WIRING

Define MailSender and define two different qualifiers for injecting two difference instances:

Page 16: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

XML OBJECT WIRING

Configure two instances of MailSender:

Page 17: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

XML OBJECT WIRING

Injecting the MailSupport MailSender instance:

Page 18: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

QUALIFIER TYPE-SAFETY VS. VERBOSITY

•  Benefits of Qualifiers? •  Type-safe •  Java-centric dependency resolution •  Enhances readability, fluency, maintainability

•  Drawbacks: •  Can be verbose •  Qualifiers can be used with enumerations to reduce

verbosity

•  Best Practice: •  @Named as Qualifier – should be used sparingly because

they are a lot more brittle.

Page 19: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

TYPE-BASED QUALIFIERS

@Qualifier @Retention(RUNTIME) @Target({ FIELD, TYPE }) public @interface Admin {} @Qualifier @Retention(RUNTIME) @Target({ FIELD, TYPE }) public @interface Support {} @Inject @Support private MailSender mailSender;

Page 20: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

QUALIFIERS WITH ENUMERATIONS

Qualifiers with enumerations reduce verbosity: @Qualifier @Retention(RUNTIME) @Target({ FIELD, TYPE }) public @interface MailSenderType { MailSenderTypes value(); } public enum MailSenderTypes { ADMIN, SUPPORT } @Inject @MailSenderType(MailSenderTypes.SUPPORT) private MailSender mailSender;

Page 21: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

@NAMED AS QUALIFIER

Named can be used as a Qualifier (brittle): @Named(“admin”) public class AdminMailSender implements MailSender { … } @Named(“support”) public class SupportMailSender implements MailSender { … } @Inject @Named(“support”) private MailSender mailSender;

Page 22: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

COMPONENT NAMING

Practices when using the @Named annotation: •  String based names not needed for dependency

resolution. •  Only necessary to name components that are

referenced outside of Java such as in JSF/Facelets via EL.

•  Default names good enough in most cases. •  Be careful of naming conflicts – name-based

resolution not type-safe! •  Choose names that add to readability/API fluency.

Page 23: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

SPECIFYING COMPONENT NAMES

Implicit name using class name: @Named @ConversationScoped public class TransferWizard { <h:inputText id="amount” value="#{transferWizard.amount}"/> Explicit name: @Named(“transfer”) @ConversationScoped public class TransferWizard { <h:inputText id="amount" value="#{transfer.amount}"/>

Page 24: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

COMPONENT SCOPING

What are the scope? •  Fine-grained scopes – @ApplicationScoped,

@SessionScoped, @ConversationScoped, @RequestScoped, @Dependent

•  Scoping extensions – @WindowScoped, @RenderScoped, @ViewScoped, Flash, @TransactionScoped, @ThreadScoped

What are the purposes and best practices? •  Components with appropriate scopes maximize

memory usage. •  Scopes remove boilerplate state maintenance code –

especially at presentation tier. •  Make judicious use of conversations!

Page 25: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

SCOPE GRANULARITY

Page 26: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

CONVERSATION SCOPE

Page 27: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

GROUPING METADATA IN STEREOTYPES

•  Components self-organize into roles in well-architected systems. •  Roles get surfaced as groups of recurring

metadata. •  CDI allows component roles to be articulated as

stereotypes. •  Stereotypes encapsulate specialized behavior and

can be cumulative.

Page 28: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

STEREOTYPES FOR COMPONENT ROLES

Examples of stereotypes: Dao, Service, Model, Mock, Model. Note: Model is built-in.

Page 29: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

INTERCEPTORS VS. DECORATORS

•  Interceptors and Decorators both geared towards cross-cutting logic. •  Bypass traditional complexity associated with AOP

by avoiding point-cuts. •  Interceptors are designed for system-level cross-

cutting concerns very decoupled from business logic. •  Decorators intended for concerns that should be

compartmentalized but are still very close to business logic.

Page 30: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

INTERCEPTOR USAGE

Defining the interceptor: @InterceptorBinding @Target({TYPE, METHOD}) @Retention(RUNTIME) public @interface Audited {} @Interceptor @Audited public class AuditInterceptor { @AroundInvoke public Object audit(InvocationContext context) throws Exception { System.out.print("Executing: “ + context.getMethod().getName()); System.out.println(" with args: ”+ Arrays.toString(context.getParameters())); return context.proceed(); } }

Using the interceptor: @Stateless public class DefaultAccountService implements AccountService { ... @Audited public void addAccount(Account account) {

Page 31: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

DECORATOR USAGE

Page 32: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

INJECTION AND LOOK-UP

•  Declarative, static injection sufficient for most cases •  @Produces, @Disposes used when objects must be

programmatically created/destroyed •  Look-up using Instance used when decencies need

to be resolved at runtime •  For very dynamic resolution,

BeanManager.getBeans SPI can be used

Page 33: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

PRODUCING OBJECTS AT RUNTIME

@Inject @LoggedIn private User user; @Inject private AccountService accountService; @Named @Produces @SessionScoped @SelectedAccount public Account getCurrentAccount() { return accountService.getAccount(user.getId()); } @Produces public Logger createLogger(InjectionPoint injectionPoint) { return Logger.getLogger(injectionPoint.getMember() .getDeclaringClass().getName()); }

Page 34: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

DYNAMIC LOOK-UP

@Inject @Holiday private Instance<Discount> discount; ... if (!discount.isUnsatisfied()) { Discount currentDiscount = discount.get(); }

Page 35: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

DEPENDENCY VS. RESOURCE INJECTION

•  Java EE 5 introduced resource injection annotations - @Resource, @EJB, @PersistenceContext •  Limited, specialized, name-based injection

•  Java EE 6 introduces CDI •  Powerful, general purpose, type-safe injection •  Mostly orthogonal functionality, some overlap •  Favor CDI for greater flexibility

Page 36: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

JAVA EE INJECTION FLAVORS

@Inject private AccountDao accountDao; @Inject // @EJB private AccountService accountService; @Inject // @Resource private UserTransaction userTransaction; @PersistenceContext private EntityManager entityManager; @Resource(name="jdbc/AcmeBankDB") private DataSource dataSource; @Resource(name="jms/TransferQueue") private Queue transferQueue;

Page 37: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

WHEN TO USE EJB?

•  Use EJB for enterprise services – like transactions •  Use CDI managed beans otherwise – EJB does

have overhead •  Avoid plain managed beans •  EJBs most appropriate for service/business logic tier •  CDI makes it possible to use EJBs as JSF managed

beans – only good practice for RAD/prototyping •  Using EJB services without EJB

Page 38: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

CDI & EJB

Page 39: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

CDI AND JSF

•  CDI more powerful replacement to JSF managed beans •  Seam Faces excellent enhancement •  CDI naming, scopes, producers, qualifiers work in

powerful ways with JSF •  CDI designed with JSF in mind, but not limited to JSF

– Wicket, Struts 2, Spring MVC, Swing Desktop application.

Page 40: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

CDI & JSF

Page 41: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

CDI & JSF

Page 42: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

CDI AND JPA

•  CDI/JPA bean life-cycle mismatch •  Life-cycle mismatch can be bridged by CDI

producers •  Make judicious use of stateful session bean

extended persistence context

Page 43: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

PRODUCING ENTITY BEANS

Page 44: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

USING PORTABLE EXTENSIONS

•  Portable extensions perhaps most important part of CDI •  Rich CDI ecosystem already evolving •  Tools/utilities, extending Java EE, integration with

Java EE APIs, integrating with non-standard APIs, making Java EE features available in non-Java EE environments •  Arquillian possibly most important CDI extension •  Use portable extensions judiciously

Page 45: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

CDI ECOSYSTEM SNAPSHOT

Implementations

Weld CanDI

OpenWebBeans

Runtimes Portable Extensions

Tools

Page 46: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

CDI PORTABLE EXTENSIONS SNAPSHOT

Page 47: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

ARQUILLIAN UNIT TEST

Page 48: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

SUMMARY

•  CDI critical part of Java EE – now and going forward •  CDI well-received by community •  Focus on loose-coupling, Java-centric type-safety,

annotations, expressiveness, fluency and ease-of-use •  Utilize CDI effectively to maximize benefits •  Continuously refactor to patterns •  Have fun!

Page 49: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

REFERENCES

•  JSR 299: Contexts and Dependency Injection for Java EE, http://jcp.org/en/jsr/detail?id=299 •  Weld Reference, http://docs.jboss.org/weld/

reference/1.0.0/en-US/html/ •  CDI Advocacy, http://cdisource.org/site/ •  Weld, the JBoss reference implementation for JSR

299, http://seamframework.org/Weld •  OpenWebBeans, Apache implementation of JSR

299, http://openwebbeans.apache.org •  CanDI, the JSR 299 implementation for Caucho

Resin, http://caucho.com/projects/candi/

Page 50: PATTERNS & BEST PRACTICES FOR CDI · PDF fileSESSION 20181 PATTERNS & BEST PRACTICES FOR CDI Ryan Cuprak e-Formulation Analyst, Author, Connecticut Java Users Group President Reza

REFERENCES

•  Seam 3, http://seamframework.org •  Apache MyFaces CODI, http://

myfaces.apache.org/extensions/cdi/ •  Arquillian, http://www.jboss.org/arquillian •  Seam Forge, http://seamframework.org/

Documentation/SeamForge