Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
CDI
V4?
controllers
@Resource                                               V3public class ProductController {	   private final ProductDAO dao...
@Controller                                             V4public class ProductController {	   private final ProductDAO dao...
@Controller                                          CDIpublic class ProductController {	   @Inject private ProductDAO dao...
V4package br.com.caelum.example.controller;public class ProductController {	   private ProductDAO dao;		   public void add...
V4package br.com.caelum.example.controller;public class ProductController {	   private ProductDAO dao;	   public void inde...
V4package br.com.caelum.example.controller;public class RootController {	   private ProductDAO dao;		   public void list()...
CDI                                               V4@Controllerpackage br.com.caelum.example.customPackage;
components
V3@Componentpublic class ProdutoDao {	   private final EntityManager entityManager;	    public ProductDao(EntityManager en...
CDIpublic class ProdutoDao {	   @Inject private EntityManager entityManager;		   public void save(Product product) {      ...
component factories
@Component@RequestScopedpublic class EntityManagerCreator implements                               ComponentFactory<Entity...
CDIpublic class EntityManagerProducer {	   public EntityManager create(EntityManagerFactory factory) {	   	 return factory...
CDIpublic class EntityManagerProducer {	   @Produces @RequestScoped	   public EntityManager create(EntityManagerFactory fa...
CDIpublic class EntityManagerProducer {	   @Produces @RequestScoped	   public EntityManager create(EntityManagerFactory fa...
interceptors
V3@Intercepts@RequestScopedpublic class HelloInterceptor implements Interceptor {    public boolean accepts(ResourceMethod...
V4public class HelloInterceptor implements ControllerInterceptor {	   public void intercept(InterceptorStack stack) {	   	...
public class LogUri implements ControllerInterceptor {                                                         V4    @Inje...
public class LogUri implements ControllerInterceptor {                                                           V4    @In...
public class LogUri implements SpecificControllerInterceptor {    @Inject @RequestAttribute private String requestUri;    ...
@Before(OtherInterceptor.class)public class LogUri implements SpecificControllerInterceptor {    @Inject @RequestAttribute...
events
V3@Intercepts(before=ExecuteMethodInterceptor.class)@RequestScopedpublic class BeforeInvocation implements Interceptor {  ...
V4public class BeforeInvocation {                                                        CDI	   public void do(@Observes B...
V4public void do(@Observes AfterValidationEvent evt) {	 ...                                                  CDI}public vo...
V3@Component@ApplicationScopedpublic class CustomRoutes implements RoutesConfiguration {  public void config(Router router...
CDI        V4public class AppRoutesExtensions {	   public void register(@Observes CustomRoutesBuilder route) {        rout...
CDI        V4public class AppRoutesExtensions {	   public void register(@Observes CustomRoutesBuilder route) {        rout...
decorators
V3@Componentpublic class CustomPathResolver extends DefaultPathResolver {       @Override    protected String getPrefix() ...
CDI@Decoratorpublic class CustomPathResolver implements PathResolver {       @Inject @Delegate private PathResolver resolv...
view helpers
CDI@Namedpublic class Global {	   public String getAsa() {	   	 return "Valor ultra-interessante pra view";	   }}
CDI@Namedpublic class Global {	   public String getAsa() {	   	 return "Valor ultra-interessante pra view";	   }}<p>Valor:...
@Named                                                CDI@SessionScopedpublic class LoggedUser {    private User user;    ...
E mais:                qualifiers       CDI              stereotypes           interceptors (cdi)             custom scopes...
V4     baseado no cdicompatível com vraptor3 integração com springainda ideias, nada codado    aberto pra ideias!
V4           Responda a pesquisa sobre o            futuro do VRaptor e dê sua                      opinião!              ...
Upcoming SlideShare
Loading in …5
×

CDI e as ideias pro futuro do VRaptor

4,069 views

Published on

Palestra com ideias sobre o futuro do VRaptor ser baseado em CDI

Published in: Technology, Sports
  • Be the first to comment

CDI e as ideias pro futuro do VRaptor

  1. 1. CDI
  2. 2. V4?
  3. 3. controllers
  4. 4. @Resource V3public 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 V4public 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 CDIpublic 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. V4package br.com.caelum.example.controller;public class ProductController { private ProductDAO dao; public void add(Product product) { dao.save(product); }}
  8. 8. V4package 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. V4package br.com.caelum.example.controller;public class RootController { private ProductDAO dao; public void list() { /list ... } public void index() { / ... }}
  10. 10. CDI V4@Controllerpackage br.com.caelum.example.customPackage;
  11. 11. components
  12. 12. V3@Componentpublic class ProdutoDao { private final EntityManager entityManager; public ProductDao(EntityManager entityManager) { this.entityManager = entityManager; } public void save(Product product) { entityManager.persist(product); }}
  13. 13. CDIpublic class ProdutoDao { @Inject private EntityManager entityManager; public void save(Product product) { entityManager.persist(product); }}
  14. 14. component factories
  15. 15. @Component@RequestScopedpublic 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. CDIpublic class EntityManagerProducer { public EntityManager create(EntityManagerFactory factory) { return factory.createEntityManager(); } public void close(@Disposes EntityManager entityManager) { entityManager.close(); }}
  17. 17. CDIpublic class EntityManagerProducer { @Produces @RequestScoped public EntityManager create(EntityManagerFactory factory) { return factory.createEntityManager(); } public void close(@Disposes EntityManager entityManager) { entityManager.close(); }}
  18. 18. CDIpublic 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@RequestScopedpublic 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. V4public 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)@RequestScopedpublic 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. V4public class BeforeInvocation { CDI public void do(@Observes BeforeMethodExecutionEvent evt) { System.out.println("Antes da invocação!!"); }}
  29. 29. V4public void do(@Observes AfterValidationEvent evt) { ... CDI}public void do(@Observes BeforeRedirectToViewEvent evt) { ...}public void do(@Observes BeforeAnythingEvent evt) { ...}.....
  30. 30. V3@Component@ApplicationScopedpublic class CustomRoutes implements RoutesConfiguration {  public void config(Router router) {     new Rules(router) {         public void routes() { routeFor("/clist").is(ClienteController.class).list();         }     };  }}
  31. 31. CDI V4public class AppRoutesExtensions { public void register(@Observes CustomRoutesBuilder route) { route.of("/clist").is(ClienteController.class).list(); }}
  32. 32. CDI V4public 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@Componentpublic class CustomPathResolver extends DefaultPathResolver {       @Override    protected String getPrefix() {        return "/pasta/raiz/";    }}
  35. 35. CDI@Decoratorpublic 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@Namedpublic class Global { public String getAsa() { return "Valor ultra-interessante pra view"; }}
  38. 38. CDI@Namedpublic class Global { public String getAsa() { return "Valor ultra-interessante pra view"; }}<p>Valor: ${global.asa}</p>
  39. 39. @Named CDI@SessionScopedpublic 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 cdicompatível com vraptor3 integração com springainda 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

×