Successfully reported this slideshow.
Your SlideShare is downloading. ×

CDI e as ideias pro futuro do VRaptor

Loading in …3
×

Check these out next

1 of 42 Ad
1 of 42 Ad
Advertisement

More Related Content

Advertisement

More from Caelum (20)

Advertisement

CDI e as ideias pro futuro do VRaptor

  1. 1. CDI
  2. 2. V4?
  3. 3. controllers
  4. 4. @Resource V3 public class ProductController { private final ProductDAO dao; private final Result result; public ProductController(ProductDAO dao, Result result) { this.dao = dao; this.result = result; } public void add(Product product) { dao.save(product); result.include("message", "Product saved!"); } }
  5. 5. @Controller V4 public class ProductController { private final ProductDAO dao; private final Result result; CDI @Inject public ProductController(ProductDAO dao, Result result) { this.dao = dao; this.result = result; } public void add(Product product) { dao.save(product); result.include("message", "Product saved!"); } }
  6. 6. @Controller CDI public class ProductController { @Inject private ProductDAO dao; @Inject private Result result; public void add(Product product) { dao.save(product); result.include("message", "Product saved!"); } }
  7. 7. V4 package br.com.caelum.example.controller; public class ProductController { private ProductDAO dao; public void add(Product product) { dao.save(product); } }
  8. 8. V4 package br.com.caelum.example.controller; public class ProductController { private ProductDAO dao; public void index() { /product .... } public void add(Product product) { dao.save(product); } }
  9. 9. V4 package br.com.caelum.example.controller; public class RootController { private ProductDAO dao; public void list() { /list ... } public void index() { / ... } }
  10. 10. CDI V4 @Controller package br.com.caelum.example.customPackage;
  11. 11. components
  12. 12. V3 @Component public class ProdutoDao { private final EntityManager entityManager; public ProductDao(EntityManager entityManager) { this.entityManager = entityManager; } public void save(Product product) { entityManager.persist(product); } }
  13. 13. CDI public class ProdutoDao { @Inject private EntityManager entityManager; public void save(Product product) { entityManager.persist(product); } }
  14. 14. component factories
  15. 15. @Component @RequestScoped public class EntityManagerCreator implements ComponentFactory<EntityManager> { private final EntityManagerFactory factory; private EntityManager entityManager; public EntityManagerCreator(EntityManagerFactory factory) { this.factory = factory; entityManager = factory.createEntityManager(); } public EntityManager getInstance() { return entityManager; } @PreDestroy public void destroy() { V3 entityManager.close(); } }
  16. 16. CDI public class EntityManagerProducer { public EntityManager create(EntityManagerFactory factory) { return factory.createEntityManager(); } public void close(@Disposes EntityManager entityManager) { entityManager.close(); } }
  17. 17. CDI public class EntityManagerProducer { @Produces @RequestScoped public EntityManager create(EntityManagerFactory factory) { return factory.createEntityManager(); } public void close(@Disposes EntityManager entityManager) { entityManager.close(); } }
  18. 18. CDI public class EntityManagerProducer { @Produces @RequestScoped public EntityManager create(EntityManagerFactory factory) { return factory.createEntityManager(); } public void close(@Disposes EntityManager entityManager) { entityManager.close(); } }
  19. 19. interceptors
  20. 20. V3 @Intercepts @RequestScoped public class HelloInterceptor implements Interceptor {     public boolean accepts(ResourceMethod method) {         return true;     }     public void intercept(InterceptorStack stack, ResourceMethod method, Object instance) {         System.out.println("Hello Interceptor ");         stack.next(method, instance);     } }
  21. 21. V4 public class HelloInterceptor implements ControllerInterceptor { public void intercept(InterceptorStack stack) { System.out.println("Hello Interceptor"); stack.next(); } }
  22. 22. public class LogUri implements ControllerInterceptor { V4 @Inject private HttpServletRequest request; CDI public void intercept(InterceptorStack stack) { System.out.println("URI "+ request.getRequestURI()); stack.next(); } }
  23. 23. public class LogUri implements ControllerInterceptor { V4 @Inject @RequestAttribute private String requestUri; CDI public void intercept(InterceptorStack stack) { System.out.println("URI "+ requestUri); stack.next(); } }
  24. 24. public class LogUri implements SpecificControllerInterceptor { @Inject @RequestAttribute private String requestUri; @Inject private ControllerMethod method; public void intercept(InterceptorStack stack) { System.out.println("URI "+ requestUri); stack.next(); } public boolean accepts() { return method.getName().startsWith("log"); } } V4
  25. 25. @Before(OtherInterceptor.class) public class LogUri implements SpecificControllerInterceptor { @Inject @RequestAttribute private String requestUri; @Inject private ControllerMethod method; public void intercept(InterceptorStack stack) { System.out.println("URI "+ requestUri); stack.next(); } public boolean accepts() { return method.getName().startsWith("log"); } } V4
  26. 26. events
  27. 27. V3 @Intercepts(before=ExecuteMethodInterceptor.class) @RequestScoped public class BeforeInvocation implements Interceptor {     public boolean accepts(ResourceMethod method) {         return true;     }     public void intercept(InterceptorStack stack, ResourceMethod method, Object instance) {         System.out.println("Antes da invocação!!");         stack.next(method, instance);     } }
  28. 28. V4 public class BeforeInvocation { CDI public void do(@Observes BeforeMethodExecutionEvent evt) { System.out.println("Antes da invocação!!"); } }
  29. 29. V4 public void do(@Observes AfterValidationEvent evt) { ... CDI } public void do(@Observes BeforeRedirectToViewEvent evt) { ... } public void do(@Observes BeforeAnythingEvent evt) { ... } .....
  30. 30. V3 @Component @ApplicationScoped public class CustomRoutes implements RoutesConfiguration {   public void config(Router router) {      new Rules(router) {          public void routes() { routeFor("/clist").is(ClienteController.class).list();          }      };   } }
  31. 31. CDI V4 public class AppRoutesExtensions { public void register(@Observes CustomRoutesBuilder route) { route.of("/clist").is(ClienteController.class).list(); } }
  32. 32. CDI V4 public class AppRoutesExtensions { public void register(@Observes CustomRoutesBuilder route) { route.of("/clist").is(ClienteController.class).list(); } } public class PluginExtensions { public void register(@Observes CustomRoutesBuilder route) { route.of("/_plugin").is(MyPlugin.class).plug(); } }
  33. 33. decorators
  34. 34. V3 @Component public class CustomPathResolver extends DefaultPathResolver {         @Override     protected String getPrefix() {         return "/pasta/raiz/";     } }
  35. 35. CDI @Decorator public class CustomPathResolver implements PathResolver {         @Inject @Delegate private PathResolver resolver;     public String getPrefix() {         return "/pasta/raiz/";     } // delegação public String getExtension() {         return resolver.getExtension();     }     public String extractControllerFromName(String baseName) { return resolver.extractControllerFromName(baseName); } }
  36. 36. view helpers
  37. 37. CDI @Named public class Global { public String getAsa() { return "Valor ultra-interessante pra view"; } }
  38. 38. CDI @Named public class Global { public String getAsa() { return "Valor ultra-interessante pra view"; } } <p>Valor: ${global.asa}</p>
  39. 39. @Named CDI @SessionScoped public class LoggedUser { private User user; ... public String getUsername() { return user.getUsername(); } } <p>Usuário Logado: ${loggedUser.username}</p>
  40. 40. E mais: qualifiers CDI stereotypes interceptors (cdi) custom scopes proxies e Instance portable extensions classpath scanning
  41. 41. V4 baseado no cdi compatível com vraptor3 integração com spring ainda ideias, nada codado aberto pra ideias!
  42. 42. V4 Responda a pesquisa sobre o futuro do VRaptor e dê sua opinião! http://bit.ly/HWTKmO @sergio_caelum http://groups.google.com/group/ @paulo_caelum caelum-vraptor-dev @caelum

Editor's Notes

  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

×