advertisement

MyFaces und Google Guice

50 %
50 %
advertisement
Information about MyFaces und Google Guice

Published on April 26, 2008

Author: mwessendorf

Source: slideshare.net

advertisement

Google Guice und Apache MyFaces 1.2 Matthias Weßendorf | matzew@apache.org

Matthias Weßendorf Oracle Corp. ADFv team (ADF Faces) Apache Software Foundation Apache MyFaces (Trinidad | Core) Apache Shale Autor Java- & Eclipse-Magazin Bücher zu Struts, J2ME&WebServices Speaker Oracle Open World JavaOne JAX, W-JAX ApacheCon

Oracle Corp.

ADFv team (ADF Faces)

Apache Software Foundation

Apache MyFaces (Trinidad | Core)

Apache Shale

Autor

Java- & Eclipse-Magazin

Bücher zu Struts, J2ME&WebServices

Speaker

Oracle Open World

JavaOne

JAX, W-JAX

ApacheCon

Agenda Dependency Injection Google Guice DI mit JSF Integration JSF und Google Guice Ausblick

Dependency Injection

Google Guice

DI mit JSF

Integration

JSF und Google Guice

Ausblick

Dependency Injection - I Entwurfsmuster Delegiert Erzeugung von Objekten Service myService = new DefaultServiceImpl(); Vermeidet unnötige Abhängigkeiten  DefaultServiceImpl Vereinfacht das Testen  MockServiceImpl

Entwurfsmuster

Delegiert Erzeugung von Objekten

Service myService = new DefaultServiceImpl();

Vermeidet unnötige Abhängigkeiten

 DefaultServiceImpl

Vereinfacht das Testen

 MockServiceImpl

Dependency Injection - II DI von Hand: public class ClientComponent { ... public void setService(Service service) { this.service = service; } public void doSomething() { service.doSomething(); } } ... ClientComponent comp = new ClientComponent(); comp.setService( new DefaultServiceImpl() );

DI von Hand:

public class ClientComponent {

...

public void setService(Service service) {

this.service = service;

}

public void doSomething() {

service.doSomething();

}

}

...

ClientComponent comp = new ClientComponent();

comp.setService( new DefaultServiceImpl() );

Dependency Injection - III DI mit Framework (Spring): <bean class=&quot; de.jax.DefaultServiceImpl &quot; id=&quot; serviceImpl &quot; /> <bean class=&quot;ClientComponent&quot; id=&quot;client&quot; p: service -ref=&quot; serviceImpl &quot;/> oder via Annotations (2.5)

DI mit Framework (Spring):

<bean class=&quot; de.jax.DefaultServiceImpl &quot; id=&quot; serviceImpl &quot; />

<bean class=&quot;ClientComponent&quot; id=&quot;client&quot; p: service -ref=&quot; serviceImpl &quot;/>

oder via Annotations (2.5)

Dependency Injection - IV Java Spring Pico JavaServer Faces Ruby Needle Copland .NET Spring.NET add your language...

Java

Spring

Pico

JavaServer Faces

Ruby

Needle

Copland

.NET

Spring.NET

add your language...

Google Guice - I lightweight DI Container (Java >= 5) Java statt XML Module Klassen  Konfiguration Annotations @Inject == the new new ... Lead „crazy“ Bob Lee (Google)

lightweight DI Container (Java >= 5)

Java statt XML

Module Klassen  Konfiguration

Annotations

@Inject == the new new ...

Lead „crazy“ Bob Lee (Google)

Google Guice - II public class ClientComponent { @com.google.inject.Inject private Service service; public void doSomething() { service .doSomething(); } }

public class ClientComponent

{

@com.google.inject.Inject

private Service service;

public void doSomething()

{

service .doSomething();

}

}

Google Guice - III public class Module extends com.google.inject.AbstractModule { @Override protected void configure() { bind( Service.class ).to( DefaultServiceImpl.class ); } }

