SlideShare une entreprise Scribd logo
1  sur  28
Sapphire Gimlets*  Robert Cooper ReachCall.com *no onions
Guice Dependency Injection Java Source configuration Convention over configuration Fast
Gin Guice for GWT apps 100% Client Side Based on Deferred Binding, so some limitations Fast
Guicevs Spring Spring Config Hell Promise of simplicity Ever expanding config files Lots of subprojects Becomes more brittle, in that it becomes harder to extract code for reuse.
Guicevs Spring Guice Java Source “configuration” Convention over configuration. Annotation based Opposite of Spring in defaults Prefer constructor injection Prefer new instances (“Prototype”) over Singletons Provider<T> interface encourages mixing scopes Type Safe – you got static typing, use it!
Guice 101 Basic Annotations @Inject @ImplementedBy/@ProvidedBy @[Scope] @Named
Guice 101 public class MyClass{     private final MyService service;     @Injectpublic MyClass(finalMyService service){this.service = service;}     public String sayHelloToUser(){	return this.service.hello();} }    @ImplementedBy(MyServiceImpl.class)public interface MyService{     public String hello();}
Guice 101 @Singleton public class MyServiceImpl implements MyService {     private final Provider<MySessionObject> sessionProvider;     public MyServiceImpl(final Provider<MySessionObject> sessionProvider){ this.sessionProvider = sessionProvider;     }     @Override     public String hello() {         return "Hello, "+sessionProvider.get().getName() +"!";     } }
Guice 101 import com.google.inject.servlet.SessionScoped; @SessionScoped public class MySessionObject {     private String name;     public String getName(){         return this.name;     }     public void setName(String name){ this.name = name;     } }
Guice 101 What have we done? MyClass gets a MyService injected. MyService is implemented by MyServiceImpl as a Singleton MyServiceImpl gets a Provider<MySessionObject> The hello() method fetches the Session scoped MySession object, and says Hello!
Guice 101 @ImplementedBy provides defaults for any interface (You don’t need to config these if the default is OK) Injection is type safe on constructors (Unit tests have obvious requirements) Scope annotations (@SessionScoped, @Singleton) provide defaults. Provider<T> hides the scope shift from the Singleton service in a single thread method.
Guice 101 Making it work on the web…
Guice 101 public class GuiceContextListener extends GuiceServletContextListener {     @Override     protected Injector getInjector() {         return Guice.createInjector(             new Module() {     public void configure(Binder binder) {                   // Oh Wait, we don’t need anything here!                 }             }          }, new ServletModule() {                 @Override                 protected void configureServlets() { this.serve(“/myPath/*”).with(MyServlet.class);                 }           }     } }
Guice 101 Warning! Overly complicated coming up!
Guice 101 @Singleton public class MyServlet extends HttpServlet{     private final Provider<MySessionObject> sessionProvider;     private final MyClassmyClass;     @Inject     public MyServlet(final Provider<MySessionObject> sessionProvider, MyClassmyClass){ this.sessionProvider = sessionProvider; this.myClass = myClass;     }     @Override     public void doGet(HttpServletRequestreq, HttpServletResponse res) throws IOException { this.sessionProvider.get().setName(req.getParameter("name")); res.getWriter().println(myClass.hello());     } }
Guice 101 So this is interesting. What happened there? The SessionScoped object was created, and set on the session. MyClass was created one off for injection into the servlet. Since it wasn’t a Provider<MyClass> it was implicitly @Singleton with the servlet But since the MyClass implementation used a provider, it ended up performing a singleton-session scoped op.
Guice 101 What else? Servlets, Filters, etc must always be @Singleton of course, this just makes sense. They can *still* get anything injected into them. You need to add your boostrapGuiceContextListener to your web.xml For Servlet < 3.0 you need to add a com.google.inject.servlet.GuiceFilter to /*
Guice 201 Why does this rock? (Hint GWT stuff…)  Can you say RemoteServiceServlet? No more calls to getThreadLocalX() for servlet context stuff. Just use Provider<T>
Guice 201: Warp The Warp-* projects provide a lot of utility ops for Guice: @Transactional scope Transaction per Request Some stuff not dissimilar to Spring WebFlow or Stripes.
Guice 201: More Config @Named lets you provide named values in your configuration. (You can also create custom annotations) You can also use @Provides for factory methods in your modules for simple providers @Provides  public Connection getConnection(@Named(“jdbc-url”) String jdbcUrl) { DriverManager.forName(whatever); DriverManager.getConnection(jdbcUrl); }
Guice 201: More Config binder.bind(String.class).annotatedWith(Names.named(“jdbc-url”)).toInstance(“jdbc:mysql://localhost/mydb”); Note: @Provides methods could do JNDI lookups, or get other stuff. (This is a stupid example!)
Of Junipers and Bathtubs Gin is Google Guice for GWT client side apps. Based on DeferrredBinding. Certain features lost: Binding .toInstance() on any module Anonymous Provider<T> can’t be done (@Provides is *kinda* OK) Scoping needs to be by hard class reference.
Of Junipers and Bathtubs That is.. in(Singleton.class) not .asEagerSingleton() or with @Singleton Gin seems both stupid and too smart. Binding a RemoteServiceAsync class works out of the box… Unless you want to do @Provides Gin created classes are new Class() not GWT.create(Class.class); -- except for RemoteServiceAsync, which is a special scope.
Gin 101 @GinModules(MyGinModule.class) public interface Injector extends Ginjector {     public static final Injector INSTANCE = GWT.create(Injector.class);     public MyStartupClassstartupClass(); } public class Module extends AbstractGinModule {     @Override     protected void configure() { this.bind(Resources.class)             .toProvider(ResourcesProvider.class)             .in(Singleton.class)     } } public static class ResourcesProvider implements Provider<Resources> { @Override  public Resources get() {             return GWT.create(Resources.class);      } }
Gin 101 Does this step on the toes of DeBi?  OK, maybe a little bit. Sure you could do a singleton Resources.INSTANCE but injection makes Unit testing without dropping into GWTTestCase more frequent. (Read: much, much faster) This reserves DeBi for what it is best for: local environment specific code.
Gin 201 What can’t you do with Gin? Remember your Injector.INSTANCE is compile-time: No “toInstance()” bindings… ever. No “toProvider(AnonymousProvider(){})” providers need to be public scoped static classes at the least. Gin treats RemoteServiceAsync specially. You can do a provider, but if you try @Provides methods in your GinModule classes, you will get a duplicate declaration error.
Gin 201 All this aside, stacked with DeBi, Gin can rock rough and stuff with its Afro Puffs. Replace your Startup call with a Sync method. Replace your Async classes with thing that talk to local storage. Automagically retry onFailure() methods on network errors while your uses if offline.
Way way more Warp-* is some great stuff: http://code.google.com/p/warp-persist/ http://code.google.com/p/google-sitebricks/ Guice with JAX-WShttps://jax-ws-commons.dev.java.net/guice/ (Strangely harder than JAX-RS)

Contenu connexe

Tendances

End to end todo list app with NestJs - Angular - Redux & Redux Saga
End to end todo list app with NestJs - Angular - Redux & Redux SagaEnd to end todo list app with NestJs - Angular - Redux & Redux Saga
End to end todo list app with NestJs - Angular - Redux & Redux SagaBabacar NIANG
 
Advanced VCL: how to use restart
Advanced VCL: how to use restartAdvanced VCL: how to use restart
Advanced VCL: how to use restartFastly
 
Design & Performance - Steve Souders at Fastly Altitude 2015
Design & Performance - Steve Souders at Fastly Altitude 2015Design & Performance - Steve Souders at Fastly Altitude 2015
Design & Performance - Steve Souders at Fastly Altitude 2015Fastly
 
Solving anything in VCL
Solving anything in VCLSolving anything in VCL
Solving anything in VCLFastly
 
Avoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promisesAvoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promisesAnkit Agarwal
 
利用Init connect做mysql clients stat 用户审计
 利用Init connect做mysql clients stat 用户审计 利用Init connect做mysql clients stat 用户审计
利用Init connect做mysql clients stat 用户审计Dehua Yang
 
Hacking the Mesh: Extending Istio with WebAssembly Modules | DevNation Tech Talk
Hacking the Mesh: Extending Istio with WebAssembly Modules | DevNation Tech TalkHacking the Mesh: Extending Istio with WebAssembly Modules | DevNation Tech Talk
Hacking the Mesh: Extending Istio with WebAssembly Modules | DevNation Tech TalkRed Hat Developers
 
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017Mike Nakhimovich
 
Vielseitiges In-Memory Computing mit Apache Ignite und Kubernetes
Vielseitiges In-Memory Computing mit Apache Ignite und KubernetesVielseitiges In-Memory Computing mit Apache Ignite und Kubernetes
Vielseitiges In-Memory Computing mit Apache Ignite und KubernetesQAware GmbH
 
OSGi Puzzlers - Neil Bartlett & Peter Kriens
OSGi Puzzlers - Neil Bartlett & Peter KriensOSGi Puzzlers - Neil Bartlett & Peter Kriens
OSGi Puzzlers - Neil Bartlett & Peter Kriensmfrancis
 
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...EPAM_Systems_Bulgaria
 
Middy.js - A powerful Node.js middleware framework for your lambdas​
Middy.js - A powerful Node.js middleware framework for your lambdas​ Middy.js - A powerful Node.js middleware framework for your lambdas​
Middy.js - A powerful Node.js middleware framework for your lambdas​ Luciano Mammino
 
Altitude NY 2018: Programming the edge workshop
Altitude NY 2018: Programming the edge workshopAltitude NY 2018: Programming the edge workshop
Altitude NY 2018: Programming the edge workshopFastly
 
Martin Anderson - threads v actors
Martin Anderson - threads v actorsMartin Anderson - threads v actors
Martin Anderson - threads v actorsbloodredsun
 
IPC: AIDL is sexy, not a curse
IPC: AIDL is sexy, not a curseIPC: AIDL is sexy, not a curse
IPC: AIDL is sexy, not a curseYonatan Levin
 
Ipc: aidl sexy, not a curse
Ipc: aidl sexy, not a curseIpc: aidl sexy, not a curse
Ipc: aidl sexy, not a curseYonatan Levin
 
Serverless, The Middy Way - Workshop
Serverless, The Middy Way - WorkshopServerless, The Middy Way - Workshop
Serverless, The Middy Way - WorkshopLuciano Mammino
 

Tendances (20)

End to end todo list app with NestJs - Angular - Redux & Redux Saga
End to end todo list app with NestJs - Angular - Redux & Redux SagaEnd to end todo list app with NestJs - Angular - Redux & Redux Saga
End to end todo list app with NestJs - Angular - Redux & Redux Saga
 
Advanced VCL: how to use restart
Advanced VCL: how to use restartAdvanced VCL: how to use restart
Advanced VCL: how to use restart
 
Design & Performance - Steve Souders at Fastly Altitude 2015
Design & Performance - Steve Souders at Fastly Altitude 2015Design & Performance - Steve Souders at Fastly Altitude 2015
Design & Performance - Steve Souders at Fastly Altitude 2015
 
Solving anything in VCL
Solving anything in VCLSolving anything in VCL
Solving anything in VCL
 
Avoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promisesAvoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promises
 
利用Init connect做mysql clients stat 用户审计
 利用Init connect做mysql clients stat 用户审计 利用Init connect做mysql clients stat 用户审计
利用Init connect做mysql clients stat 用户审计
 
Hacking the Mesh: Extending Istio with WebAssembly Modules | DevNation Tech Talk
Hacking the Mesh: Extending Istio with WebAssembly Modules | DevNation Tech TalkHacking the Mesh: Extending Istio with WebAssembly Modules | DevNation Tech Talk
Hacking the Mesh: Extending Istio with WebAssembly Modules | DevNation Tech Talk
 
Enterprise Java Puzzlers
Enterprise Java PuzzlersEnterprise Java Puzzlers
Enterprise Java Puzzlers
 
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
 
Vielseitiges In-Memory Computing mit Apache Ignite und Kubernetes
Vielseitiges In-Memory Computing mit Apache Ignite und KubernetesVielseitiges In-Memory Computing mit Apache Ignite und Kubernetes
Vielseitiges In-Memory Computing mit Apache Ignite und Kubernetes
 
OSGi Puzzlers - Neil Bartlett & Peter Kriens
OSGi Puzzlers - Neil Bartlett & Peter KriensOSGi Puzzlers - Neil Bartlett & Peter Kriens
OSGi Puzzlers - Neil Bartlett & Peter Kriens
 
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
 
Middy.js - A powerful Node.js middleware framework for your lambdas​
Middy.js - A powerful Node.js middleware framework for your lambdas​ Middy.js - A powerful Node.js middleware framework for your lambdas​
Middy.js - A powerful Node.js middleware framework for your lambdas​
 
Hands on: Hystrix
Hands on: HystrixHands on: Hystrix
Hands on: Hystrix
 
Altitude NY 2018: Programming the edge workshop
Altitude NY 2018: Programming the edge workshopAltitude NY 2018: Programming the edge workshop
Altitude NY 2018: Programming the edge workshop
 
Martin Anderson - threads v actors
Martin Anderson - threads v actorsMartin Anderson - threads v actors
Martin Anderson - threads v actors
 
IPC: AIDL is sexy, not a curse
IPC: AIDL is sexy, not a curseIPC: AIDL is sexy, not a curse
IPC: AIDL is sexy, not a curse
 
Ipc: aidl sexy, not a curse
Ipc: aidl sexy, not a curseIpc: aidl sexy, not a curse
Ipc: aidl sexy, not a curse
 
Serverless, The Middy Way - Workshop
Serverless, The Middy Way - WorkshopServerless, The Middy Way - Workshop
Serverless, The Middy Way - Workshop
 
Unqlite
UnqliteUnqlite
Unqlite
 

En vedette

18. truong cong dat presentation
18. truong cong dat presentation18. truong cong dat presentation
18. truong cong dat presentationBinhThang
 
Things You Dont See Everyday
Things You Dont See EverydayThings You Dont See Everyday
Things You Dont See EverydaySam Ignarski
 
Extreme Source Compatibility
Extreme Source CompatibilityExtreme Source Compatibility
Extreme Source CompatibilityRobert Cooper
 
Galicia Calidade
Galicia CalidadeGalicia Calidade
Galicia Calidadeguest027ad
 
8. nguyen van hai
8. nguyen van hai8. nguyen van hai
8. nguyen van haiBinhThang
 
Introducing FOB Network
Introducing FOB NetworkIntroducing FOB Network
Introducing FOB NetworkSam Ignarski
 
6. le tan phung
6. le tan phung6. le tan phung
6. le tan phungBinhThang
 

En vedette (9)

18. truong cong dat presentation
18. truong cong dat presentation18. truong cong dat presentation
18. truong cong dat presentation
 
Amazing
AmazingAmazing
Amazing
 
Things You Dont See Everyday
Things You Dont See EverydayThings You Dont See Everyday
Things You Dont See Everyday
 
Extreme Source Compatibility
Extreme Source CompatibilityExtreme Source Compatibility
Extreme Source Compatibility
 
Galicia Calidade
Galicia CalidadeGalicia Calidade
Galicia Calidade
 
8. nguyen van hai
8. nguyen van hai8. nguyen van hai
8. nguyen van hai
 
Introducing FOB Network
Introducing FOB NetworkIntroducing FOB Network
Introducing FOB Network
 
6. le tan phung
6. le tan phung6. le tan phung
6. le tan phung
 
Stop Complaining
Stop ComplainingStop Complaining
Stop Complaining
 

Similaire à Sapphire Gimlets

Enterprise Guice 20090217 Bejug
Enterprise Guice 20090217 BejugEnterprise Guice 20090217 Bejug
Enterprise Guice 20090217 Bejugrobbiev
 
Jug Guice Presentation
Jug Guice PresentationJug Guice Presentation
Jug Guice PresentationDmitry Buzdin
 
Thomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalThomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalDroidcon Berlin
 
Das kannste schon so machen
Das kannste schon so machenDas kannste schon so machen
Das kannste schon so machenAndré Goliath
 
Cassandra Day NY 2014: Getting Started with the DataStax C# Driver
Cassandra Day NY 2014: Getting Started with the DataStax C# DriverCassandra Day NY 2014: Getting Started with the DataStax C# Driver
Cassandra Day NY 2014: Getting Started with the DataStax C# DriverDataStax Academy
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotationjavatwo2011
 
CDI Telco Framework & Arquillian presentation at Mobicents Summit, Sochi 2011
CDI Telco Framework & Arquillian presentation at Mobicents Summit, Sochi 2011CDI Telco Framework & Arquillian presentation at Mobicents Summit, Sochi 2011
CDI Telco Framework & Arquillian presentation at Mobicents Summit, Sochi 2011telestax
 
SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8Ben Abdallah Helmi
 
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten ZiegelerOSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegelermfrancis
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web ToolkitsYiguang Hu
 
谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testabilitydrewz lin
 
Getting Started with Datatsax .Net Driver
Getting Started with Datatsax .Net DriverGetting Started with Datatsax .Net Driver
Getting Started with Datatsax .Net DriverDataStax Academy
 
06 response-headers
06 response-headers06 response-headers
06 response-headerssnopteck
 
Architecting your GWT applications with GWT-Platform - Lesson 02
Architecting your GWT applications with GWT-Platform - Lesson 02Architecting your GWT applications with GWT-Platform - Lesson 02
Architecting your GWT applications with GWT-Platform - Lesson 02rhemsolutions
 

Similaire à Sapphire Gimlets (20)

Enterprise Guice 20090217 Bejug
Enterprise Guice 20090217 BejugEnterprise Guice 20090217 Bejug
Enterprise Guice 20090217 Bejug
 
Jug Guice Presentation
Jug Guice PresentationJug Guice Presentation
Jug Guice Presentation
 
Thomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalThomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-final
 
GWT MVP Case Study
GWT MVP Case StudyGWT MVP Case Study
GWT MVP Case Study
 
Devoxx 2012 (v2)
Devoxx 2012 (v2)Devoxx 2012 (v2)
Devoxx 2012 (v2)
 
GWT Extreme!
GWT Extreme!GWT Extreme!
GWT Extreme!
 
Das kannste schon so machen
Das kannste schon so machenDas kannste schon so machen
Das kannste schon so machen
 
Cassandra Day NY 2014: Getting Started with the DataStax C# Driver
Cassandra Day NY 2014: Getting Started with the DataStax C# DriverCassandra Day NY 2014: Getting Started with the DataStax C# Driver
Cassandra Day NY 2014: Getting Started with the DataStax C# Driver
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
guice-servlet
guice-servletguice-servlet
guice-servlet
 
Guice
GuiceGuice
Guice
 
Guice
GuiceGuice
Guice
 
CDI Telco Framework & Arquillian presentation at Mobicents Summit, Sochi 2011
CDI Telco Framework & Arquillian presentation at Mobicents Summit, Sochi 2011CDI Telco Framework & Arquillian presentation at Mobicents Summit, Sochi 2011
CDI Telco Framework & Arquillian presentation at Mobicents Summit, Sochi 2011
 
SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8
 
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten ZiegelerOSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web Toolkits
 
谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability
 
Getting Started with Datatsax .Net Driver
Getting Started with Datatsax .Net DriverGetting Started with Datatsax .Net Driver
Getting Started with Datatsax .Net Driver
 
06 response-headers
06 response-headers06 response-headers
06 response-headers
 
Architecting your GWT applications with GWT-Platform - Lesson 02
Architecting your GWT applications with GWT-Platform - Lesson 02Architecting your GWT applications with GWT-Platform - Lesson 02
Architecting your GWT applications with GWT-Platform - Lesson 02
 

Dernier

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 

Dernier (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Sapphire Gimlets

  • 1. Sapphire Gimlets* Robert Cooper ReachCall.com *no onions
  • 2. Guice Dependency Injection Java Source configuration Convention over configuration Fast
  • 3. Gin Guice for GWT apps 100% Client Side Based on Deferred Binding, so some limitations Fast
  • 4. Guicevs Spring Spring Config Hell Promise of simplicity Ever expanding config files Lots of subprojects Becomes more brittle, in that it becomes harder to extract code for reuse.
  • 5. Guicevs Spring Guice Java Source “configuration” Convention over configuration. Annotation based Opposite of Spring in defaults Prefer constructor injection Prefer new instances (“Prototype”) over Singletons Provider<T> interface encourages mixing scopes Type Safe – you got static typing, use it!
  • 6. Guice 101 Basic Annotations @Inject @ImplementedBy/@ProvidedBy @[Scope] @Named
  • 7. Guice 101 public class MyClass{ private final MyService service; @Injectpublic MyClass(finalMyService service){this.service = service;} public String sayHelloToUser(){ return this.service.hello();} } @ImplementedBy(MyServiceImpl.class)public interface MyService{ public String hello();}
  • 8. Guice 101 @Singleton public class MyServiceImpl implements MyService { private final Provider<MySessionObject> sessionProvider; public MyServiceImpl(final Provider<MySessionObject> sessionProvider){ this.sessionProvider = sessionProvider; } @Override public String hello() { return "Hello, "+sessionProvider.get().getName() +"!"; } }
  • 9. Guice 101 import com.google.inject.servlet.SessionScoped; @SessionScoped public class MySessionObject { private String name; public String getName(){ return this.name; } public void setName(String name){ this.name = name; } }
  • 10. Guice 101 What have we done? MyClass gets a MyService injected. MyService is implemented by MyServiceImpl as a Singleton MyServiceImpl gets a Provider<MySessionObject> The hello() method fetches the Session scoped MySession object, and says Hello!
  • 11. Guice 101 @ImplementedBy provides defaults for any interface (You don’t need to config these if the default is OK) Injection is type safe on constructors (Unit tests have obvious requirements) Scope annotations (@SessionScoped, @Singleton) provide defaults. Provider<T> hides the scope shift from the Singleton service in a single thread method.
  • 12. Guice 101 Making it work on the web…
  • 13. Guice 101 public class GuiceContextListener extends GuiceServletContextListener { @Override protected Injector getInjector() { return Guice.createInjector( new Module() { public void configure(Binder binder) { // Oh Wait, we don’t need anything here! } } }, new ServletModule() { @Override protected void configureServlets() { this.serve(“/myPath/*”).with(MyServlet.class); } } } }
  • 14. Guice 101 Warning! Overly complicated coming up!
  • 15. Guice 101 @Singleton public class MyServlet extends HttpServlet{ private final Provider<MySessionObject> sessionProvider; private final MyClassmyClass; @Inject public MyServlet(final Provider<MySessionObject> sessionProvider, MyClassmyClass){ this.sessionProvider = sessionProvider; this.myClass = myClass; } @Override public void doGet(HttpServletRequestreq, HttpServletResponse res) throws IOException { this.sessionProvider.get().setName(req.getParameter("name")); res.getWriter().println(myClass.hello()); } }
  • 16. Guice 101 So this is interesting. What happened there? The SessionScoped object was created, and set on the session. MyClass was created one off for injection into the servlet. Since it wasn’t a Provider<MyClass> it was implicitly @Singleton with the servlet But since the MyClass implementation used a provider, it ended up performing a singleton-session scoped op.
  • 17. Guice 101 What else? Servlets, Filters, etc must always be @Singleton of course, this just makes sense. They can *still* get anything injected into them. You need to add your boostrapGuiceContextListener to your web.xml For Servlet < 3.0 you need to add a com.google.inject.servlet.GuiceFilter to /*
  • 18. Guice 201 Why does this rock? (Hint GWT stuff…) Can you say RemoteServiceServlet? No more calls to getThreadLocalX() for servlet context stuff. Just use Provider<T>
  • 19. Guice 201: Warp The Warp-* projects provide a lot of utility ops for Guice: @Transactional scope Transaction per Request Some stuff not dissimilar to Spring WebFlow or Stripes.
  • 20. Guice 201: More Config @Named lets you provide named values in your configuration. (You can also create custom annotations) You can also use @Provides for factory methods in your modules for simple providers @Provides public Connection getConnection(@Named(“jdbc-url”) String jdbcUrl) { DriverManager.forName(whatever); DriverManager.getConnection(jdbcUrl); }
  • 21. Guice 201: More Config binder.bind(String.class).annotatedWith(Names.named(“jdbc-url”)).toInstance(“jdbc:mysql://localhost/mydb”); Note: @Provides methods could do JNDI lookups, or get other stuff. (This is a stupid example!)
  • 22. Of Junipers and Bathtubs Gin is Google Guice for GWT client side apps. Based on DeferrredBinding. Certain features lost: Binding .toInstance() on any module Anonymous Provider<T> can’t be done (@Provides is *kinda* OK) Scoping needs to be by hard class reference.
  • 23. Of Junipers and Bathtubs That is.. in(Singleton.class) not .asEagerSingleton() or with @Singleton Gin seems both stupid and too smart. Binding a RemoteServiceAsync class works out of the box… Unless you want to do @Provides Gin created classes are new Class() not GWT.create(Class.class); -- except for RemoteServiceAsync, which is a special scope.
  • 24. Gin 101 @GinModules(MyGinModule.class) public interface Injector extends Ginjector { public static final Injector INSTANCE = GWT.create(Injector.class); public MyStartupClassstartupClass(); } public class Module extends AbstractGinModule { @Override protected void configure() { this.bind(Resources.class) .toProvider(ResourcesProvider.class) .in(Singleton.class) } } public static class ResourcesProvider implements Provider<Resources> { @Override public Resources get() { return GWT.create(Resources.class); } }
  • 25. Gin 101 Does this step on the toes of DeBi? OK, maybe a little bit. Sure you could do a singleton Resources.INSTANCE but injection makes Unit testing without dropping into GWTTestCase more frequent. (Read: much, much faster) This reserves DeBi for what it is best for: local environment specific code.
  • 26. Gin 201 What can’t you do with Gin? Remember your Injector.INSTANCE is compile-time: No “toInstance()” bindings… ever. No “toProvider(AnonymousProvider(){})” providers need to be public scoped static classes at the least. Gin treats RemoteServiceAsync specially. You can do a provider, but if you try @Provides methods in your GinModule classes, you will get a duplicate declaration error.
  • 27. Gin 201 All this aside, stacked with DeBi, Gin can rock rough and stuff with its Afro Puffs. Replace your Startup call with a Sync method. Replace your Async classes with thing that talk to local storage. Automagically retry onFailure() methods on network errors while your uses if offline.
  • 28. Way way more Warp-* is some great stuff: http://code.google.com/p/warp-persist/ http://code.google.com/p/google-sitebricks/ Guice with JAX-WShttps://jax-ws-commons.dev.java.net/guice/ (Strangely harder than JAX-RS)