0

I have the following code in my DAO:

@Repository
public class PedidoDAO {

    private static final Logger logger = Logger.getLogger(PedidoDAO.class);

    @PersistenceContext
    private EntityManager manager;

    public List<Pedido> getPedidos() throws SQLException {

        List<Pedido> listPedidos = new ArrayList<Pedido>();

        try {

            listPedidos = manager.createQuery("select e from Pedido e", Pedido.class).getResultList();

        } catch (NoResultException e) {

            logger.error("Data does'nt exist!");

        } catch (Exception e) {

            logger.error("Error creating HQL to get Pedidos: ", e);
        }

        return listPedidos;
    }
}

Until here everything is working fine, if I call this DAO in a Controller(that already has @Transactional) it works fine.

But when I create an @Service like here:

@Transactional
@Service
public class PedidoBU {

    @Autowired
    private PedidoDAO pedidoDAO;

    public List<Pedido> getPedidos() throws SQLException{
        return pedidoDAO.getPedidos();
    }

}

It don't wire the DAO and pedidoDAO.getPedidos() throws an NullPointerException ever.

What Am I doing wrong in the Service? Am I understanding wrong the EntityManager and Transactional controller?

My EntityManager Configuration in the servlet:

<context:component-scan base-package="com.nassoft.erpweb"/>

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/my_database" />
    <property name="user" value="" />
    <property name="password" value="" />

    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="20" />
    <property name="maxStatements" value="50" />
    <property name="idleConnectionTestPeriod" value="3000" />
    <property name="loginTimeout" value="300" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" autowire-candidate="default">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven/>

Edited.

My controller that call the @Service:

@Controller
public class PedidoVendaController {

    @Autowired
    private PedidoBU pedidoBU;

    public String listPedidoVenda(){
       listPedidos = pedidoBU.getPedidos();
    }
}

First problem was solved autowiring the pedidoBU.

But another error is appearing when I start the server:

2015-02-24 13:20:14 WARN  XmlWebApplicationContext:489 - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pedidoVendaController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.nassoft.erpweb.register.business.PedidoBU com.nassoft.erpweb.register.controller.PedidoVendaController.pedidoBU; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nassoft.erpweb.register.business.PedidoBU] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5123)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5407)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.nassoft.erpweb.register.business.PedidoBU com.nassoft.erpweb.register.controller.PedidoVendaController.pedidoBU; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nassoft.erpweb.register.business.PedidoBU] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:522)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
    ... 29 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nassoft.erpweb.register.business.PedidoBU] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:494)

Second problem solved: I was trying to use a @Service inside another @Service in another point of my code without use Interfaces. When I changed to use a Interface its called the main class as a Proxy and works correctly.

  • 1
    how do you inject `PedidoBU` service into your controller? npe suggests it is not treated as spring bean, otherwise you would get exception on initialization of your controller – Vladimir Feb 24 '15 at 15:55
  • In what packages are both classes? – Javi Mollá Feb 24 '15 at 15:55
  • and both classes are in package `com.nassoft.erpweb`? – Jens Feb 24 '15 at 15:55
  • Jens the null pointer isnt thrown by getPedidos - its the DAO thats null – farrellmr Feb 24 '15 at 15:55
  • @JavierMollá the packages are in com.nassoft.erpweb.register.business (my services) and com.nassoft.erpweb.register.controller, com.nassoft.erpweb.register.model. –  Feb 24 '15 at 16:01
  • @Vladimir post updated to post my controller. –  Feb 24 '15 at 16:02
  • 1
    The controller doesn't compile. And you didn't post the stack trace of the exception. Post the real code and the real stack trace. – JB Nizet Feb 24 '15 at 16:07
  • @felipeocr you inject `PedidoVendaBU pedidoVendaBU;` yet your call `pedidoBU.getPedidos()` and you service class is called `PedidoBU` – Vladimir Feb 24 '15 at 16:08
  • Try configuring the PedidoDAO bean in the spring configuration. – Sandeep Patange Feb 24 '15 at 16:24
  • Problem updated, another error is appearing. If someone can help, thank you very much! –  Feb 24 '15 at 18:41
  • What happens if you change Service annotation for Component annotation? – Javi Mollá Feb 25 '15 at 11:48
  • @JavierMollá another problem solved! What happens: I was trying to use a `@Service` inside another `@Service` in another part of the code without use Interfaces. Thank you for the answers! –  Feb 25 '15 at 12:01

0 Answers0