Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 将bean自动连接到JSF托管bean时的空指针_Spring_Jsf 2_Dependency Injection_Autowired_Managed Bean - Fatal编程技术网

Spring 将bean自动连接到JSF托管bean时的空指针

Spring 将bean自动连接到JSF托管bean时的空指针,spring,jsf-2,dependency-injection,autowired,managed-bean,Spring,Jsf 2,Dependency Injection,Autowired,Managed Bean,我使用SpringJavaMail和Velocity模板开发了一个电子邮件服务,如下所示 Email.java @Component public class Email { private JavaMailSender mailSender; private VelocityEngine velocityEngine; @Autowired private ApplReviewService app

我使用SpringJavaMail和Velocity模板开发了一个电子邮件服务,如下所示

Email.java

@Component
public class Email {    

        private JavaMailSender mailSender;      
        private VelocityEngine velocityEngine;  


         @Autowired
        private ApplReviewService applReviewService;

       @Autowired
        private UserService userService;


        public void setUserService(UserService userService ) {
            this.userService=userService;
        }


        public UserService getuserService() {
            return userService;
        }

        @Autowired
        @Required
        public void setMailSender(JavaMailSender mailSender) {
            this.mailSender = mailSender;
        }

        public VelocityEngine getVelocityEngine() {
            return velocityEngine;
        }

        @Autowired
        @Required
        public void setVelocityEngine(VelocityEngine velocityEngine) {
            this.velocityEngine = velocityEngine;
        }
//方法发送电子邮件。 }

My Spring.xml

<context:component-scan base-package="com.test.common"/>

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
           </bean>

   <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
      <property name="velocityProperties">
         <value>
            resource.loader=class
            class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
         </value>
      </property>
   </bean>


@ManagedBean(name="person")
@SessionScoped
Public class Person{

@Autowired
private Email email ; // getter and setter for this.

}

resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
@ManagedBean(name=“person”)
@会议范围
公共阶层人士{
@自动连线
私人电子邮件;//此邮件的getter和setter。
}

我正在尝试将我的电子邮件类自动连接到Jsf managedBean中,但我遇到了空指针异常。我错在哪里。

您不能在JSF管理的bean中注入这样的Springbean。换成

@ManagedBean(name="person")
@SessionScoped
Public class Person{

@ManagedProperty(value="#{email}")
private Email email ; // getter and setter for this.

}
另见:


@Ravi…..我尝试了上面的答案,但它不起作用。我仍然得到空指针。你能澄清我一件事吗。我已经在主/java/resources/config文件夹Spring.xml中定义了电子邮件bean。这是个问题吗。我需要在WEB-INF文件夹ApplicationContext.xml中专门定义它吗?不管你在哪里尽管默认位置为/web-INF/applicationContext.xml,但只要您在web.xml的contextConfigLocation中提到,就可以将xml文件放入其中。另见。您是如何在web.xml中指定的?您是否也在faces-config.xml中添加了org.springframework.web.jsf.el.SpringBeanFacesELResolver嘿,谢谢Ravi…..您的建议清楚地说明了如何将Springbean加载到应用程序中。现在,我能够使我的应用程序正常工作,并清楚地了解了Springbean的加载。还有一个问题。一旦我启动应用程序,ApplicationContext在我的应用程序中加载了六次。如何停止?您在控制台输出中看到了吗?