Red5Pro: Autowiring custom dependencies in custom webapp

24 views
Skip to first unread message

Herb Jellinek

unread,
Oct 25, 2018, 4:37:27 PM10/25/18
to red5
Hi all,
I've written a custom webapp that looks like so:

package my.package.webapp;

import my.package.io.MyUploader;

public class Notifier extends MultiThreadedApplicationAdapter {

    private final Logger log = Red5LoggerFactory.getLogger(getClass());

    private MyUploader uploader;

    @Override
public boolean appStart(final IScope app) {
        log.info("uploader = {}", uploader);
    }

    ...

}

I've got a JAR file, MyJar.jar, created as part of a separate project, that includes the class my.package.io.MyUploader.  The JAR contains a Spring app, and MyUploader is a singleton, annotated as a @Service.

I've added MyJar.jar to the webapp's pom.xml as a provided dependency, and the webapp compiles as I'd expect.

I copied MyJar.jar to my Red5Pro server's lib directory.

=> I want Notifier to have its uploader field set to an instance of my.package.io.MyUploader at startup using Spring dependency injection

I edited the webapp's red5-web.xml file and added various <bean .../> declarations like so:

<bean id="uploader" class="my.package.io.Uploader" autowire="byType"/>

I also tried omitting the autowire declaration, tried using byName, and more.

What I expect:

The appStart method runs.
The log shows

uploader = my.package.io.MyUploader@xxxxxx

What actually happens:

The appStart method runs.
The log shows

uploader = null

My Question:

How do I arrange it so my Notifier class gets a MyUploader instance injected into its uploader field?

I normally use Spring with annotation-based dependency management, so I may have overlooked something obvious in my XML.

Thanks,

    Herb

Rajdeep Rath

unread,
Oct 25, 2018, 4:48:27 PM10/25/18
to red5in...@googlegroups.com
Don't use autowire. Create a property in your Notifier class of type Uploader. Create getter setter for it .


In red5-web.xml instantiate your component as you have shown. Then pass the reference to your Notifier (web.handler) like so:

 <property name="uploader" ref="uploader" />


Where bean I'd is uploader and also your property in Notifier class is called uploader.

--

---
You received this message because you are subscribed to the Google Groups "red5" group.
To unsubscribe from this group and stop receiving emails from it, send an email to red5interest...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Herb Jellinek

unread,
Oct 25, 2018, 5:15:55 PM10/25/18
to red5
Excellent - thanks for the quick reply.  For the benefit of others coming across this later, here's what I did:

- Added public getUploader and setUploader methods to my.package.Notifier.

- Modified red5-web.xml like so:

<bean id="uploader" class="my.package.io.MyUploader"/>

<bean id="web.handler" class="my.package.Notifier">

<property name="uploader" ref="uploader" />
</bean>

And now the uploader field contains a MyUploader instance by the time appStart runs.

        Herb
Reply all
Reply to author
Forward
0 new messages