CDI的问题 - (Weld + TomCat7 + JSF)

时间:2014-10-13 11:32:26

标签: tomcat7 cdi weld

我正在使用焊接启动CDI项目,当我尝试注射时出现以下错误:

@Inject
private UserRepository repository;

控制台:

INFO: Starting Servlet Engine: Apache Tomcat/7.0.12
13:16:22,859  INFO servletWeldServlet:57 - WELD-ENV-001008: Initialize Weld using ServletContainerInitializer
13:16:22,875  INFO Version:151 - WELD-000900: 2.2.5 (Final)
13:16:22,906  INFO Bootstrap:206 - WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
13:16:22,937  WARN Interceptor:47 - WELD-001700: Interceptor annotation class javax.ejb.PostActivate not found, interception based on it is not enabled
13:16:22,937  WARN Interceptor:47 - WELD-001700: Interceptor annotation class javax.ejb.PrePassivate not found, interception based on it is not enabled
13:16:23,046  INFO servletTomcat:45 - WELD-ENV-001100: Tomcat 7+ detected, CDI injection will be available in Servlets, Filters and Listeners.
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type UserRepository with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private br.com.controllers.UserController.repository
  at br.com.controllers.UserController.repository(UserController.java:0)

    at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:372)
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:293)
    at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:167)
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:531)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

EntityManagerFactory.java

package br.com.util;

import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class EntityManagerFactory {

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

    @Produces
    public EntityManager create() {
        return em;
    }

    public void close(@Disposes EntityManager em) {
        em.close();
    }

}

Repository.java

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

public abstract class Repository<T, I extends Serializable> {


    @Inject
    protected final EntityManager entityManager;
    protected final Class<T> clazz;

    protected Repository(EntityManager entityManager) {

        this.entityManager = entityManager;
        @SuppressWarnings("unchecked")
        Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        this.clazz = clazz;

    }

    public void refresh(T entity){
        entityManager.refresh(entity);  
    }

    public void create(T entity) {
        entityManager.persist(entity);  
    }

    public T update(T entity) {
        return entityManager.merge(entity);
    }

    public void destroy(T entity) {
        entityManager.remove(entity);
    }

    public T find(I id) {
        return entityManager.find(clazz, id);
    }

    public List<T> findAll() {

        TypedQuery<T> query = entityManager.createQuery("FROM " + clazz.getName() ,clazz);    
        List<T> resultList = query.getResultList();
        return resultList;    
    }



}

UserRepositoryImpl.java

package br.com.repositories;


import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.com.models.User;


@Transactional
public class UserRepositoryImpl extends Repository<User, Long >implements UserRepository {

    protected UserRepositoryImpl(EntityManager entityManager) {
        super(entityManager);
    }


}

UserController.java

package br.com.controllers;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.repositories.UserRepository;



@Named("userController")  
@RequestScoped  
public class UserController {

    @Inject
    private UserRepository repository;

    UserController(){

    }

    @PostConstruct
    public void init(){
        System.out.println("started");
    }

    public String getMessage(){
        return "test";
    }

    public void create(){
        System.out.println("test method");
    }

}

META-INF / beans.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                           http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       version="1.1" bean-discovery-mode="all">

</beans>

META-INF / context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Manager pathname=""/> <!-- disables storage of sessions across restarts -->
<Resource name="BeanManager"
auth="Container"
type="javax.enterprise.inject.spi.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory"/>
</Context>

WEB-INF / web.xml中

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
      <display-name>wochenbericht</display-name>

     <welcome-file-list>
        <welcome-file>index.jsf</welcome-file>
      </welcome-file-list>

      <!-- CDI - WELD -->
       <listener>
       <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
       </listener>

        <resource-env-ref>
        <resource-env-ref-name>BeanManager</resource-env-ref-name>
            <resource-env-ref-type>
               javax.enterprise.inject.spi.BeanManager
            </resource-env-ref-type>
        </resource-env-ref>


      <!-- JSF -->
      <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
      </servlet-mapping>


      <!-- SESSION TIMEOUT -->
      <session-config>
         <session-timeout>30</session-timeout>
      </session-config> 

      <!-- CONTEXT LOCAL -->
      <context-param>
          <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
          <param-value>client</param-value>
      </context-param> 

      <context-param>
          <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
          <param-value>2</param-value>
      </context-param> 

      <context-param>
        <param-name>javax.faces.WEBAPP_RESOURCES_DIRECTORY</param-name>
        <param-value>/assets</param-value> 
      </context-param>

      <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
      </context-param>

      <!--  SECURITY SETTINGS -->
      <security-constraint>
        <display-name>Restrict direct access to XHTML files</display-name>
        <web-resource-collection>
            <web-resource-name>XHTML files</web-resource-name>
            <url-pattern>*.xhtml</url-pattern>
        </web-resource-collection>
        <auth-constraint />
      </security-constraint>

