CreateDynamicProxy

Description

Creates a dynamic proxy of the ColdFusion component that is passed to a Java library. Dynamic proxy lets you pass ColdFusion components to Java objects. Java objects can work with the ColdFusion components seamlessly as if they are native Java objects.

Returns

Metadata structure related to the current session.

History

ColdFusion (2018 release): Renamed the following parameters:

  • fullyQualifiedNameOfCFC  to cfc
  • interfaceName to interfaces

Category

Other functions

Function syntax

createDynamicProxy(cfc, [interfaces])

Parameters

Name

Description

cfc

Fully qualified name of the ColdFusion component or a CFC instance.

interfaces

An array of Java interfaces for which you want to create the dynamic proxy.

Example

The following example shows how to create a Java interface. Interface defines a method. A ColdFusion component implements the method as a ColdFusion function. The dynamic proxy of the ColdFusion component calls a Java class by passing the object of the interface. It then calls the method in ColdFusion as if it is a native Java class.

  1. Create a Java interface, MyInterface.

    public interface MyInterface 
    { 
    public String sayHello(); 
    }
  2. Compile the Java file and place it in a directory, lib.

  3. Create a Java class, InvokeHelloProxy, that calls the ColdFusion object using the instance of the interface.The constructor, InvokeHelloProxy, creates an object of MyInterface. The invokeHello() method calls the sayHello()method using the object.

    public class InvokeHelloProxy 
    { 
    private MyInterface myInterface; 
    public InvokeHelloProxy(MyInterface x) 
    { 
    this.myInterface = x; 
    } 
    public String invokeHello() 
    { 
    return myInterface.sayHello(); 
    } 
    }
  4. Compile the Java file and place it in a directory, lib.

  5. Create a CFC file, HelloWorld.cfc, that implements the method defined in the interface and save it in a directory, cfc.

    <cfcomponent> 
    <cffunction name="sayHello" returntype="string"> 
    <cfreturn "Hello World!!!!"> 
    </cffunction> 
    </cfcomponent>
  6. Add the following code in the Application.cfc.{{}}

    <cfset THIS.javaSettings = {LoadPaths = ["/lib"],reloadOnChange=true,watchInterval=10}/>
  7. Add a CFM file that creates a dynamic proxy for HelloWorld as interface, MyInterface. Create an object of InvokeHelloProxy class and initiate the class.The code creates a dynamic proxy of MyInterface.

    <cfset dynInstnace = createDynamicProxy("HelloWorld.cfc", ["MyInterface"])>  
    <cfset x = createObject("java","InvokeHelloProxy").init(dynInstnace)>  
    <cfset y = x.invokeHello()>  
    <cfoutput>#y#</cfoutput>

    Example: using a CFC instance

    <cfset instance=new helloWorld()>  
    <cfset dynInstnace = createDynamicProxy(instance, ["MyInterface"])>  
    <cfset x = createObject("java","InvokeHelloProxy").init(dynInstnace)>  
    <cfset y = x.invokeHello()>  
    <cfoutput>#y#</cfoutput>
  8. Deploy the application and access the CFM file.

Get help faster and easier

New user?