Parameter injection with @Value

January 24, 2010

I recently added support to @Value annotation to the ServiceContainerAnnotationsLoader. See my previous post for detailed information about this loader for Symfony Dependency Injection component.

Inspired by Spring Framework 3.0 new equivalent annotation, it allows to inject parameter values defined in your service container with a simple annotation in your services. Check the updated code on my Bitbucket.

If you use @Value without description, the parameter identifier will be automatically determined. You can also explicitly define the parameter identifier: @Value %mail.transport%.

    foo: bar
    mailer.username: foo
    mailer.password: bar
 * @Service 
class Default_Service_TestService 
     * @Value
    protected $_foo; 
    public function setFoo($foo) 
        $this->_foo = $foo; 
        return $this; 

    protected $_username;

     * @Value %mailer.username%
    public function setUsername($username)
        $this->_username = $username;
        return $this;

    // ....

4 Responses to “Parameter injection with @Value”

  1. André Says:

    Hey thanks for these posts! I’m trying to integrate your libraries in our project and so far the @value injection does not work, seems like the @value tags are never parsed. After several hours trying to make it work, I’m wondering if I did something wrong or if there is a bug in the classes. Cheers!

    • Hello André, please note that the annotation is case sensitive: @Value annotation must be used and not @value. If it does not resolve your problem, feel free to provide some code you’re using to let me help you.

      • André Says:

        Well I’m using your example on bitbucket without any modification. The @Value doesn’t seem to be parsed on the setFoo($foo) in the Default_Service_TestService class so the injection is not made and I end up with no value in $_foo. The same happens with the @Inject on the setTestService2($testService2) function. Any ideas? Thanks!

  2. It seems that your service is not loaded in the container with the annotation loader. Did you clone the whole project? If not, you need to follow my previous post to integrate the Symfony Service Container with your Zend Framework application and as I said at the end you need to use my autoloader resource to automatically load your services classes into the container with the annotation loader.

    I didn’t wrote the post about this last step but have a look at my bitbucket and particularly at these two classes: LoSo_Zend_Application_Resource_Autoloader and LoSo_Zend_Loader_Autoloader_Resource. Hope this will help and do not hesitate if you encounter difficulties again.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: