You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Ron Cecchini <ro...@comcast.net> on 2019/08/26 15:23:17 UTC

Spring configuring a bean

I apologize ahead of time, because I feel like I've taken a few steps backward and am now hung up on a simple newbie issue regarding application properties and Spring configuring beans.

Backstory: I've now created about a dozen Camel projects, all reusing the same basic "architecture" I set up a long time ago and the way I imported some custom Spring files and properties.

But now I'm having a frustrating newbie problem getting the Spring Boot app to see any bean configurations in a "default" Spring or Camel XML file.

I'm sure it's likely a classpath issue or something.  But in case it's something more, I willingly embarrass myself and ask you all...

Let's make it real simple:

In IntelliJ, create a new project using the archetype: camel-archetype-spring-boot.

You get the file structure:

.
├── java
│   └── org
│       └── apache
│           └── camel
│               └── archetypes
│                   ├── MySpringBean.java
│                   ├── MySpringBootApplication.java
│                   └── MySpringBootRouter.java
└── resources
    ├── application.properties
    └── META-INF
        ├── LICENSE.txt
        └── NOTICE.txt


I now comment out the Spring-injected @Value("${greeting}") in MySpringBean and am trying to replace it with a simple camel-context.xml where I Spring configure the 'say' class variable containing the "greeting".

-----

camel-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   [...blah blah blah...]

    <bean id="myBean" class="org.apache.camel.archetypes.MySpringBean">
        <property name="say" value="Hey There"/>
    </bean>

</beans>

-----

MySpringBean.java:

@Component("myBean")
public class MySpringBean {

//    @Value("${greeting}")
    private String say;

    public void setSay(String say) {
        this.say = say;
    }

    public String saySomething() {
        System.out.println("*** say = " + say);
        return say;
    }
}

-----

When I run MySpringBootApplication all I get is:

*** say = null
*** say = null
*** say = null
etc

FWIW, I tried naming the file both "camel-context.xml" and "camelContext.xml" as well as putting the files under "resources" and "resources/spring".  But none of those 4 configurations works.

Thanks for any help.

(I am now positioning my palm directly in front of my space so I can smack myself once my simple error is pointed out.....)

Ron

Re: Spring configuring a bean

Posted by Mark Nuttall <mk...@gmail.com>.
if you have the bean defined in XML and also the one in Java, then what is
happening is the Java one is "winning" and say is never set.

On Mon, Aug 26, 2019 at 11:24 AM Ron Cecchini <ro...@comcast.net>
wrote:

> I apologize ahead of time, because I feel like I've taken a few steps
> backward and am now hung up on a simple newbie issue regarding application
> properties and Spring configuring beans.
>
> Backstory: I've now created about a dozen Camel projects, all reusing the
> same basic "architecture" I set up a long time ago and the way I imported
> some custom Spring files and properties.
>
> But now I'm having a frustrating newbie problem getting the Spring Boot
> app to see any bean configurations in a "default" Spring or Camel XML file.
>
> I'm sure it's likely a classpath issue or something.  But in case it's
> something more, I willingly embarrass myself and ask you all...
>
> Let's make it real simple:
>
> In IntelliJ, create a new project using the archetype:
> camel-archetype-spring-boot.
>
> You get the file structure:
>
> .
> ├── java
> │   └── org
> │       └── apache
> │           └── camel
> │               └── archetypes
> │                   ├── MySpringBean.java
> │                   ├── MySpringBootApplication.java
> │                   └── MySpringBootRouter.java
> └── resources
>     ├── application.properties
>     └── META-INF
>         ├── LICENSE.txt
>         └── NOTICE.txt
>
>
> I now comment out the Spring-injected @Value("${greeting}") in
> MySpringBean and am trying to replace it with a simple camel-context.xml
> where I Spring configure the 'say' class variable containing the "greeting".
>
> -----
>
> camel-context.xml:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <beans xmlns="http://www.springframework.org/schema/beans"
>    [...blah blah blah...]
>
>     <bean id="myBean" class="org.apache.camel.archetypes.MySpringBean">
>         <property name="say" value="Hey There"/>
>     </bean>
>
> </beans>
>
> -----
>
> MySpringBean.java:
>
> @Component("myBean")
> public class MySpringBean {
>
> //    @Value("${greeting}")
>     private String say;
>
>     public void setSay(String say) {
>         this.say = say;
>     }
>
>     public String saySomething() {
>         System.out.println("*** say = " + say);
>         return say;
>     }
> }
>
> -----
>
> When I run MySpringBootApplication all I get is:
>
> *** say = null
> *** say = null
> *** say = null
> etc
>
> FWIW, I tried naming the file both "camel-context.xml" and
> "camelContext.xml" as well as putting the files under "resources" and
> "resources/spring".  But none of those 4 configurations works.
>
> Thanks for any help.
>
> (I am now positioning my palm directly in front of my space so I can smack
> myself once my simple error is pointed out.....)
>
> Ron
>