You need to use <reference> in your blueprint to access your ds component.
Using <bean> creates a new instance, but doesn't wire it into the ds
infrastructure.

On Fri, 9 Jun 2023, 16:22 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.
>
>
>

Reply via email to