1

I'm new to Spring boot, I have developed my first application which received data from XML and queries Oracle DB, JPA.

I'm getting the following error:

[main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inlandPricingRestController': Unsatisfied dependency expressed through field 'inlandPricingService': Error creating bean with name 'inlandPricingServiceImpl': Unsatisfied dependency expressed through field 'inlandPricingDAO': Error creating bean with name 'inlandPricingDAO': Cannot create inner bean '(inner bean)#1386313f' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager';
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1386313f': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined;
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inlandPricingDAO': Cannot create inner bean '(inner bean)#1386313f' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager';
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1386313f': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined;
 nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inlandPricingServiceImpl': Unsatisfied dependency expressed through field 'inlandPricingDAO': Error creating bean with name 'inlandPricingDAO': Cannot create inner bean '(inner bean)#1386313f' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager';
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1386313f': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined;
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inlandPricingDAO': Cannot create inner bean '(inner bean)#1386313f' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager';
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1386313f': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inlandPricingRestController': Unsatisfied dependency expressed through field 'inlandPricingService': No qualifying bean of type [com.inlandpricinglocationauditor.dao.InlandPricingService] found for dependency [com.inlandpricinglocationauditor.dao.InlandPricingService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.inlandpricinglocationauditor.dao.InlandPricingService] found for dependency [com.inlandpricinglocationauditor.dao.InlandPricingService]:

Here're my project files:

Application.java:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

InlandPricingRestController.java:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class InlandPricingRestController {

    @Autowired
    InlandPricingService inlandPricingService;

    @RequestMapping(path = "/inlandlocation", method = RequestMethod.GET)
    public String hello() {
        return "Hello Mudassir!";
    }

    @RequestMapping(path = "/inlandlocationPOSTtest1", method = RequestMethod.POST, consumes = { "application/xml" })
    public InlandDBStructure postMethodXMLreturnXML1(
            @RequestBody InlandDBStructure list) {

        InlandDBStructure InlandCY = list;
        System.out.println("SOP : post Method started . Name "+ InlandCY.geteffective_dt());
        return InlandCY;
    }

    @RequestMapping(path = "/inlandlocationReal", method = RequestMethod.POST, consumes = { "application/xml" })
    public String postMethodXMLreturnXML2(@RequestBody InlandDBStructure list) {

        InlandDBStructure InlandCY = list;
        System.out.println("SOP : post Method started . Name "+ InlandCY.geteffective_dt());
        return inlandPricingService.findByGeoIDEffectiveExpiryDate(InlandCY.gethub_geoid(), InlandCY.geteffective_dt(),InlandCY.getexpiry_dt());

    }

}

InlandDBStructure.java:

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/*public class InlandDBStructure {

}
*/


@XmlRootElement(name = "InlandDBStructure")
public class InlandDBStructure {  
    private String hub_geoid;
    private String effective_dt;
    private String expiry_dt;
    private String last_updated_dt;

    public InlandDBStructure() {}  
    public InlandDBStructure(String hUB_GEOID, String eFFECTIVE_DT, String eXPIRY_DT, String lAST_UPDATED_BY) {  
        super();  
        this.hub_geoid = hUB_GEOID;  
        this.effective_dt = eFFECTIVE_DT;  
        this.expiry_dt = eXPIRY_DT;
        this.last_updated_dt = lAST_UPDATED_BY;
    }
    @XmlAttribute 
    public String gethub_geoid() {
        return hub_geoid;
    }
    public void sethub_geoid(String hUB_GEOID) {
        hub_geoid = hUB_GEOID;
    }
    @XmlElement 
    public String geteffective_dt() {
        return effective_dt;
    }
    public void seteffective_dt(String eFFECTIVE_DT) {
        effective_dt = eFFECTIVE_DT;
    }
    @XmlElement 
    public String getexpiry_dt() {
        return expiry_dt;
    }
    public void setexpiry_dt(String eXPIRY_DT) {
        expiry_dt = eXPIRY_DT;
    }
    @XmlElement 
    public String getlast_updated_dt() {
        return last_updated_dt;
    }
    public void setlast_updated_dt(String lAST_UPDATED_BY) {
        last_updated_dt = lAST_UPDATED_BY;
    }  


}

InlandPricingDAO.java:

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface InlandPricingDAO extends CrudRepository<InlandPricingDB, Long>{

    @Query(value="SELECT HUB_GEOID from INLAND_CY where HUB_GEOID=:hubgeoid AND EFFECTIVE_DT <= TO_DATE(:effectivedate,'dd-mm-yyyy') AND EXPIRY_DT >= TO_DATE(:exiprydate,'DD-MM-YYYY')",nativeQuery = true)
    String findByGeoIDEffectiveExpiryDate(@Param("hubgeoid") String hubgeoid,@Param("effectivedate") String effectivedate ,@Param("exiprydate") String exiprydate);


}

InlandPRicingDetailRepository.java:

public interface InlandPricingDetailRepository  extends JpaRepository<InlandPricingDB, Long>{

}

InlandPricingService.java:

public interface InlandPricingService {
    List<InlandPricingDB> getAllDetails();
    String findByGeoIDEffectiveExpiryDate(String hubgeoid, String effectivedate, String exiprydate);
}

InlandPricingServiceImpls.java:

@Service
public class InlandPricingServiceImpl implements InlandPricingService{

    public InlandPricingServiceImpl() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Autowired
    private DataSource dataSource;

    @Autowired
    private InlandPricingDAO inlandPricingDAO;

    @Autowired
    private InlandPricingDetailRepository inlandPricingDetailRepository;

    @Override 
    public String findByGeoIDEffectiveExpiryDate(String hubgeoid, String effectivedate, String exiprydate) {
        return inlandPricingDAO.findByGeoIDEffectiveExpiryDate(hubgeoid,effectivedate,exiprydate);
    }

     @Override               
        public List<InlandPricingDB> getAllDetails() {
            return inlandPricingDetailRepository.findAll();
        }

     public InlandPricingDetailRepository getInlandPricingDetailRepository() {
            return inlandPricingDetailRepository;
        }

        public void setInlandPricingDetailRepository(InlandPricingDetailRepository inlandPricingDetailRepository) {
            this.inlandPricingDetailRepository = inlandPricingDetailRepository;
        }

        public InlandPricingDAO getInlandPricingDAO() {
            return inlandPricingDAO;
        }

        public void setInlandPricingDAO(InlandPricingDAO inlandPricingDAO) {
            this.inlandPricingDAO = inlandPricingDAO;
        }       

        public DataSource getDataSource() {
            return dataSource;
        }

        public void setDataSource(DataSource dataSource) {
            this.dataSource = dataSource;
        }
}

InlandPricingDB.java:

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.springframework.data.annotation.Id;


/**
 * The persistent class for the INLANCY_CY VIP database table.
 * 
 */

@Entity
@Table(name = "INLAND_CY")
public class InlandPricingDB {
       @Id
        @Column(name = "HUB_GEOID")
        private String hub_geo_id;

        @Column(name = "EFFECTIVE_DT")
        private Date effective_dt;

        @Column(name = "EXPIRY_DT")
        private Date expitry_dt;

        @Column(name = "LAST_UPDATED_BY")
        private String last_updated_by;

        public String getHub_geo_id() {
            return hub_geo_id;
        }

        public void setHub_geo_id(String hub_geo_id) {
            this.hub_geo_id = hub_geo_id;
        }

        public Date getEffective_dt() {
            return effective_dt;
        }

        public void setEffective_dt(Date effective_dt) {
            this.effective_dt = effective_dt;
        }

        public Date getExpitry_dt() {
            return expitry_dt;
        }

        public void setExpitry_dt(Date expitry_dt) {
            this.expitry_dt = expitry_dt;
        }

        public String getLast_updated_by() {
            return last_updated_by;
        }

        public void setLast_updated_by(String last_updated_by) {
            this.last_updated_by = last_updated_by;
        }
}

Application.properties

server.port=9000

spring.unbilled.read.datasource.url=jdbc:oracle:thin:@localhost:1521/xe
spring.unbilled.read.datasource.username=test
spring.unbilled.read.datasource.password=test
spring.unbilled.read.datasource.driver-class-name=oracle.jdbc.OracleDriver

What am I doing wrong?

Shaikh Mudassir
  • 141
  • 1
  • 6
  • 18
  • Did you specify connection properties for Spring Data in `application.properties`? – questionaire Mar 23 '18 at 13:55
  • I think issue is when Spring boot is trying to inject InlandPricingService in the rest Controller, are these classes in different folders ? if yes, use @SpringBootApplication(scanBasePackages = {"pkg1", "pkg2"} – JaisAnkit Mar 23 '18 at 14:00
  • Possible duplicate of: https://stackoverflow.com/questions/24520602/spring-data-jpa-no-bean-named-entitymanagerfactory-is-defined-injection-of-a If this is the case we can close, but I'm not 100% sure since we're missing a lot of setup. – Compass Mar 23 '18 at 14:13
  • I have tried adding the scan packages in application.java but it also didnt work . i have updated the question with application.property file. – Shaikh Mudassir Mar 23 '18 at 14:21
  • Move the `Application` class to the `com.inlandpricinglocationauditor` packages (assuming everything else is now in sub packages of that). – M. Deinum Mar 23 '18 at 14:47

3 Answers3

2

In spring boot your main class should be the outermost package and your beans can be in sub packages like controller, service, repository. I see that you are missing a default constructor on your controller. Spring boot has a ridiculous dependency on it. I am just guessing since I have got such an error in the past . However spring recommends constructor injection for must have beans. If you have too many must haves use setter injection with @Required annotations.

Harish
  • 3,343
  • 15
  • 54
  • 75
1

Ideally bean injection using constructor injection should work.Try this also @SpringBootApplication(scanBasePackages = {"pkg1", "pkg2"} if not working share your package/folder structure .. That would help to analyse further.

Sanjay A
  • 316
  • 3
  • 15
0

move the package of your InlandPricingDB.java class into the package of Application class for to make that inner.

sorry I can help you well in french ,i don't now well english

carrow
  • 43
  • 1
  • 7
  • moving InlandPricingDB.java class into the package of Application class also didnt work, thanks by the way for your prompt reply – Shaikh Mudassir Mar 23 '18 at 14:29