public class Module extends com.google.inject.AbstractModule

{

@Override

protected void configure()

{

bind( Service.class ).to( DefaultServiceImpl.class );

}

}

Google Guice - IV ... Injector injector = Guice.createInjector( new Module(), ... ); ClientComponent cc = injector.getInstance( ClientComponent.class ); cc .doSomething(); ... Ab Java 6: ServiceLoader<com.google.inject.Module> modules = ServiceLoader.load(com.google.inject.Module.class); Injector injector = Guice.createInjector(modules);

...

Injector injector = Guice.createInjector( new Module(), ... );

ClientComponent cc = injector.getInstance( ClientComponent.class );

cc .doSomething();

...

Ab Java 6:

ServiceLoader<com.google.inject.Module> modules = ServiceLoader.load(com.google.inject.Module.class);

Injector injector = Guice.createInjector(modules);

DI mit JSF - I public class BackingBean { ... private Service service ; //get und set Methode... ... public String actionMethod() { ... getService().doSomething(); ... } }

public class BackingBean

{

...

private Service service ;

//get und set Methode...

...

public String actionMethod()

{

...

getService().doSomething();

...

}

}

DI mit JSF - II ... <managed-bean> <managed-bean-name> serviceImpl </managed-bean-name> <managed-bean-class> de.jax.DefaultServiceImpl </managed-bean-class> <managed-bean-scope> none </managed-bean-scope> </managed-bean> ...

...

<managed-bean>

<managed-bean-name> serviceImpl </managed-bean-name>

<managed-bean-class>

de.jax.DefaultServiceImpl

</managed-bean-class>

<managed-bean-scope> none </managed-bean-scope>

</managed-bean>

...

DI mit JSF - III ... <managed-bean> <managed-bean-name>backingBean</managed-bean-name> <managed-bean-class>de.jax.BackingBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name> service </property-name> <property-class> de.jax.Service </property-class> <value> #{serviceImpl} </value> </managed-property> </managed-bean> ...

...

<managed-bean>

<managed-bean-name>backingBean</managed-bean-name>

<managed-bean-class>de.jax.BackingBean</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

<managed-property>

<property-name> service </property-name>

<property-class> de.jax.Service </property-class>

<value> #{serviceImpl} </value>

</managed-property>

</managed-bean>

...

Guice und Apache MyFaces - I GuiceResolver (j avax.el.ELResolver ) Seit Apache MyFaces 1.2.2 MyFaces feature... Nicht automatisch verfügbar... Eintragen in faces-config.xml <application> <el-resolver> org.apache.myfaces.el.unified.resolver.GuiceResolver </el-resolver> </application> und ServeltContextListener...

GuiceResolver (j avax.el.ELResolver )

Seit Apache MyFaces 1.2.2

MyFaces feature...

Nicht automatisch verfügbar...

Eintragen in faces-config.xml

<application>

<el-resolver>

org.apache.myfaces.el.unified.resolver.GuiceResolver

</el-resolver>

</application>

und ServeltContextListener...

Guice und Apache MyFaces - II public class GuiceServletContextListener implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { ServletContext ctx = event.getServletContext(); ServiceLoader<com.google.inject.Module> modules ... Injector injector = Guice.createInjector( modules ); ctx.setAttribute( GuiceResolver .KEY, injector); } public void contextDestroyed(ServletContextEvent event) { ServletContext ctx = event.getServletContext(); ctx.removeAttribute(GuiceResolver.KEY); } }

public class GuiceServletContextListener implements ServletContextListener

{

public void contextInitialized(ServletContextEvent event)

{

ServletContext ctx = event.getServletContext();

ServiceLoader<com.google.inject.Module> modules ...

Injector injector = Guice.createInjector( modules );

ctx.setAttribute( GuiceResolver .KEY, injector);

}

public void contextDestroyed(ServletContextEvent event)

{

ServletContext ctx = event.getServletContext();

ctx.removeAttribute(GuiceResolver.KEY);

}

}

