将bean自动连接到JSF Managed bean时的空指针 [英] Null pointer when autowiring the bean into JSF Managed bean
本文介绍了将bean自动连接到JSF Managed bean时的空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经开发了一个使用Spring Java邮件和速度模板的电子邮件服务,如下所示。
I have developed a Email service using Spring Java mail and Velocity Template like below.
Email.java
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;
}
//发送电子邮件的方法。
}
// Method to send Email. }
我的Spring.xml
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.
}
我正在尝试将我的电子邮件类自动连接到Jsf managedBean,但我我得到空指针异常。
I am trying autowire my Email class into Jsf managedBean but I am getting null pointer exception. Where I am going wrong.
推荐答案
您不能在JSF托管的bean中注入这样的Spring bean。将其更改为
You cannot inject a Spring bean like that in a JSF managed bean. Change it to
@ManagedBean(name="person")
@SessionScoped
Public class Person{
@ManagedProperty(value="#{email}")
private Email email ; // getter and setter for this.
}
另请参见:
- @Scope("request") not working
这篇关于将bean自动连接到JSF Managed bean时的空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文