Wstrzykiwanie komponentów EJB

Odpowiedz Nowy wątek
KrzysiekGość
2014-01-04 04:55
KrzysiekGość
0

Cześć!

Próbuję stworzyć jakieś proste wstrzykiwanie które nie mam pojęcia czemu nie działa. Oczywiście przeglądałem niesamowicie dużo stron w poszukiwaniu rozwiązania ale wszędzie moje rozwiązanie było działające. Mianowicie mam klasę jakąś dao z interface:

 package pl.com;

import java.util.List;
import java.util.UUID;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.faces.bean.RequestScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import pl.com.jpa.FREQUENCY;
import pl.com.jpa.ROLE;
import pl.com.jpa.User;

@Stateless
public class UserDao implements UserDaoInterface {

    @PersistenceContext(unitName = "ApkaPU")
    private EntityManager em;

    UserDao() {
    };

    UserDao(User entityClass){
        this.entityClass = entityClass;
    }

    User entityClass;

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    @Override
    public User persist(User entity) {
        getEntityManager().persist(entity);
        return entity;
    }

    @Override
    public User edit(User entity) {
        getEntityManager().merge(entity);
        return entity;
    }

    @Override
    public User remove(User entity) {
        getEntityManager().remove(entity);
        return entity;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> findAll() {

        Query q = getEntityManager().createQuery(
                "SELECT e FROM " + entityClass.getName() + " e");
        List<User> list = (List<User>) q.getResultList();
        return list;
    }

    @Override
    public User find(Long id) {
        User e = getEntityManager().find(User.class, id);
        return e;
    }

    @PostConstruct
    public void createSomeTemplateUsers() {
        System.err.println("$$$$$$$ probuje post contructa");
        for (int i = 0; i < 15; i++) {
            User user1 = new User();
            user1.setName(UUID.randomUUID().toString());
            user1.setSurname(UUID.randomUUID().toString());
            user1.setAtentions(UUID.randomUUID().toString());
            user1.setWorks(UUID.randomUUID().toString());
            user1.setRoleInFamily(ROLE.BABCIA);
            user1.setSundayChurch(FREQUENCY.WCALE);
            persist(user1);
        }
    }

Interface implementuje @Local

No i chciałbym to wstrzyknąć do pewnego beana sesyjnego:

 package pl.com;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.LazyDataModel;

import pl.com.jpa.User;

@ManagedBean
@ViewScoped
public class TableBean implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -5001188771775941660L;

    private static String[] colors;

    private static String[] manufacturers;

    private LazyDataModel<User> lazyModel;

    private User selectedCar;

    private List<User> cars;

    @EJB
    private UserDaoInterface userDao;

    public TableBean() {
        cars = new ArrayList<User>();
        wygeneruj(cars, 50);
        lazyModel = new LazyCarDataModel(cars);
    }

    public User getSelectedCar() {
        return selectedCar;
    }

    public void setSelectedCar(User selectedCar) {
        this.selectedCar = selectedCar;
    }

    public LazyDataModel<User> getLazyModel() {
        return lazyModel;
    }

    private void wygeneruj(List<User> list, int size) {

        if (userDao == null) {
            System.err.println("NULL!!!");
        } else {
            list = userDao.findAll();
        }
    }
}

I zamiast userDao dostaję null:

com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: pl.com.TableBean.
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:191)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:100)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:116)
    at com.sun.el.parser.AstValue.getBase(AstValue.java:151)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:200)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIData.getValue(UIData.java:732)
    at org.primefaces.component.datatable.DataTable.getValue(DataTable.java:910)
    at org.primefaces.component.api.UIData.getDataModel(UIData.java:615)
    at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:726)
    at org.primefaces.component.datatable.DataTableRenderer.preEncode(DataTableRenderer.java:93)
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:81)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
    at pl.com.TableBean.populateRandomCars(TableBean.java:88)
    at pl.com.TableBean.<init>(TableBean.java:64)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:186)
    ... 62 more

2014-01-04T04:51:41.590+0100|WARNING: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
    at pl.com.TableBean.populateRandomCars(TableBean.java:88)
    at pl.com.TableBean.<init>(TableBean.java:64)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:186)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:100)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:116)
    at com.sun.el.parser.AstValue.getBase(AstValue.java:151)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:200)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIData.getValue(UIData.java:732)
    at org.primefaces.component.datatable.DataTable.getValue(DataTable.java:910)
    at org.primefaces.component.api.UIData.getDataModel(UIData.java:615)
    at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:726)
    at org.primefaces.component.datatable.DataTableRenderer.preEncode(DataTableRenderer.java:93)
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:81)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)

Czegoś pewnie nie widzę albo o czymś nie wiem. Podczas lektury wielu stron też pewnie czegoś nie zauważyłem.
Proszę o pomoc za którą z góry dziękuję.

Pozostało 580 znaków

2014-01-04 09:10
Moderator

Rejestracja: 13 lat temu

Ostatnio: 3 minuty temu

Lokalizacja: Stacktrace

0

Przecież masz napisane jasno i wyraźnie:

2014-01-04T04:51:41.590+0100|WARNING: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
    at pl.com.TableBean.populateRandomCars(TableBean.java:88)
    at pl.com.TableBean.<init>(TableBean.java:64)

Coś nie jest zainicjowane.


Sięgam tam, gdzie wzrok nie sięga… a tam NullPointerException

Pozostało 580 znaków

KrzysiekGość
2014-01-04 09:32
KrzysiekGość
0

No tak jak pisałem. userDao jest null'em. I moje pytanie dlaczego? Może źle wstrzykuję? Albo może czegoś innego brakuje? Może powinienem gdzieś jeszcze zaznaczyć że to jest to dao.

Pozostało 580 znaków

2014-01-04 11:11
Moderator

Rejestracja: 13 lat temu

Ostatnio: 3 minuty temu

Lokalizacja: Stacktrace

0

Kolejność inicjacji.

  • wywołanie konstruktora
  • wstrzyknięcie pól
  • wstrzyknięcie przez setter

U ciebie w konstruktorze odwołujesz się do pola, które nie jest jeszcze zainicjowane.

Zapoznaj się z adnotacją @PostConstruct i przenieś ciało konstruktora do metody oznaczonej tą adnotacją.


Sięgam tam, gdzie wzrok nie sięga… a tam NullPointerException

Pozostało 580 znaków

Odpowiedz

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0