Guice und Apache MyFaces - III public class BackingBean { @Inject private Service service ; ... public String actionMethod() { ... service .doSomething(); ... } }

public class BackingBean

{

@Inject

private Service service ;

...

public String actionMethod()

{

...

service .doSomething();

...

}

}

Ausblick I JBoss Seam Komponentenmodell für JSF und EJB3 JSR 299 WebBeans Standard für DI... (Ideen von Guice) „ Standard“ für JBoss Seam The Spring Framework Ab 2.5  Java für Konfiguration ebenfalls möglich

JBoss Seam

Komponentenmodell für JSF und EJB3

JSR 299 WebBeans

Standard für DI... (Ideen von Guice)

„ Standard“ für JBoss Seam

The Spring Framework

Ab 2.5  Java für Konfiguration ebenfalls möglich

Ausblick II DI mit WebBeans package de.jax; import javax.webbeans.*; @Component @RequestScoped @Named(&quot;bean&quot;) public class Bean { ... @In Service service; ... }

DI mit WebBeans

package de.jax;

import javax.webbeans.*;

@Component

@RequestScoped

@Named(&quot;bean&quot;)

public class Bean

{

...

@In Service service;

...

}

Q&A Fragen ? Danke! Blog: http://matthiaswessendorf.wordpress.com Email: matzew AT apache DOT org

Fragen ?

Danke!

Blog:

http://matthiaswessendorf.wordpress.com

Email:

matzew AT apache DOT org

Add a comment

Related pages

Google Guice and Apache MyFaces 1.2.x | Matthias ...

With the advent of Apache MyFaces 1.2.2 it is possible to use Google Guice for managing your backing beans dependencies (see MYFACES-1747 ...
Read more

Tutorial project showing how to setup a Google Guice ...

guice-jsf - Tutorial project showing how to setup a Google Guice featured web stack for running my-faces JSF
Read more

JavaServer Faces 2 - jug-muenster.de

MyFaces 2.0 • aktueller Stand ... –Google Guice (RI) –Spring –JSR 299 (Apache, JBoss, Resin) JSR 299 –Quick introduction • Producer Methods:
Read more

Apache MyFaces JSF-2.2 Core API - Project Dependency ...

GroupId ArtifactId Version Type License; com.google.code.guice: guice: 1.0: jar: The Apache Software License, Version 2.0: commons-beanutils: commons-beanutils
Read more

Andreas Wagner from Langenfeld/Düsseldorf, Senior ...

... DB2, Maven, SVN, JSF2/Tomahawk/Myfaces. ... Software: Tomcat, Google Guice, JUnit, Mockito, Maven, SVN, Jira, Eclipse, properitäres GUI-Framework.
Read more

Myfaces | LinkedIn

View 2140 Myfaces posts, presentations, experts, and more. Get the professional knowledge you need on LinkedIn.
Read more

Lightweight Web Application Framework : PrimeFaces (JSF ...

For MyFaces, just add the following sample configuration into your web.xml. ... Integrate Google Guice with JSF Google Guice will be used as IOC container.
Read more

Apache MyFaces-Trinidad - FrontPage - General Wiki

Apache MyFaces. JavaServer(tm) Faces (JSF) is a web application framework specification (JSR252, ... JSF and Google Guice Integrating Guice and ...
Read more

Apache MyFaces JSF-2.2 Core Impl - Project Dependencies

com.google.code.guice: guice: 1.0: jar: ... Transitive dependencies are the dependencies of the project dependencies. ... org.apache.myfaces.core.internal: ...
Read more

Add-ons - Apache MyFaces Extensions Validator - Apache ...

MyFaces@Twitter; Development. Source Code; Issue Tracker; Sponsorship. Thanks; Sponsoring Apache; Attachments (0) Page History Restrictions
Read more