Autowiring Spring beans inside Camel routes defined through Spring XML

16 views Asked by At

I am trying to inject a dummy spring bean inside a service defined in camel routes. It is basically a simple Spring Camel application demonstrating rest crud operations. Camel Routes are defined in Spring XML.

There is a service class which defines the crud operations. Now I want to Autowire a dummy bean inside this service class. The bean is defined with @Component annotation and not as a bean in XML file.

Result : Dummy bean is always null inside the service class.

Even if the Dummy bean is defined in Spring XML, the Autowired instance is always null.

But it works if we use @BeanInject instead of @Autowire for DummyBean in Service class along with the bean declaration in Spring XML.

How can the Dummy bean be autowired inside Service without defining in Spring XML ?

DummyBean.java

@Component
public class DummyBean {
    public String getMessage() {
        return "Testing Spring Context inside Camel Context";
    }
}

routes.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://camel.apache.org/schema/spring
                       http://camel.apache.org/schema/spring/camel-spring.xsd">

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <!-- Rest Configuration -->
    <restConfiguration bindingMode="json" component="servlet" port="8080">
        <dataFormatProperty key="prettyPrint" value="true"/>
    </restConfiguration>

    <!-- Define the REST API -->
    <rest id="userRestApi" path="/users" produces="application/json" consumes="application/json">
        <description>Rest Apis for User List Access</description>

        <!-- Get All Users -->
        <get>
            <description>Get All Users</description>
            <responseMessage code="200" message="All users returned"/>
            <to uri="bean:userService?method=getAllUsers"/>
        </get>
    </rest>

    <!-- Route for updating user -->
    <route id="updateUserRoute">
        <from uri="direct:updateUser"/>
        <to uri="bean:userService?method=updateUser"/>
    </route>

</camelContext>

<bean id="userService" class="com.gosemathraj.springdsl.usercrudrestapi.service.UserServiceImpl"/>

</beans>

UserServiceImpl.java

@Service("userService")
public class UserServiceImpl {

    @Autowired
    DummyBean dummyBean;

    private final Map<Integer, User> users = new TreeMap<>();

    public UserServiceImpl() {
        users.put(1, new User(1, "John Coltrane"));
        users.put(2, new User(2, "Miles Davis"));
        users.put(3, new User(3, "Sonny Rollins"));
    }

    @Override
    public List<User> getAllUsers() {
        return new ArrayList<>(users.values());
    }

    @Override
    public User getUser(Integer id) {
        return users.get(id);
    }
}

MainApplication.java

@SpringBootApplication
public class UserCrudRestApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserCrudRestApiApplication.class, args);
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/usercrudrestapi/user-crud-rest-api.xml");
    }
}
0

There are 0 answers