Java 异常:创建名为';richBean';

Java 异常:创建名为';richBean';,java,spring,hibernate,maven,tomcat,Java,Spring,Hibernate,Maven,Tomcat,我在实例化事件时出错。 我在不同的论坛上搜索,发现它可能是tomcat的版本,但实际上我使用的是7.042版本,问题已经解决了 我想问题就在这里,但我找不到解决方法: @javax.inject.Inject Event<String> pushEvent; 这是我的beanClass: import javax.annotation.PostConstruct; import javax.enterprise.event.Event; import javax.fa

我在实例化事件时出错。 我在不同的论坛上搜索,发现它可能是tomcat的版本,但实际上我使用的是7.042版本,问题已经解决了

我想问题就在这里,但我找不到解决方法:

   @javax.inject.Inject
    Event<String> pushEvent;
这是我的beanClass:

import javax.annotation.PostConstruct;
import javax.enterprise.event.Event;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;

import org.richfaces.application.push.TopicKey;
import org.richfaces.application.push.TopicsContext;
import org.richfaces.cdi.push.Push;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.ocpsoft.pretty.faces.annotation.URLMapping;
import com.ocpsoft.pretty.faces.annotation.URLMappings;

@ManagedBean(name = "richBean")
@javax.inject.Named
@Component(value = "richBean")
@Scope(value = "session")
@URLMappings(mappings = { @URLMapping(id = "rich", pattern = "/test", viewId = "/test.xhtml") })
public class RichBean {

    public static final String PUSH_CDI_TOPIC = "pushCdi";

    private String message;

    @javax.inject.Inject
    @Push(topic = PUSH_CDI_TOPIC)
    Event<String> pushEvent;


    /**
     * Sends message.
     *
     * @param message to send
     */
    public void sendMessage() {
        System.out.println("***COUCOU***");
        pushEvent.fire(message);
        message = "";
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}
import javax.annotation.PostConstruct;
导入javax.enterprise.event.event;
导入javax.faces.bean.ManagedBean;
导入javax.inject.inject;
导入org.richfaces.application.push.TopicKey;
导入org.richfaces.application.push.TopicsContext;
导入org.richfaces.cdi.push.push;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.context.annotation.Scope;
导入org.springframework.stereotype.Component;
导入org.springframework.stereotype.Service;
导入com.ocpsoft.pretty.faces.annotation.URLMapping;
导入com.ocpsoft.pretty.faces.annotation.URLMappings;
@ManagedBean(name=“richBean”)
@javax.inject.Named
@组件(value=“richBean”)
@范围(value=“会话”)
@URLMappings(mappings={@URLMapping(id=“rich”,pattern=“/test”,viewId=“/test.xhtml”)})
公共类RichBean{
公共静态最终字符串PUSH\u CDI\u TOPIC=“pushCdi”;
私有字符串消息;
@javax.inject.inject
@推送(主题=推送\u CDI\u主题)
事件推送事件;
/**
*发送消息。
*
*@param要发送的消息
*/
公共无效发送消息(){
System.out.println(“***COUCOU***”);
pushEvent.fire(消息);
message=“”;
}
公共字符串getMessage(){
返回消息;
}
公共无效设置消息(字符串消息){
this.message=消息;
}
}
My test.xhtml:

    <ui:composition template="/pages/template/template.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:c="http://java.sun.com/jsp/jstl/core" 
    xmlns:b="http://richfaces.org/sandbox/bootstrap"
    lang="fr">

    <ui:define name="body">

    <h:form>
        <h:panelGrid columns="4">
            <h:outputLabel value="Message:" />
            <h:inputText id="messageInput" styleClass="message" value="#{richBean.message}">
            </h:inputText>

            <a4j:commandButton value="Send" action="#{richBean.sendMessage}"
                execute="@form" render="messageInput" />

            <rich:messages for="messageInput" />
        </h:panelGrid>


    </h:form>

    <h:form>
        <h:panelGrid columns="3">
            <a4j:push address="pushCdi"
                ondataavailable="jQuery('&lt;li /&gt;').prependTo('#messages').text(event.rf.data)" />

            <ul id="messages" />
        </h:panelGrid>
    </h:form>

    </ui:define>

</ui:composition>


您没有任何名为“richBean”的bean。确保您遗漏了bean定义。

您应该用
@javax.inject.Named
注释您的
CustomClass
类,以便能够将其注入
RichBean
bean:

package my.package;

import javax.inject.Named;

@Named
public class CustomClass {
...
}
注意:确保启用基于注释的扫描,以便让依赖关系管理器通过在spring上下文文件中添加
标记来发现bean:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.1.xsd">

  ...
  <context:component-scan base-package="your.package.name" />
  ...

</beans>

...
...

另外,我看不出同时使用
@javax.inject.Named
@org.springframework.stereotype.Component
注释没有任何基本优势。您应该保留它们中的任何一个用于bean注入。

问题在于这个
事件pushEvent
是否定义了这个bean的任何地方?这是来自javax.enterprise.Event.Event抱歉,RichBean实际上是我的类名。我更新了我的帖子,你知道@Inject的问题吗?这个注释仍然不起作用,我仍然有这个“自动连线依赖项的注入失败”。此外,当我在firefox上查看控制台时,我还发现:“TypeError:jQuery.atmosphere未定义”您认为我应该怎么做?请提供完整的堆栈跟踪,并且在浏览器中检测到的错误应该与缺少某些脚本的导入有关,而不是与主线程问题有关。您是否在spring上下文描述符中添加了
?是的,我添加了它。这里是完整的堆栈跟踪:这里是在我的浏览器中检测到的错误:15:17:04096 GET[HTTP/1.1 200 OK 134ms]15:17:04183 L'Usilization de«getUserData()»ou«setUserData()»est obsolète。利用者«WeakMap»ou«element.dataset»la place。requestNotifier.js:64 15:17:04719 GET[HTTP/1.1 404 Intruvable 2ms]15:17:04685 TypeError:jQuery.atmosphere未定义打包。js:2261
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.1.xsd">

  ...
  <context:component-scan base-package="your.package.name" />
  ...

</beans>