0

Am trying to develop a integration application with camel using JAVA DSL but when i try to autowire my connection helper i get a null value

Here is the code of :

import com.example.helperproject.ConnectionHelper;

@Singleton
@Startup
@ComponentScan(basePackages = {"com.example.helperproject"})
public class Bootstrap {

    @Autowired
    private ConnectionHelper connectionHelper;



    @PostConstruct
    public void init() throws Exception {

        try {
            System.out.println("Init process begin in singleton bootstrap");

            System.out.println(connectionHelper); 
            }

The connectionHelper prints a null value when we try to autowire with the help of spring when using JAVA DSL. Help me out with any Sample project and how to proceed further ?

Adding ConnectionHelper :

package com.example.helperproject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * ConnectionHelper to establish connection to Database
 * 
 *
 */
@Component
public class ConnectionHelper {

    @Autowired
    private PropertyHelper propertyHelper;

    /**
     * Method for getting host
     * 
     * @return host variable for connection
     */
    private String getHost() {
        String host = propertyHelper.getPropertyByName("host") == null ? "localhost"
                : propertyHelper.getPropertyByName("host");
        return host;

    }


    public void insertXMLDocument() {
        System.out.println("Test Insert");
        System.out.println("------------------>HOST :" + this.getHost());
    }
}
Vikram
  • 635
  • 1
  • 9
  • 29

1 Answers1

0

You should try adding a @Configuration annotation on Bootstrap class.

ComponentScan annotation are parsed by ComponentScanAnnotationParser which is called from ConfigurationClassParser. That scans only classes with @Configuration annotation. Essentially, your componentScan is not working in your code.

The documentation also suggests to use @ComponentScan with @Configuration

http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html

Edit 1: You can try putting the component scan in the XML and see if it works.

yaswanth
  • 2,349
  • 1
  • 23
  • 33
  • Even after adding @Configuration in the Bootstrap class it does not make any difference still getting the same null value. Can you let me know an another way to tackle this scenario.... – Vikram Feb 01 '17 at 18:01
  • Can you post the xml file you are using as well? Are you starting spring camel context? – yaswanth Feb 01 '17 at 18:22