You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Claus Ibsen <cl...@gmail.com> on 2021/04/26 11:01:02 UTC

Re: Spring beans request scope

Hi

You need to configure spring to use prototype scoped for the bean, or
what else scope it may have for that.
eg on those spring annotations you use.

The camel request scope is only on the Camel side.


On Mon, Apr 26, 2021 at 12:03 PM Roberto Gasdia
<ro...@innoteam.it> wrote:
>
> I am struggling to understand how request scope beans work.
> I'm using camel 3.8.0 with javaconfig.
> I've made a simple test with rest component, here is the snippet:
>
> ------ bean -------
>
> @Component
> public class TestBean {
>     private String param = "";
>
>     public TestBean() {
>     }
>
>     public String getParam() {
>         return param;
>     }
>
>     public void init(@Body ParametriTestScope body) {
>         System.out.println("initial value " + param);
>         System.out.println("changing state to " + body.getParam());
>         param = body.getParam();
>     }
>
> }
>
>
> ------ bean spring configuration -------
>
> @Configuration
> public class TestBeanService {
>
>     private TestBean test = new TestBean();
>
>     @Bean("testBean")
>     public TestBean getTest() {
>         return test;
>     }
> }
>
> and finally the route under test
>
> @Component
> public class RottaTestScope extends RouteBuilder {
>
>     @Override
>     public void configure() {
>         rest("/testscope").post().type(ParametriTestScope.class)
>                 .to("direct:process_testscope");
>
>         from("direct:process_testscope")
>                 .to("bean:testBean?scope=Request")
>
> .setBody(simple("${bean:testBean?method=getParam&scope=Request}"));
>     }
>
> }
>
>
>
> I expect this behavior by calling the end point "
> http://localhost:8080/testscope"
> First postman call should log:
>
> initial value
> changing state to hello
>
> Second postman call should log:
>
> initial value
> changing state to hello
>
> On the second postman call it logs instead
>
> initial value hello
> changing state to hello
>
> which makes me believe that the bean is cached (default scope).
>
> What am I missing?
>
> --
>
> Roberto Gasdia
>
> Software Developer
>
> Innoteam - Digital Change
>
> Innoteam Srl - Via B. Bedosti, 21 - 61122 Pesaro
> Phone +39 02 4507 4709 - www.innoteam.it



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: Spring beans request scope

Posted by Roberto Gasdia <ro...@innoteam.it>.
following your hint I removed all spring references and replacing

.to("bean:testBean?scope=Request")

with

.bean(TestBean.class, BeanScope.Request)

I now have the expected behavior.
My goal is to instantiate a dataSource with request scope and reference it
in the sql component like this

.to("sql:classpath:sql/gsl/show_search_path.sql?dataSource=postgresDataSource")

but I don't know how to tell it to take the specific instance that I just
created.
Any suggestions?















Il giorno lun 26 apr 2021 alle ore 11:01 Claus Ibsen <cl...@gmail.com>
ha scritto:

> Hi
>
> You need to configure spring to use prototype scoped for the bean, or
> what else scope it may have for that.
> eg on those spring annotations you use.
>
> The camel request scope is only on the Camel side.
>
>
> On Mon, Apr 26, 2021 at 12:03 PM Roberto Gasdia
> <ro...@innoteam.it> wrote:
> >
> > I am struggling to understand how request scope beans work.
> > I'm using camel 3.8.0 with javaconfig.
> > I've made a simple test with rest component, here is the snippet:
> >
> > ------ bean -------
> >
> > @Component
> > public class TestBean {
> >     private String param = "";
> >
> >     public TestBean() {
> >     }
> >
> >     public String getParam() {
> >         return param;
> >     }
> >
> >     public void init(@Body ParametriTestScope body) {
> >         System.out.println("initial value " + param);
> >         System.out.println("changing state to " + body.getParam());
> >         param = body.getParam();
> >     }
> >
> > }
> >
> >
> > ------ bean spring configuration -------
> >
> > @Configuration
> > public class TestBeanService {
> >
> >     private TestBean test = new TestBean();
> >
> >     @Bean("testBean")
> >     public TestBean getTest() {
> >         return test;
> >     }
> > }
> >
> > and finally the route under test
> >
> > @Component
> > public class RottaTestScope extends RouteBuilder {
> >
> >     @Override
> >     public void configure() {
> >         rest("/testscope").post().type(ParametriTestScope.class)
> >                 .to("direct:process_testscope");
> >
> >         from("direct:process_testscope")
> >                 .to("bean:testBean?scope=Request")
> >
> > .setBody(simple("${bean:testBean?method=getParam&scope=Request}"));
> >     }
> >
> > }
> >
> >
> >
> > I expect this behavior by calling the end point "
> > http://localhost:8080/testscope"
> > First postman call should log:
> >
> > initial value
> > changing state to hello
> >
> > Second postman call should log:
> >
> > initial value
> > changing state to hello
> >
> > On the second postman call it logs instead
> >
> > initial value hello
> > changing state to hello
> >
> > which makes me believe that the bean is cached (default scope).
> >
> > What am I missing?
> >
> > --
> >
> > Roberto Gasdia
> >
> > Software Developer
> >
> > Innoteam - Digital Change
> >
> > Innoteam Srl - Via B. Bedosti, 21 - 61122 Pesaro
> > Phone +39 02 4507 4709 - www.innoteam.it
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
>


-- 

Roberto Gasdia

Software Developer

Innoteam - Digital Change

Innoteam Srl - Via B. Bedosti, 21 - 61122 Pesaro
Phone +39 02 4507 4709 - www.innoteam.it