Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

onLoad() event.

Add onLoad() to register a listener.
Add onLoadEvent() to fire all onLoad listeners.
Allow injectProperties() to take a type name as well as a bean name.
  • Loading branch information...
commit 771a2dd6b343992479bc5c97a0208c76eabc428c 1 parent 54c2cc7
Sean Corfield authored
10 Application.cfc
View
@@ -12,6 +12,7 @@ component extends="org.corfield.framework" {
var svcPath = expandPath( 'services' );
var bf = new ioc( '/model, #svcPath#', { transients = [ 'fish' ] } );
bf.setParent( xbf );
+ bf.onLoad( variables.loader );
setBeanFactory( bf );
// programmatically declare another bean:
@@ -20,4 +21,11 @@ component extends="org.corfield.framework" {
// used to track creation of transient beans for illustration purposes:
structDelete( application, 'itemCount' );
}
-}
+
+ function loader( any ioc ) {
+ var bf = new ioc( '' );
+ bf.addBean( 'config', '/some/xml/file.xml' );
+ bf.declareBean( 'configObject', 'declared.things.myconfig' );
+ ioc.addBean( 'myconfig', bf.injectProperties( 'configObject', { name = "MyConfig" } ) );
+ }
+}
10 declared/things/myconfig.cfc
View
@@ -0,0 +1,10 @@
+component accessors=true {
+ property string name;
+ function init( any config ) {
+ variables.config = config;
+ }
+ function explain() {
+ writeDump( var = variables.config, label = "MyConfig.config" );
+ writeDump( var = variables.name, label = "MyConfig.name" );
+ }
+}
30 ioc.cfc
View
@@ -23,6 +23,7 @@ component {
variables.beanInfo = { };
variables.beanCache = { };
variables.autoExclude = [ '/WEB-INF', '/Application.cfc' ];
+ variables.onLoad = 0;
setupFrameworkDefaults();
return this;
}
@@ -129,9 +130,13 @@ component {
}
- // given a bean (by name or by value), call the named setters with the specified property values
+ // given a bean (by name, by type or by value), call the named
+ // setters with the specified property values
public any function injectProperties( any bean, struct properties ) {
- if ( !isSimpleValue( bean ) ) bean = getBean( bean );
+ if ( isSimpleValue( bean ) ) {
+ if ( containsBean( bean ) ) bean = getBean( bean );
+ else bean = createObject( 'component', bean );
+ }
for ( var property in properties ) {
var args = { };
args[ property ] = properties[ property ];
@@ -147,13 +152,24 @@ component {
// if you reload the parent, you must reload *all* child factories to ensure
// things stay consistent!)
public any function load() {
+ var discovered = structKeyExists( variables, 'discoveryComplete' );
discoverBeans( variables.folders );
variables.beanCache = { };
for ( var key in variables.beanInfo ) {
if ( variables.beanInfo[ key ].isSingleton ) getBean( key );
}
+ if ( discovered ) onLoadEvent();
return this;
}
+
+
+ // add a listener for processing after a (re)load of the factory
+ // called with just the factory, should be a plain function
+ public any function onLoad( any listener ) {
+ var head = { next = variables.onLoad, listener = listener };
+ variables.onLoad = head;
+ return this;
+ }
// set the parent bean factory
@@ -259,6 +275,7 @@ component {
}
variables.discoveryComplete = true;
}
+ onLoadEvent();
}
@@ -362,6 +379,15 @@ component {
logMissingBean( beanName, resolvingBeanName );
}
}
+
+
+ private void function onLoadEvent() {
+ var head = variables.onLoad;
+ while ( isStruct( head ) ) {
+ head.listener( this );
+ head = head.next;
+ }
+ }
private any function resolveBean( string beanName ) {
1  views/main/default.cfm
View
@@ -14,4 +14,5 @@
<cfdump var="#bf.getBean('userfish').product.getUserService()#" label="bf.getBean('userfish').product.getUserService()"/>
<cfdump var="#bf.getBean('magicvalue')#" label="bf.getBean('magicvalue')"/>
<cfdump var="#bf.getBean('item')#" label="bf.getBean('item') - transient"/>
+<cfset bf.getBean('myconfig').explain() />
<cfdump var="#bf.getBeanInfo()#"/>
Please sign in to comment.
Something went wrong with that request. Please try again.