简体   繁体   English

在托管bean中用spring注入:

[英]injection with spring in the managed bean :

I created a simple application that s using JSF and managed beans.From the managed bean I m trying to instantiate a spring bean that stores data to the database. 我创建了一个使用JSF和托管bean的简单应用程序。我试图从托管bean实例化将数据存储到数据库的spring bean。 However " @Autowired " annotation doesnt seem to work since I get a nullPointerExcpetion : 但是,由于我得到nullPointerExcpetion,@Autowired ”注释似乎不起作用

javax.faces.el.EvaluationException: java.lang.NullPointerException javax.faces.el.E​​valuationException:java.lang.NullPointerException

The problem appears when there invocation authenticationComponent.authenticate () method 当有调用authenticationComponent.authenticate()方法时出现问题

rmq : I tested the methods of ClientService and all is well rmq :我测试了ClientService的方法,一切都很好

managed bean : 托管Bean:

@ManagedBean(name="authenticationComponent")
@RequestScoped
public class AuthenticationComponent implements Serializable {

    private static final long serialVersionUID = -5085727127290746426L;
    private static final String SUCCESS = "success";
    private static final String ERROR = "error";

    @Autowired
    ClientService clientService;
    Client client;
    String clientName;
    String clientPass;
    public String authenticate() {

        client = clientService.authenticate(clientName, clientPass);
        if (client != null) {
            return SUCCESS;
        }
        return ERROR;

    }

authentification.xhtml authentification.xhtml

  <p:inputText id="userName" required="true" label="User Name" title="Enter Your Name !" value="#{authenticationComponent.clientName}"/> <p:tooltip for="userName" styleClass="tooltip" showEvent="focus" hideEvent="blur" /> <h:panelGroup> <h:outputText value="User Password : " /> <h:outputText style="color:red" value="* " /> </h:panelGroup> <p:inputText id="userPass" required="true" label="User Password" title="Enter Your Password !" value="#{authenticationComponent.clientPass}" /> <!-- label option is needed for messages component --> <p:tooltip for="userPass" styleClass="tooltip" showEvent="focus" hideEvent="blur" /> <p:commandButton id="submitLoginButton" update="loginPanelGrid,messages" value="Sign In" icon="ui-icon-check" action="#{authenticationComponent.authenticate()}" /> <p:commandButton id="newUserButton" update="loginPanelGrid,messages" value="Sign Up" icon="ui-icon-plus" /> 

applicationContext: 的applicationContext:

    <bean id="clientService" class="com.webapp.service.ClientServiceImpl">
            <property name="clientDao" ref="clientDao" />
        </bean>
 <bean id="genericDao" class="com.webapp.dao.GenericDaoImpl" abstract="true" >
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="clientDao" class="com.webapp.dao.ClientDaoImpl" parent="genericDao">
        <constructor-arg ref="client"  />
   </bean>

  <bean id="client" class="com.webapp.model.Client"/>

First you will need a custom el-resolver in your faces-config.xml file: 首先,您将在faces-config.xml文件中需要一个自定义的el-resolver:

 <application>
        <!-- For DI of beans from Spring WebApplicationContext -->
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

Then you will need to use the annotation @ManagedProperty like this: 然后,您将需要使用注释@ManagedProperty如下所示:

@ManagedProperty(value = "#{clientService}")
ClientService clientService;

So the spring el-resolver can inject your service bean by name. 因此spring el-resolver可以按名称注入服务bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM