SlideShare a Scribd company logo
JavaFX Dependency
Injection with
FxContainer

                    Presented by
                 Srikanth Shenoy
                    ObjectSource
  Learn FxContainer in 10 minutes
Introduction
   Who am I?
       A hands-on architect
       Experienced and very knowledgeable in Java and
        Java EE
       Authored a book on Struts
       Several articles on Java EE for leading journals
       Director of Enterprise Java Practice at
        ObjectSource
       Creator of FxObjects and FxContainer framework
Introduction
   What is FxContainer? (Currently 1.0)
       Open source IoC container written in JavaFX.
       Meant specifically for Dependency Injection in
        JavaFX applications
       Very much Spring-like
       Small footprint (< 75K)
       Sequence, Collection Support
       Mix Java and JavaFX wiring
   Learn FxContainer in 20 slides! (10 minutes)
IoC Container Landscape
   Spring
       Supports XML and Annotations based DI
       Supports Constructor & Setter Injection
   Guice
       Supports Annotations and API based DI
       Supports Constructor & Setter Injection
   PicoContainer and many more..
Why another IoC Container?
   Problems with existing IoC Containers (in the
    context of JavaFX)
       JavaFX does not support annotations 
           Hence Guice leaves only programmatic DI option
           Xml DI with Spring works, but minimal jars > 1 MB
       Constructor Injection not supported in JavaFX
       Setter Injection is unnatural for JavaFX style
           JavaFX variables are written as public or public-init
           Writing setXYZ() method for variables feels artificial
       Nobody supports Sequence (the first class
        JavaFX collection citizen)
Setter Injection
var finder = MovieFinder {
  movieDao: MovieDao { }
}

   Dependency Injection with any other IoC
    container needs a setter like this (Not Good)
public class MovieFinder {
  public-init movieDao:MovieDao;
  public function setMovieDao(dao:MovieDao) {
    this.movieDao = dao;
  }
}
Side effects of Setter Injection
   Init and Post-Init code that depends on
    public-init variables will not work
   Classic Example – CustomNode.create()
       Called automatically in every UI
       Called immediately after init and post init
       No time to call setter methods
       If create() depends on objects injected by setter
        injection, then it will fail !!
A Different Kind of Injection
 Constructor Injection is DURING memory
  allocation
 Setter Injection is AFTER memory allocation
  and object initialization
 JavaFX needs something in between the two

 We call it Init injection

 A DI based on Init Injection does not exist

 So we created it 
 FxContainer is the ONLY IoC container that
        provides Init Injection for JavaFX
FxContainer Core Concept
   Based on JavaFX Reflection and Init Injection
FXLocal.Context ctx = FXLocal.getContext();

//get the class
FXClassType clzType = ctx.findClass("org.fxobjects.MovieFinder");

FXObjectValue objValue = clzType.allocate(); //allocate memory

//get the variable-type
FXVarMember varMember = clzType.getVariable("movieDao");

//create the variable-value
FXValue varValue = ctx.mirrorOf(Some String or object);

//initialize the variable. Basis for FxContainer Init Injection

objValue.initVar(varMember, varValue);
objValue.initialize(); //Finally initialize the object
FxContainer Overview
   Uses Setter Injection for Java objects
   Uses Init Injection for JavaFX objects
   Can mix both Java and JavaFX objects
   Java objects can hold references to JavaFX
    objects via interfaces
   Very Spring Like in configuration
   Powerful, Lightweight and Easy to use
FxContainer: Simple
Dependency Injection
<fxcontainer>

<fxobject name="movieFinder"
  class="org.fxobjects.samples.fxcontainer.MovieFinderImpl">
  <property name="someVar" value="Some random value"/>
  <property name="javaHelper" ref="javaHelperObj"/>
  <property name="jfxHelper" ref="jfxHelperVarObj"/>
</fxobject>

<fxobject name="movieLister"
  class="org.fxobjects.samples.fxcontainer.MovieLister">
  <property name="finder" ref="movieFinder"/>
</fxobject>

</fxcontainer>
FxContainer: Import XML
   Good for organizing large XML into smaller
    logical chunks
<fxcontainer>

<import resource="/org/fxobjects/samples/abc.xml"/>

<fxobject name="movieFinder"
  class="org.fxobjects.samples.fxcontainer.MovieFinderImpl">
  <property name="someVar" value="Some random value"/>
  <property name="javaHelper" ref="javaHelperObj"/>
  <property name="jfxHelper" ref="jfxHelperVarObj"/>
</fxobject>

</fxcontainer>
FxContainer: Import Properties
   Good for Spring style ${ } substitutions
<fxcontainer>

<import resource="/org/fxobjects/samples/abc.properties"/>

<fxobject name="movieFinder"
  class="org.fxobjects.samples.fxcontainer.MovieFinderImpl">
  <property name="someVar" value=“${svr1Name} is ${svr1Status}"/>
  <property name="javaHelper" ref=“${helperObj}"/>
  <property name="jfxHelper" ref="jfxHelperVarObj"/>
</fxobject>

</fxcontainer>

   value and ref can be substituted
FxContainer: Wired Object
Attributes
   Wired Objects in FxContainer have the following defaults
    (can be overridden)
       Every wired object is singleton
       Every wired object is lazily initialized (i.e on demand)
       There is no order of initialization (Although load order can be
        specified for eagerly loaded objects)
       Init-method is called after all properties are injected
<fxcontainer>
<fxobject name="movieFinder"
   class="org.fxobjects.samples.fxcontainer.MovieFinderImpl“ lazy-
   init=“false” load-order=“1” init-method=“someMethod”>
..
 </fxobject>
</fxcontainer>
FxContainer: Sequences
<fxcontainer>
 <fxobject name="movieFinder"
   class="org.fxobjects.samples.fxcontainer.MovieFinderImpl">
   <property name="movieCodes">     Primitive Sequence
     <sequence>
       <entry value=“1" />
       <entry value="2" />
     </sequence>
   </property>
   <property name="movies">         Object Sequence
     <sequence>
       <entry ref="movie1"/>
       <entry ref="movie2"/>
     </sequence>
   </property>
  </fxobject>
</fxcontainer>
FxContainer: Lists and Sets
<fxcontainer>
 <fxobject name="movieFinder"
   class="org.fxobjects.samples.fxcontainer.MovieFinderImpl">
   <property name="movieList">              Object LIST
     <list> (or <set>)
       <entry ref="movie1"/>
       <entry ref="movie2"/>
     </list>
   </property>
   <property name="movieCodes">             Primitive LIST
     <list valueClass=“java.lang.Integer”>
       <entry value=“1" />
       <entry value="2" />
     </list>
   </property>
  </fxobject>
</fxcontainer>
FxObjects Lists and Sets
(Contd.)
   Lists and Sets have to be initialized in their
    parents with a new …..();
   valueClass attribute
       Optional in most cases
       Reason: One cannot tell from the xml value
        attributed if a list is Integer, String, BigDecimal
        etc.
       Needed for Java and JavaFX objects when
           value attribute is specified in xml AND
           List is not parameterized (always the case in JavaFX)
FxContainer: Maps
<fxcontainer>
 <fxobject name="movieFinder"
   class="org.fxobjects.samples.fxcontainer.MovieFinderImpl">
   <property name="movieMap">              Object MAP
     <map>
       <entry keyRef=“stage1” valueRef="movie1"/>
     </map>
   </property>
   <property name="movieCodes">             Primitive MAP
    <map keyClass=“java.lang.String”
   valueClass=“java.lang.Integer”>
       <entry key=“Terminator 1” value=“1" />
       <entry value=“Terminator 2" />
     </map>
   </property>
  </fxobject>
</fxcontainer>
keyClass and valueClass are needed for Maps in JavaFX with key
   and value specified. (since they cannot be parameterized) and
FxContainer: Startup
   Spring Like
var loader = ClasspathXmlContainerLoader {
  resourceLocation: "/org/fxobjects/samples/my.xml“
}
var container:FxContainer = loader.load();
var mvLister:MovieLister =
      container.getFxObject("movieLister") as MovieLister;

   FxContainer can be used the IoC container
    with FxObjects or any other JavaFX
    application independently
   Just include 2 jars – fxcontainer.jar and
    fxobjects-util.jar in classpath
