Hi JB!Yes, I could make it work after I changed the <bean> declaration for
<reference>. I didn't know this was possible, is there an "official" (or not so
official) documentation that explains DS, dependency injection in Karaf, and
how to use it in a blueprint?
Thanks!
El sábado, 10 de junio de 2023, 01:54:23 ART, Jean-Baptiste Onofré
<[email protected]> escribió:
Hi
In blueprint, a bean (<bean/>) is searched inside the blueprint container.
If you want to use a service declared outside of the blueprint
container (DS in your case), you have to use <reference/>: it will
create a bean proxying the OSGi service available in the OSGi registry
(registered with OSGi "classic" code, DS, blueprint, whatever).
Regards
JB
On Fri, Jun 9, 2023 at 5:21 PM z8sbk.yahoo.com.ar via user
<[email protected]> wrote:
>
> Hi,
> I'm trying to understand how declarative service works. In a bundle, I have
> these two clasess, very basic:
>
> @Component
> public class App implements IApp {
>
> @Reference
> private IConf conf;
>
> public App() {}
>
> public void doIt() {
>
> System.out.println("foo=" + conf.getFoo());
>
> }
>
> }
>
> @Component(name = "Conf", immediate = true, configurationPid = "confByDS")
> public class Conf implements IConf {
>
> private String foo = "bar";
>
>
> public Conf() {}
> @Override
>
> public String getFoo() {
>
> return foo;
>
> }
>
> @Activate
> @Modified
> public void activate(ComponentContext context) {
>
> foo = context.getProperties().get("foo") != null ? (String)
> context.getProperties().get("foo") : this.foo;
>
> }
>
> }
>
> Then, I have this blueprint/camel route:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
>
> <bean id="app" class="com.app.App" />
> <camelContext xmlns="http://camel.apache.org/schema/blueprint">
> <route>
> <from uri="timer:simple?period=5000"/>
> <to uri="bean:app?method=doIt"/>
> </route>
> </camelContext>
>
> </blueprint>
>
> After deploying the bundle, when I put the blueprint in /deploy, throws a
> NullPointerException in conf.getFoo(), which means that the Conf reference is
> not injected:
>
> Message History
> ---------------------------------------------------------------------------------------------------------------------------------------
> RouteId ProcessorId Processor
> Elapsed (ms)
> [route3 ] [route3 ] [timer://simple?period=5000
> ] [ 7]
> [route3 ] [to2 ] [bean:app?method=doIt
> ] [ 0]
>
> Stacktrace
> ---------------------------------------------------------------------------------------------------------------------------------------
> java.lang.NullPointerException: null
> at com.app.App.doIt(App.java:17) ~[!/:?]
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>~[?:1.8.0_321]
> at
>sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>~[?:1.8.0_321]
> at
>sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[?:1.8.0_321]
>
> I think I'm misunderstanding how the DI in Karaf works plus DS, could you
> please tell me what am I missing? Does DS work with Camel XML DSL routes?
>
> Thanks in advanced.
> Nick.
>
>