Conhecendo CDI
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
925
On Slideshare
912
From Embeds
13
Number of Embeds
1

Actions

Shares
Downloads
18
Comments
0
Likes
4

Embeds 13

http://danielsoro.com.br 13

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Conhecendo CDI CDI 1.1 - JSR 346 Daniel Cunha (soro) danielsoro@gmail.com http://www.danielsoro.com.br
  • 2. Daniel Cunha soro - irc.freenode.net http://danielsoro.com.br http://github.com/danielsoro http://twitter.com/dvlc_ danielsoro@gmail.com
  • 3. O que é e pra que serve o CDI?
  • 4. Iniciando com CDI
  • 5. beans.xml <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http: //xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.1" bean-discovery-mode="all"> </beans>
  • 6. Injetando suas dependências
  • 7. @
  • 8. Inject
  • 9. E quando eu tenho mais de uma implementação?
  • 10. Qualifiers
  • 11. Alternatives
  • 12. Alternatives <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http: //xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.1" bean-discovery-mode="all"> <alternatives> <class>package.RelaxedItemValdidator</class> </alternatives> </beans>
  • 13. @
  • 14. Priority Interceptor.Priority.PLATFORM_BEFORE = 0 Interceptor.Priority.LIBRARY_BEFORE = 1000 Interceptor.Priority.APPLICATION = 2000 Interceptor.Priority.LIBRARY_AFTER = 3000 Interceptor.Priority.PLATFORM_AFTER = 4000
  • 15. E quando eu quiser dar nome ao meu bean?
  • 16. @Inject @Named("user.name") private String userName;
  • 17. E quando eu quiser evoluir o meu bean?
  • 18. @Specializes public class ClasseFilha extends ClasseMae {}
  • 19. @Specializes @Cejug public class ClasseFilha extends ClasseMae {}
  • 20. @RequestScope @Ivia @Named(“mae”) public class ClasseMae @Specializes @Cejug public class ClasseFilha extends ClasseMae {}
  • 21. @RequestScope @Ivia @Named(“mae”) public class ClasseMae @Specializes @Cejug @Named(“filha”) public class ClasseFilha extends ClasseMae {}
  • 22. Tenho outra forma de evitar ambiguidade?
  • 23. @Typed({Cejug.class,GUJavaSC.class}) public class BrazilJugs extends Jugs {}
  • 24. E quando eu quiser injetar uma classe que não é um bean CDI?
  • 25. import javax.enterprise.inject.Produces; public class Produtor { @Produces public Logger criarLogger(){ return LoggerFactory.getLogger(ConhecendoCDI.class); } }
  • 26. E quando eu quiser finalizar esse bean?
  • 27. import javax.enterprise.inject.Disposes; public class Produtor { @Produces @CejugDB public EntityManager criaEntityManager(){ EntityManagerFactory emf = Persistence. createEntityManagerFactory("cejugdb"); return emf.createEntityManager(); } public void fechaEntityManager(@Dispose @CejugDB EntityManager em){ em.close(); } }
  • 28. Criações mais flexiveis
  • 29. import javax.enterprise.inject.Produces; public class Produtor { @Produces public Logger criarLogger(InjectPoint injectPoint){ return LoggerFactory.getLogger(injectPoint.getMember() .getDeclaringClass().getName()); } }
  • 30. Escopos CDI
  • 31. @RequestScoped @SessionScoped @ApplicationScoped @ConversationScoped @Dependent
  • 32. Quero interceptar.. Como eu faço?
  • 33. @Interceptor @Priority(Interceptor.Priority. APPLICATION) public class Auditor {}
  • 34. Posso escolher quem interceptar?
  • 35. @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) @InterceptorBinding public @interface Auditavel {}
  • 36. @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) @Inherited @InterceptorBinding public @interface Auditavel {}
  • 37. Interceptando o ciclo de vida
  • 38. @AroundConstruct @PostConstruct @PreDestroy
  • 39. Decorando o nosso bean
  • 40. @Decorator public abstract class CoderDecorator implements Coder { @Inject @Delegate @Any Coder coder; public String codeString(String s, int tval) { int len = s.length(); return coder.codeString(s, tval); } }
  • 41. Decorator <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http: //xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.1" bean-discovery-mode="all"> <decorators> <class>decorators.CoderDecorator</class> </decorators> </beans>
  • 42. Preciso replicar as 30 annotation da classe X na Y… e agora?
  • 43. @Stereotype
  • 44. import javax.enterprise.inject.Stereotype; @Stereotype @Named @RequestScoped @Auditavel @Rastreavel @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface CejugStereotype { }
  • 45. Diminuindo acoplamento
  • 46. @Inject private Event<Palestrante> eventoCejug;
  • 47. eventoCejug.fire(palestrante);
  • 48. public void adicionarPalestrante(@Observes Palestrante palestrante){}
  • 49. E eu posso fazer chamadas assíncronas?
  • 50. @Asynchronous public void adicionarPalestrante(@Observes Palestrante palestrante){}
  • 51. Dúvidas?
  • 52. Obrigado
  • 53. Conhecendo CDI CDI 1.1 - JSR 346 Daniel Cunha (soro) danielsoro@gmail.com http://www.danielsoro.com.br