FxObjects & FxContainer:
Roadmap
Links
   Project site – https://fxobjects/dev.java.net
   FxContainer is a subproject of FxObjects
   Not a single person open source project
       FxObjects and FxContainer developed and supported
        by ObjectSource (http://www.objectsource.com)
   Download, use, extend, redistribute, OEM
   Discussion Forums on project site
   Participation welcome
       Post ideas, questions, concerns, comments
       Contribute code

More Related Content

PPT
Java Serialization
PDF
Serialization & De-serialization in Java
PDF
Bytecode manipulation with Javassist and ASM
PPTX
Making Java more dynamic: runtime code generation for the JVM
PPTX
Byte code field report
PDF
Java Programming - 04 object oriented in java
PPTX
Java 10, Java 11 and beyond
PPTX
The definitive guide to java agents
Java Serialization
Serialization & De-serialization in Java
Bytecode manipulation with Javassist and ASM
Making Java more dynamic: runtime code generation for the JVM
Byte code field report
Java Programming - 04 object oriented in java
Java 10, Java 11 and beyond
The definitive guide to java agents

What's hot (20)

PPTX
Getting started with Java 9 modules
PPTX
The Java memory model made easy
PPT
Java basic
PPTX
Java 9
PPT
Invoke dynamics
PPT
Java basic tutorial by sanjeevini india
PPTX
Java and OpenJDK: disecting the ecosystem
PPTX
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
PPTX
Java byte code in practice
PPTX
RelProxy, Easy Class Reload and Scripting with Java
PDF
Javascript closures
 
PPTX
Fixing the Java Serialization Mess
ODP
Synapseindia reviews.odp.
PPT
Java Tut1
PPT
Javatut1
PDF
Testing untestable code - phpday
PDF
Native code in Android applications
PDF
Basic java for Android Developer
PDF
Core Java Certification
PDF
Java Annotation Processing: A Beginner Walkthrough
Getting started with Java 9 modules
The Java memory model made easy
Java basic
Java 9
Invoke dynamics
Java basic tutorial by sanjeevini india
Java and OpenJDK: disecting the ecosystem
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java byte code in practice
RelProxy, Easy Class Reload and Scripting with Java
Javascript closures
 
Fixing the Java Serialization Mess
Synapseindia reviews.odp.
Java Tut1
Javatut1
Testing untestable code - phpday
Native code in Android applications
Basic java for Android Developer
Core Java Certification
Java Annotation Processing: A Beginner Walkthrough
Ad

Similar to JavaFX Dependency Injection with FxContainer (20)

PDF
Effective JavaFX architecture with FxObjects
PPTX
Java Core | JavaFX 2.0: Great User Interfaces in Java | Simon Ritter
PDF
JavaFX, because you're worth it
PDF
Java FX Part2
PDF
JavaFX Overview
ODP
JavaFX introduction
PPT
JavaFX - Next Generation Java UI
PPTX
JavaFX Presentation
PDF
Javafxpressentation 140524053934-phpapp01 (1)
PDF
PDF
Java Fx Ajaxworld Rags V1
PDF
JavaOne - The JavaFX Community and Ecosystem
PPTX
A brief overview of java frameworks
PPTX
L05 Frameworks
PPT
Unit 1 informatica en ingles
PPT
Unit i informatica en ingles
PDF
Scripting with Java FX - Cédric Tabin - December 2007
PPT
JavaFX
PDF
DataFX - JavaOne 2013
PDF
L07 Frameworks
Effective JavaFX architecture with FxObjects
Java Core | JavaFX 2.0: Great User Interfaces in Java | Simon Ritter
JavaFX, because you're worth it
Java FX Part2
JavaFX Overview
JavaFX introduction
JavaFX - Next Generation Java UI
JavaFX Presentation
Javafxpressentation 140524053934-phpapp01 (1)
Java Fx Ajaxworld Rags V1
JavaOne - The JavaFX Community and Ecosystem
A brief overview of java frameworks
L05 Frameworks
Unit 1 informatica en ingles
Unit i informatica en ingles
Scripting with Java FX - Cédric Tabin - December 2007
JavaFX
DataFX - JavaOne 2013
L07 Frameworks
Ad

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
Teaching material agriculture food technology
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PPTX
Machine Learning_overview_presentation.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Empathic Computing: Creating Shared Understanding
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Machine learning based COVID-19 study performance prediction
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
Teaching material agriculture food technology
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
Machine Learning_overview_presentation.pptx
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25-Week II
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Assigned Numbers - 2025 - Bluetooth® Document
Empathic Computing: Creating Shared Understanding
Review of recent advances in non-invasive hemoglobin estimation
“AI and Expert System Decision Support & Business Intelligence Systems”
Machine learning based COVID-19 study performance prediction

JavaFX Dependency Injection with FxContainer

  • 1. JavaFX Dependency Injection with FxContainer Presented by Srikanth Shenoy ObjectSource Learn FxContainer in 10 minutes
  • 2. Introduction  Who am I?  A hands-on architect  Experienced and very knowledgeable in Java and Java EE  Authored a book on Struts  Several articles on Java EE for leading journals  Director of Enterprise Java Practice at ObjectSource  Creator of FxObjects and FxContainer framework
  • 3. Introduction  What is FxContainer? (Currently 1.0)  Open source IoC container written in JavaFX.  Meant specifically for Dependency Injection in JavaFX applications  Very much Spring-like  Small footprint (< 75K)  Sequence, Collection Support  Mix Java and JavaFX wiring  Learn FxContainer in 20 slides! (10 minutes)
  • 4. IoC Container Landscape  Spring  Supports XML and Annotations based DI  Supports Constructor & Setter Injection  Guice  Supports Annotations and API based DI  Supports Constructor & Setter Injection  PicoContainer and many more..
  • 5. Why another IoC Container?  Problems with existing IoC Containers (in the context of JavaFX)  JavaFX does not support annotations   Hence Guice leaves only programmatic DI option  Xml DI with Spring works, but minimal jars > 1 MB  Constructor Injection not supported in JavaFX  Setter Injection is unnatural for JavaFX style  JavaFX variables are written as public or public-init  Writing setXYZ() method for variables feels artificial  Nobody supports Sequence (the first class JavaFX collection citizen)
  • 6. Setter Injection var finder = MovieFinder { movieDao: MovieDao { } }  Dependency Injection with any other IoC container needs a setter like this (Not Good) public class MovieFinder { public-init movieDao:MovieDao; public function setMovieDao(dao:MovieDao) { this.movieDao = dao; } }
  • 7. Side effects of Setter Injection  Init and Post-Init code that depends on public-init variables will not work  Classic Example – CustomNode.create()  Called automatically in every UI  Called immediately after init and post init  No time to call setter methods  If create() depends on objects injected by setter injection, then it will fail !!
  • 8. A Different Kind of Injection  Constructor Injection is DURING memory allocation  Setter Injection is AFTER memory allocation and object initialization  JavaFX needs something in between the two  We call it Init injection  A DI based on Init Injection does not exist  So we created it  FxContainer is the ONLY IoC container that provides Init Injection for JavaFX
  • 9. FxContainer Core Concept  Based on JavaFX Reflection and Init Injection FXLocal.Context ctx = FXLocal.getContext(); //get the class FXClassType clzType = ctx.findClass("org.fxobjects.MovieFinder"); FXObjectValue objValue = clzType.allocate(); //allocate memory //get the variable-type FXVarMember varMember = clzType.getVariable("movieDao"); //create the variable-value FXValue varValue = ctx.mirrorOf(Some String or object); //initialize the variable. Basis for FxContainer Init Injection objValue.initVar(varMember, varValue); objValue.initialize(); //Finally initialize the object
  • 10. FxContainer Overview  Uses Setter Injection for Java objects  Uses Init Injection for JavaFX objects  Can mix both Java and JavaFX objects  Java objects can hold references to JavaFX objects via interfaces  Very Spring Like in configuration  Powerful, Lightweight and Easy to use
  • 11. FxContainer: Simple Dependency Injection <fxcontainer> <fxobject name="movieFinder" class="org.fxobjects.samples.fxcontainer.MovieFinderImpl"> <property name="someVar" value="Some random value"/> <property name="javaHelper" ref="javaHelperObj"/> <property name="jfxHelper" ref="jfxHelperVarObj"/> </fxobject> <fxobject name="movieLister" class="org.fxobjects.samples.fxcontainer.MovieLister"> <property name="finder" ref="movieFinder"/> </fxobject> </fxcontainer>
  • 12. FxContainer: Import XML  Good for organizing large XML into smaller logical chunks <fxcontainer> <import resource="/org/fxobjects/samples/abc.xml"/> <fxobject name="movieFinder" class="org.fxobjects.samples.fxcontainer.MovieFinderImpl"> <property name="someVar" value="Some random value"/> <property name="javaHelper" ref="javaHelperObj"/> <property name="jfxHelper" ref="jfxHelperVarObj"/> </fxobject> </fxcontainer>
  • 13. FxContainer: Import Properties  Good for Spring style ${ } substitutions <fxcontainer> <import resource="/org/fxobjects/samples/abc.properties"/> <fxobject name="movieFinder" class="org.fxobjects.samples.fxcontainer.MovieFinderImpl"> <property name="someVar" value=“${svr1Name} is ${svr1Status}"/> <property name="javaHelper" ref=“${helperObj}"/> <property name="jfxHelper" ref="jfxHelperVarObj"/> </fxobject> </fxcontainer>  value and ref can be substituted
  • 14. FxContainer: Wired Object Attributes  Wired Objects in FxContainer have the following defaults (can be overridden)  Every wired object is singleton  Every wired object is lazily initialized (i.e on demand)  There is no order of initialization (Although load order can be specified for eagerly loaded objects)  Init-method is called after all properties are injected <fxcontainer> <fxobject name="movieFinder" class="org.fxobjects.samples.fxcontainer.MovieFinderImpl“ lazy- init=“false” load-order=“1” init-method=“someMethod”> .. </fxobject> </fxcontainer>
  • 15. FxContainer: Sequences <fxcontainer> <fxobject name="movieFinder" class="org.fxobjects.samples.fxcontainer.MovieFinderImpl"> <property name="movieCodes">  Primitive Sequence <sequence> <entry value=“1" /> <entry value="2" /> </sequence> </property> <property name="movies">  Object Sequence <sequence> <entry ref="movie1"/> <entry ref="movie2"/> </sequence> </property> </fxobject> </fxcontainer>
  • 16. FxContainer: Lists and Sets <fxcontainer> <fxobject name="movieFinder" class="org.fxobjects.samples.fxcontainer.MovieFinderImpl"> <property name="movieList">  Object LIST <list> (or <set>) <entry ref="movie1"/> <entry ref="movie2"/> </list> </property> <property name="movieCodes">  Primitive LIST <list valueClass=“java.lang.Integer”> <entry value=“1" /> <entry value="2" /> </list> </property> </fxobject> </fxcontainer>
  • 17. FxObjects Lists and Sets (Contd.)  Lists and Sets have to be initialized in their parents with a new …..();  valueClass attribute  Optional in most cases  Reason: One cannot tell from the xml value attributed if a list is Integer, String, BigDecimal etc.  Needed for Java and JavaFX objects when  value attribute is specified in xml AND  List is not parameterized (always the case in JavaFX)
  • 18. FxContainer: Maps <fxcontainer> <fxobject name="movieFinder" class="org.fxobjects.samples.fxcontainer.MovieFinderImpl"> <property name="movieMap">  Object MAP <map> <entry keyRef=“stage1” valueRef="movie1"/> </map> </property> <property name="movieCodes">  Primitive MAP <map keyClass=“java.lang.String” valueClass=“java.lang.Integer”> <entry key=“Terminator 1” value=“1" /> <entry value=“Terminator 2" /> </map> </property> </fxobject> </fxcontainer> keyClass and valueClass are needed for Maps in JavaFX with key and value specified. (since they cannot be parameterized) and
  • 19. FxContainer: Startup  Spring Like var loader = ClasspathXmlContainerLoader { resourceLocation: "/org/fxobjects/samples/my.xml“ } var container:FxContainer = loader.load(); var mvLister:MovieLister = container.getFxObject("movieLister") as MovieLister;  FxContainer can be used the IoC container with FxObjects or any other JavaFX application independently  Just include 2 jars – fxcontainer.jar and fxobjects-util.jar in classpath
  • 21. Links  Project site – https://fxobjects/dev.java.net  FxContainer is a subproject of FxObjects  Not a single person open source project  FxObjects and FxContainer developed and supported by ObjectSource (http://www.objectsource.com)  Download, use, extend, redistribute, OEM  Discussion Forums on project site  Participation welcome  Post ideas, questions, concerns, comments  Contribute code