Google Guice und Apache MyFaces 1.2 Matthias Weßendorf | matzew@apache.org
Matthias Weßendorf <ul><li>Oracle Corp. </li></ul><ul><ul><li>ADFv team (ADF Faces) </li></ul></ul><ul><li>Apache Software...
Agenda <ul><li>Dependency Injection </li></ul><ul><li>Google Guice </li></ul><ul><li>DI mit JSF </li></ul><ul><li>Integrat...
Dependency Injection - I <ul><li>Entwurfsmuster  </li></ul><ul><ul><li>Delegiert Erzeugung von Objekten </li></ul></ul><ul...
Dependency Injection - II <ul><li>DI von Hand: </li></ul><ul><ul><li>public class ClientComponent { </li></ul></ul><ul><ul...
Dependency Injection - III <ul><li>DI mit Framework (Spring): </li></ul><ul><li><bean class=&quot; de.jax.DefaultServiceIm...
Dependency Injection - IV <ul><li>Java </li></ul><ul><ul><li>Spring </li></ul></ul><ul><ul><li>Pico </li></ul></ul><ul><ul...
Google Guice - I <ul><li>lightweight DI Container (Java >= 5) </li></ul><ul><li>Java statt XML </li></ul><ul><ul><li>Modul...
Google Guice - II <ul><li>public class ClientComponent </li></ul><ul><li>{ </li></ul><ul><li>@com.google.inject.Inject </l...
Google Guice - III <ul><li>public class Module extends  com.google.inject.AbstractModule </li></ul><ul><li>{ </li></ul><ul...
Google Guice - IV <ul><li>... </li></ul><ul><li>Injector injector = Guice.createInjector( new Module(), ... ); </li></ul><...
DI mit JSF - I <ul><li>public class BackingBean </li></ul><ul><li>{ </li></ul><ul><li>... </li></ul><ul><li>private   Serv...
DI mit JSF - II <ul><li>... </li></ul><ul><li><managed-bean> </li></ul><ul><li><managed-bean-name> serviceImpl </managed-b...
DI mit JSF - III <ul><li>... </li></ul><ul><li><managed-bean> </li></ul><ul><li><managed-bean-name>backingBean</managed-be...
Guice und Apache MyFaces - I <ul><li>GuiceResolver (j avax.el.ELResolver ) </li></ul><ul><li>Seit Apache MyFaces 1.2.2 </l...
Guice und Apache MyFaces - II <ul><li>public class GuiceServletContextListener implements ServletContextListener </li></ul...
Guice und Apache MyFaces - III <ul><li>public class BackingBean </li></ul><ul><li>{ </li></ul><ul><li>@Inject </li></ul><u...
Ausblick I <ul><li>JBoss Seam </li></ul><ul><ul><li>Komponentenmodell für JSF und EJB3 </li></ul></ul><ul><li>JSR 299 WebB...
Ausblick II <ul><li>DI mit WebBeans </li></ul><ul><li>package de.jax; </li></ul><ul><li>import javax.webbeans.*; </li></ul...
Q&A <ul><li>Fragen ? </li></ul><ul><ul><li>Danke! </li></ul></ul><ul><ul><li>Blog: </li></ul></ul><ul><ul><li>http://matth...
Upcoming SlideShare
Loading in...5
×

MyFaces und Google Guice

2,640

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,640
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "MyFaces und Google Guice"

  1. 1. Google Guice und Apache MyFaces 1.2 Matthias Weßendorf | matzew@apache.org
  2. 2. Matthias Weßendorf <ul><li>Oracle Corp. </li></ul><ul><ul><li>ADFv team (ADF Faces) </li></ul></ul><ul><li>Apache Software Foundation </li></ul><ul><ul><li>Apache MyFaces (Trinidad | Core) </li></ul></ul><ul><ul><li>Apache Shale </li></ul></ul><ul><li>Autor </li></ul><ul><ul><li>Java- & Eclipse-Magazin </li></ul></ul><ul><ul><li>Bücher zu Struts, J2ME&WebServices </li></ul></ul><ul><li>Speaker </li></ul><ul><ul><li>Oracle Open World </li></ul></ul><ul><ul><li>JavaOne </li></ul></ul><ul><ul><li>JAX, W-JAX </li></ul></ul><ul><ul><li>ApacheCon </li></ul></ul>
  3. 3. Agenda <ul><li>Dependency Injection </li></ul><ul><li>Google Guice </li></ul><ul><li>DI mit JSF </li></ul><ul><li>Integration </li></ul><ul><ul><li>JSF und Google Guice </li></ul></ul><ul><li>Ausblick </li></ul>
  4. 4. Dependency Injection - I <ul><li>Entwurfsmuster </li></ul><ul><ul><li>Delegiert Erzeugung von Objekten </li></ul></ul><ul><ul><ul><li>Service myService = new DefaultServiceImpl(); </li></ul></ul></ul><ul><li>Vermeidet unnötige Abhängigkeiten </li></ul><ul><ul><li> DefaultServiceImpl </li></ul></ul><ul><li>Vereinfacht das Testen </li></ul><ul><ul><li> MockServiceImpl </li></ul></ul>
  5. 5. Dependency Injection - II <ul><li>DI von Hand: </li></ul><ul><ul><li>public class ClientComponent { </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li>public void setService(Service service) { </li></ul></ul><ul><ul><li>this.service = service; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public void doSomething() { </li></ul></ul><ul><ul><li>service.doSomething(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li>ClientComponent comp = new ClientComponent(); </li></ul></ul><ul><ul><li>comp.setService( new DefaultServiceImpl() ); </li></ul></ul>
  6. 6. Dependency Injection - III <ul><li>DI mit Framework (Spring): </li></ul><ul><li><bean class=&quot; de.jax.DefaultServiceImpl &quot; id=&quot; serviceImpl &quot; /> </li></ul><ul><li><bean class=&quot;ClientComponent&quot; id=&quot;client&quot; p: service -ref=&quot; serviceImpl &quot;/> </li></ul><ul><ul><li>oder via Annotations (2.5) </li></ul></ul>
  7. 7. Dependency Injection - IV <ul><li>Java </li></ul><ul><ul><li>Spring </li></ul></ul><ul><ul><li>Pico </li></ul></ul><ul><ul><li>JavaServer Faces </li></ul></ul><ul><li>Ruby </li></ul><ul><ul><li>Needle </li></ul></ul><ul><ul><li>Copland </li></ul></ul><ul><li>.NET </li></ul><ul><ul><li>Spring.NET </li></ul></ul><ul><li>add your language... </li></ul>
  8. 8. Google Guice - I <ul><li>lightweight DI Container (Java >= 5) </li></ul><ul><li>Java statt XML </li></ul><ul><ul><li>Module Klassen  Konfiguration </li></ul></ul><ul><ul><li>Annotations </li></ul></ul><ul><li>@Inject == the new new ... </li></ul><ul><li>Lead „crazy“ Bob Lee (Google) </li></ul>
  9. 9. Google Guice - II <ul><li>public class ClientComponent </li></ul><ul><li>{ </li></ul><ul><li>@com.google.inject.Inject </li></ul><ul><li>private Service service; </li></ul><ul><li>public void doSomething() </li></ul><ul><li>{ </li></ul><ul><li>service .doSomething(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  10. 10. Google Guice - III <ul><li>public class Module extends com.google.inject.AbstractModule </li></ul><ul><li>{ </li></ul><ul><li>@Override </li></ul><ul><li>protected void configure() </li></ul><ul><li>{ </li></ul><ul><li>bind( Service.class ).to( DefaultServiceImpl.class ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  11. 11. Google Guice - IV <ul><li>... </li></ul><ul><li>Injector injector = Guice.createInjector( new Module(), ... ); </li></ul><ul><li>ClientComponent cc = injector.getInstance( ClientComponent.class ); </li></ul><ul><li>cc .doSomething(); </li></ul><ul><li>... </li></ul><ul><li>Ab Java 6: </li></ul><ul><ul><li>ServiceLoader<com.google.inject.Module> modules = ServiceLoader.load(com.google.inject.Module.class); </li></ul></ul><ul><ul><li>Injector injector = Guice.createInjector(modules); </li></ul></ul>
  12. 12. DI mit JSF - I <ul><li>public class BackingBean </li></ul><ul><li>{ </li></ul><ul><li>... </li></ul><ul><li>private Service service ; </li></ul><ul><li>//get und set Methode... </li></ul><ul><li>... </li></ul><ul><li>public String actionMethod() </li></ul><ul><li>{ </li></ul><ul><li>... </li></ul><ul><li>getService().doSomething(); </li></ul><ul><li>... </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  13. 13. DI mit JSF - II <ul><li>... </li></ul><ul><li><managed-bean> </li></ul><ul><li><managed-bean-name> serviceImpl </managed-bean-name> </li></ul><ul><li><managed-bean-class> </li></ul><ul><li>de.jax.DefaultServiceImpl </li></ul><ul><li></managed-bean-class> </li></ul><ul><li><managed-bean-scope> none </managed-bean-scope> </li></ul><ul><li></managed-bean> </li></ul><ul><li>... </li></ul>
  14. 14. DI mit JSF - III <ul><li>... </li></ul><ul><li><managed-bean> </li></ul><ul><li><managed-bean-name>backingBean</managed-bean-name> </li></ul><ul><li><managed-bean-class>de.jax.BackingBean</managed-bean-class> </li></ul><ul><li><managed-bean-scope>request</managed-bean-scope> </li></ul><ul><li><managed-property> </li></ul><ul><li><property-name> service </property-name> </li></ul><ul><li><property-class> de.jax.Service </property-class> </li></ul><ul><li><value> #{serviceImpl} </value> </li></ul><ul><li></managed-property> </li></ul><ul><li></managed-bean> </li></ul><ul><li>... </li></ul>
  15. 15. Guice und Apache MyFaces - I <ul><li>GuiceResolver (j avax.el.ELResolver ) </li></ul><ul><li>Seit Apache MyFaces 1.2.2 </li></ul><ul><ul><li>MyFaces feature... </li></ul></ul><ul><li>Nicht automatisch verfügbar... </li></ul><ul><li>Eintragen in faces-config.xml </li></ul><ul><li><application> </li></ul><ul><li><el-resolver> </li></ul><ul><li>org.apache.myfaces.el.unified.resolver.GuiceResolver </li></ul><ul><li></el-resolver> </li></ul><ul><li></application> </li></ul><ul><li>und ServeltContextListener... </li></ul>
  16. 16. Guice und Apache MyFaces - II <ul><li>public class GuiceServletContextListener implements ServletContextListener </li></ul><ul><li>{ </li></ul><ul><li>public void contextInitialized(ServletContextEvent event) </li></ul><ul><li>{ </li></ul><ul><li>ServletContext ctx = event.getServletContext(); </li></ul><ul><li>ServiceLoader<com.google.inject.Module> modules ... </li></ul><ul><li>Injector injector = Guice.createInjector( modules ); </li></ul><ul><li>ctx.setAttribute( GuiceResolver .KEY, injector); </li></ul><ul><li>} </li></ul><ul><li>public void contextDestroyed(ServletContextEvent event) </li></ul><ul><li>{ </li></ul><ul><li>ServletContext ctx = event.getServletContext(); </li></ul><ul><li>ctx.removeAttribute(GuiceResolver.KEY); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  17. 17. Guice und Apache MyFaces - III <ul><li>public class BackingBean </li></ul><ul><li>{ </li></ul><ul><li>@Inject </li></ul><ul><li>private Service service ; </li></ul><ul><li>... </li></ul><ul><li>public String actionMethod() </li></ul><ul><li>{ </li></ul><ul><li>... </li></ul><ul><li>service .doSomething(); </li></ul><ul><li>... </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  18. 18. Ausblick I <ul><li>JBoss Seam </li></ul><ul><ul><li>Komponentenmodell für JSF und EJB3 </li></ul></ul><ul><li>JSR 299 WebBeans </li></ul><ul><ul><li>Standard für DI... (Ideen von Guice) </li></ul></ul><ul><ul><li>„ Standard“ für JBoss Seam </li></ul></ul><ul><li>The Spring Framework </li></ul><ul><ul><li>Ab 2.5  Java für Konfiguration ebenfalls möglich </li></ul></ul>
  19. 19. Ausblick II <ul><li>DI mit WebBeans </li></ul><ul><li>package de.jax; </li></ul><ul><li>import javax.webbeans.*; </li></ul><ul><li>@Component </li></ul><ul><li>@RequestScoped </li></ul><ul><li>@Named(&quot;bean&quot;) </li></ul><ul><li>public class Bean </li></ul><ul><li>{ </li></ul><ul><li>... </li></ul><ul><li>@In Service service; </li></ul><ul><li>... </li></ul><ul><li>} </li></ul>
  20. 20. Q&A <ul><li>Fragen ? </li></ul><ul><ul><li>Danke! </li></ul></ul><ul><ul><li>Blog: </li></ul></ul><ul><ul><li>http://matthiaswessendorf.wordpress.com </li></ul></ul><ul><ul><li>Email: </li></ul></ul><ul><ul><li>matzew AT apache DOT org </li></ul></ul>

×