[coldbox 4.2.0] Switching from ColdSpring to ColdBox

We are in the process of switching from ColdSpring to Coldbox as we start rebuilding an application. In ColdSpring, we’re using constructor-arg’s to instantiate beans.

<bean id="“AuthorizationService” class=“com.security.AuthorizationService”>

Forgive the newbie question, but how do I go about implementing this in ColdBox? I’ve been using the following:

AuthorizationService = getModel(‘com.shortstravel.security.AuthorizationService’);

…in the main handler, but it keeps complaining that “The USERMANAGER parameter to the init function is required but was not passed in”.

In my AuthorizationService.cfc, I have:

So I’m stuck on what I’m doing incorrectly here.

Any help would be appreciated.

Thanks!

Hi David,

You can pass init arguments like

// Map with constructor arguments
map("transfer")
    .to("transfer.com.Transfer")
    .into(SCOPES.SINGLETON)
    .noAutowire()
    .asEagerInit()
    .initArg(name="configuration",ref='transferConfig');

As a clarifying note, ColdSpring is an IOC container/object factory. ColdBox is an MVC framework. It’s actually possible to use ColdSpring with ColdBox. I wouldn’t recommend it for many reasons, but they’re two separate concerns.

Now, that said, ColdBox comes bundled with WireBox, which does everything ColdSpring does and a lot more. Instead of using XML, WireBox can be configured programmatically via code using our binder DSL like Sana showed below. His example creates a mapping (like a recipe for how to create a CFC) called “transfer” that points to the CFC path given and passes in another mapping.

Another alternative to the mapping DSL is to put all of that metadata right in your CFC. Just add an “inject” annotation on each constructor arg that specifies what you want WireBox to pass in for those args and it will do so automatically when creating the instance. Here’s an ex:

component {
/**

  • @myservice.inject model:MyService
  • @foo.inject anotherMappingIDToInject
  • @mySetting.inject coldbox:settiing:mysetting
    */
    function init( required myService, required foo, required mySetting ) {
    variables.myService = arguments.myService;
    variables.foo = arguments.foo;
    variables.mySetting = arguments.mySetting;
    }
    }

WireBox inspects the metadata of CFCs before it creates them which is how it finds these annotations.

My favorite injection method though is mixin injection (because constructor args are a pain). Just add cfproperties with an inject attribute and those dependencies will be injected right into the CFC for you so there’s no need to store them on init.

component {
// These are injected into the variables scope after init() is called.
property name=‘myservice’ inject=‘model:MyService’;
property name=‘foo’ inject=‘anotherMappingIDToInject’;
property name=‘mySetting’ inject=‘coldbox:settiing:mysetting’;
}

In addition the reading Sana gave you, here is a reference card to get you going with WireBox:
https://github.com/ColdBox/cbox-refcards/raw/master/WireBox/WireBox-Refcard.pdf

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thanks for the insight Brad and Sana!

My favorite injection method though is mixin injection (because constructor args are a pain). Just add cfproperties with an inject attribute and those dependencies will be injected right into the CFC for you so there’s no need to store them on init.

component {
// These are injected into the variables scope after init() is called.
property name=‘myservice’ inject=‘model:MyService’;
property name=‘foo’ inject=‘anotherMappingIDToInject’;
property name=‘mySetting’ inject=‘coldbox:settiing:mysetting’;
}

Okay… but is that not what I am doing? In my AuthorizationService.cfc I have the following at the top:

Is that not functionally equivalent to what you’re suggesting?

Cheers,

David Byers

Yes, but did you remove the required constructor args? In your first post you said that you had required constructor args and CF errored because they weren’t being supplied. Mixin injection could be a replacement for that all together, but you’ll have to remove the constructor args to get rid of that error if you’re going with mixin injection instead. (Well technically you can use both, but let’s ignore that fact for now).

Perhaps you can show the current full code you’re working with and the exact error your currently getting. I’m not entirely sure what your issues is right now.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

And looking again at your code below, I should add-- is the issue happening when Wirebox tries to build your security service, or is the problem with building the user manager? The entire process of building and wiring an object happens for each CFC, recursively down the rabbit hole until the entire dependency hierarchy is built (or at the least the subtree that you’re requesting). It’s entirely possible that your security service is fine, and the actual error is happening when Wirebox tries to, in turn, create and wire up the user manager. This is where a complete error and stack trace would be immensely helpful.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com