In the process of calling flex and Java, there are actually more flexible ways to improve system agility and greatly improve system coupling and development efficiency. Here I will continue to introduce Java Dynamic code calls in Agile calls.
First, let's look at the Java class (beanshell). Here, it mainly receives Java code for dynamic calling.
Package COM. shine. framework. beanshell; </P> <p> Import bsh. interpreter; </P> <p> public class beanshell {<br/> Public object executbeanshell (string code) {<br/> try {<br/> interpreter inter = new interpreter (); <br/> Inter. eval (CODE); <br/> return Inter. get ("result"); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/> return NULL; <br/>}< br/>}
Then configure this class to remoting-config.xml
<Destination ID = "beanshell"> <br/> <Properties> <br/> <source> COM. shine. framework. beanshell. beanshell </source> <br/> </Properties> <br/> </destination>
Write another static beanshell In the event code, in the singleton mode.
Package COM. shine. framework. beanshell <br/>{< br/> Import MX. RPC. events. faultevent; <br/> Import MX. RPC. events. resultevent; <br/> Import MX. RPC. remoting. mxml. remoteobject; <br/> Import MX. controls. alert; </P> <p> public class beanshell <br/>{< br/> Private Static Var _ instance: beanshell; </P> <p> private VaR method: function; </P> <p> Public Function beanshell (Enforcer: singletonenforcer) <br/>{< br/>}</P> <p> publi C static function getinstance (): beanshell <br/>{< br/> If (beanshell. _ instance = NULL) <br/>{< br/> beanshell. _ instance = new beanshell (New singletonenforcer (); <br/>}< br/> return beanshell. _ instance; <br/>}</P> <p> Public Function beanshell (Code: String, method: function = NULL ): void <br/>{< br/> This. method = method; <br/> var beanshell: remoteobject = new remoteobject; <br/> beanshell. destination = "beanshell"; <Br/> beanshell. addeventlistener (faultevent. fault, error); <br/> beanshell. endpoint = "messagebroker/AMF"; <br/> beanshell.exe cutbeanshell (CODE); <br/> beanshell. addeventlistener (resultevent. result, getromotehellores); <br/>}</P> <p> private function getromotehellores (E: resultevent): void <br/>{< br/> If (this. method! = NULL) {<br/> This. method. call (this, E. result); <br/>}</P> <p> private function error (E: faultevent ): void <br/> {<br/> alert. show (E. message. tostring (); <br/>}</P> <p> class singletonenforcer <br/>{< br/>}
Example of invocation:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <mx: canvas xmlns: MX = "http://www.adobe.com/2006/mxml" width = "400" Height = "300"> <br/> <mx: SCRIPT> <br/> <! -- [CDATA [<br/> Import COM. shine. framework. beanshell. beanshell; </P> <p> private function beanshelltest () {<br/> beanshell. getinstance (). beanshell ("system. out. println (/"test/"); "); <br/>}< br/>] --> <br/> </MX: SCRIPT> <br/> <mx: button label = "test" Click = "beanshelltest ()"/> <br/> </MX: canvas>
To call other classes:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <mx: canvas xmlns: MX = "http://www.adobe.com/2006/mxml" width = "400" Height = "300"> <br/> <mx: SCRIPT> <br/> <! -- [CDATA [<br/> Import COM. shine. framework. beanshell. beanshell; </P> <p> private function beanshelltest () {<br/> beanshell. getinstance (). beanshell ("Import test. test; test = new test (); system. out. println (test. show (); "); <br/>}< br/>] --> <br/> </MX: SCRIPT> <br/> <mx: button label = "test" Click = "beanshelltest ()"/> <br/> </MX: canvas> <br/>
This design is highly flexible, and some friends say it is not easy to maintain it. It depends on how you design it.