My Libs:

antlr-2.7.7.jar
aopalliance.jar
c3p0-0.9.1.jar
cdi-api-1.1.jar
dom4j-1.6.1.jar
EasyCriteria-3.1.0.jar
hibernate-c3p0-4.3.6.Final.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.6.Final.jar
hibernate-entitymanager-4.3.5.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-validator-4.3.0.Final.jar
javassist-3.18.1-GA.jar
javax.inject.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
jsf-api-2.2.8.jar
jsf-impl-2.2.8.jar
log4j-1.2.16.jar
postgresql-9.2-1003.jdbc4.jar
slf4j-api-1.6.6.jar
slf4j-jdk14-1.6.6.jar
slf4j-log4j12-1.6.1.jar
validation-api-1.0.0.GA.jar
weld-se-core-2.2.5.Final.jar
weld-servlet-2.2.5.Final.jar
weld-servlet-core-2.2.5.Final.jar

任何人都可以帮助我吗?谢谢!

2 个答案:

答案 0 :(得分:1)

您的第一个问题是UserRepositoryImpl类没有默认(空)构造函数,并且没有@Produces方法,因此无法实例化它。从EntityManager entityManagerRepository类中删除构造函数中的UserRepositoryImplRepository中的构造函数更改为:

protected Repository() {

    /// this.entityManager = entityManager;  // <= this is already injected by
                                             // @Inject protected final EntityManager entityManager;
    @SuppressWarnings("unchecked")
    Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    this.clazz = clazz;
}

您的第二个问题是错误注入PersistenceContext。您需要使用EJB(通过EntityManager注入@PersistenceContext)或使用Web模块:

@PersistenceUnit(unitName="default") 
EntityManagerFactory emf;
...
EntityManager em = emf.createEntityManager();

而你的第三个问题将是事务 - 再次你需要切换到EJB,或者在持久化bean时处理事务。

我建议在编码之前先阅读以下内容:

答案 1 :(得分:0)

@Gas感谢您的反馈和链接,现在有效,我不知道它是不是最好的方式:

<强> UserRepositoryImpl

package br.com.repositories;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import br.com.models.User;

@Transactional
public class UserRepositoryImpl extends Repository<User, Long >implements UserRepository {


    @Inject
    protected UserRepositoryImpl(EntityManager entityManager) {
        super(entityManager);
    }


}

<强>存储库

package br.com.repositories;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

public abstract class Repository<T, I extends Serializable> {

    protected final EntityManager entityManager;
    protected final Class<T> clazz;

    protected Repository(EntityManager entityManager) {
        this.entityManager = entityManager;
        @SuppressWarnings("unchecked")
        Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        this.clazz = clazz;
    }

    public void refresh(T entity){
        entityManager.refresh(entity);  
    }

    public void create(T entity) {
        entityManager.persist(entity);  
    }

    public T update(T entity) {
        return entityManager.merge(entity);
    }

    public void destroy(T entity) {
        entityManager.remove(entity);
    }

    public T find(I id) {
        return entityManager.find(clazz, id);
    }

    public List<T> findAll() {
        TypedQuery<T> query = entityManager.createQuery("FROM " + clazz.getName() ,clazz);    
        return query.getResultList();
    }

}

<强> EntityManagerProducer

package br.com.util;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

@ApplicationScoped
public class EntityManagerProducer {

    private EntityManagerFactory factory;

    public EntityManagerProducer() {
       factory = Persistence.createEntityManagerFactory("default");
    }

    @Produces @RequestScoped
    public EntityManager createEntityManager() {
      return factory.createEntityManager();
    }

    public void closeEntityManager(@Disposes EntityManager manager) {
      manager.close();
    }

}

<强> UserController中

package br.com.vw.controllers;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.models.User;
import br.com.repositories.UserRepository;



@Named("userController")  
@RequestScoped  
public class UserController {

    @Inject UserRepository userRepository;

    UserController(){

    }

    @PostConstruct
    public void init(){
        System.out.println("Inicialilizado o construtor");
    }

    public String getMessage(){
        return "Conteúdo da mensagem";
    }

    public void create(){

        System.out.println("Método create disparado");

        User user = new User();
             user.setEmail("email@uol.com.br");
             user.setName("Marcos de Freitas");

        userRepository.create(user);


    }

}

韩国社交协会