7 Context Dependency Injection 1 1 m7 Slides
7 Context Dependency Injection 1 1 m7 Slides
1 within Java EE 7
Antonio Goncalves
www.antoniogoncalves.org
@agoncal
Module Outline
Java Enterprise Edition 7
CDI integration
JSF
Transactional components
JPA
Bean Validation
Summary
References
☂
Umbrella specification
JSR 342
Contains 32 specifications
Make them work together
Concurrency 1.0
Interceptors 1.2
JSON-P 1.0
Servlet 3.1 Web Socket 1.0
CDI 1.1
Batch 1.0
JTA 1.2 EJB 3.2 JMS 2.0
JavaMail 1.5
Java EE 7
Concurrency 1.0
Interceptors 1.2
JSON-P 1.0
Servlet 3.1 Web Socket 1.0
CDI 1.1
Batch 1.0
JTA 1.2 EJB 3.2 JMS 2.0
JavaMail 1.5
Java EE 7
Automatic in Java EE
Programmatic in Java SE
No standard API yet
Deltaspike
Standardized in CDI 2.0
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Bootstrapping CDI in Java SE
public class BookService {
@Inject
private NumberGenerator generator;
Backing beans
CDI services
CDI scopes
@ViewScoped
@FlowScoped
Facelets pages
Expression language
@Named
@Inject
private CustomerService service;
@Inject
private Conversation conversation;
<h:outputLabel value="Name:"/>
<h:inputText value="#{customerBean.customer.name}"/>
<h:outputLabel value="Email:"/>
<h:inputText value="#{customerBean.customer.email}"/>
<h:commandLink value="Create"
action='#{customerBean.doCreate}'/>
</h:form>
</h:body>
</html>
Stateless
Request scope
Stateful
Choose from application, session, conversation scope
@PersistenceContext(unitName = "myPU")
private EntityManager em;
@Inject @Paper(SECOND_HAND)
private Event<Book> bookEvent;
@PostConstruct
private void init () {...}
@Inject
private EntityManager em;
@Inject @Paper(SECOND_HAND)
private Event<Book> bookEvent;
@PostConstruct
private void init () {...}
@Inject
private EntityManager em;
@Inject @Paper(SECOND_HAND)
private Event<Book> bookEvent;
@PostConstruct
private void init () {...}
Java Transaction API
JSR 907
@Inject
private EntityManager em;
@Inject @Paper(SECOND_HAND)
private Event<Book> bookEvent;
@PostConstruct
private void init () {...}
@Inject
private EntityManager em;
@Inject @Paper(SECOND_HAND)
private Event<Book> bookEvent;
@PostConstruct
private void init () {...}
@Inject
private EntityManager em;
@Inject @Paper(SECOND_HAND)
private Event<Book> bookEvent;
@POST
@Consumes(MediaType.APPLICATION_XML)
public Book createBook(Book book) {
bookEvent.fire(book);
em.persist(book);
return book;
Do Not Place Anything
} in This Space
} (Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Transactional JSF Backing Bean
@Transactional
@Named
public class BookService {
@Inject
private EntityManager em;
@Inject @Paper(SECOND_HAND)
private Event<Book> bookEvent;
@Inject
private EntityManager em;
@Inject @Paper(SECOND_HAND)
private Event<Book> bookEvent;
Object-relational mapping
No SQL manipulation
Different lifecycles
Exists
Managed
Method invocations
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Life Cycle of a JPA Entity
Transient
@Id @GeneratedValue
private Long id;
private String firstName;
//...
}
@Inject
private Logger logger;
@Id @GeneratedValue
private Long id;
private String firstName;
//...
}
@Id @GeneratedValue
private Long id;
private String firstName;
//...
}
@Inject
private Logger logger;
@PrePersist
private void prePersist(Object object) {
logger.debug("Object persisted {})", object);
}
}
Constrain our model
Is the address valid?
Is the email well formed?
Is the customer's name null?
Is the birthday in the past?
Ensure data is valid
Annotations
Validators
CDI beans
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Validators are CDI Beans
public class NotNullValidator implements
ConstraintValidator<NotNull, Object> {
@Inject
private Logger logger;
@Override
public void initialize(NotNull parameters) { }
@Override
public boolean isValid(Object object,
ConstraintValidatorContext context) {
logger.debug("Null object {})", object);
return object != null;
}
}
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Injecting a Validator
public class BookService {
@Inject
private Validador validator;
Set<ConstraintViolation<Book>> violations;
violations = validator.validate(book);
if (violations.size() > 0)
throw new ConstraintViolationException(violations);
}
}
Introduction
Understanding Context & Dependency Injection
Dependency Injection
Dependency injection
Bridge (with alternatives)
Interceptor
Decorator
Tool box
Ease business applications development
Enrich technical frameworks
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
References
JSR 346
http://jcp.org/en/jsr/detail?id=346
Java EE 7 specification
JSR 342
http://jcp.org/en/jsr/detail?id=342
Pluralsight courses
Bean Validation
Java Persistence API (JPA)
Antonio Goncalves
www.antoniogoncalves.org
@agoncal