在支持 bean 中注入 Spring 服务 [英] Injecting Spring service in backing bean

查看:75
本文介绍了在支持 bean 中注入 Spring 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 netbeans 7.4、SPRING 3.2.3、JSF 2.2、Primefaces 3.5 和 Glassfish 4.0 制作网络.

I'm trying to make a web with netbeans 7.4, SPRING 3.2.3, JSF 2.2 with Primefaces 3.5 and Glassfish 4.0.

我的目标是创建一个非常简单的项目,在支持 bean 中注入服务.到目前为止,我已经使用这两个框架在 netbeans 中创建了一个新项目.这些是我项目中的文件:

My objective is to create a very simple project that injects a service in a backing bean. Till now I have created a new project in netbeans with these 2 frameworks. These are the files in my project:

项目树

webApplication1
  ├ Web Pages
  │    ├ WEB-INF
  │    │    ├ applicationContext.XML
  │    │    ├ dispatcher-servlet.XML
  │    │    ├ faces-config.XML
  │    │    └ web.XML
  │    └ index.XHTML
  ├ source packages
  │    └ controladores
  │         ├ IndexBB.java
  │         ├ Servicio.java
  │         └ ServicioImpl.java
  ├ Test Packages
  ├ Libraries
  ├ Test Libraries
  └ Configuration Files

Index.xhtml

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">

    <f:view contentType="text/html">    
        <h:body>
            <h:form>
                <p:inputText id="aInput" value="#{indexBB.mensaje}" />
                <p:commandButton id="btnTest" actionListener="#{indexBB.getServiceSMS}" value="Test" update="aInput" />
            </h:form>            
        </h:body>
    </f:view>
</html>

IndexBB.java

package controladores;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class IndexBB implements Serializable{

    @ManagedProperty(value="#{rfService}")
    private Servicio elServicio;

    private String mensaje="a default SMS";

    public IndexBB() {

    }

    public String getMensaje() {        
        return this.mensaje;
    }

    public void setMensaje(String mensaje) {
        this.mensaje = mensaje;
    }

    public Servicio getElServicio() {
        return this.elServicio;
    }

    public void setElServicio(Servicio elServicio) {
        this.elServicio = elServicio;
    }

    public void getServiceSMS() {
        this.mensaje=elServicio.getSMS();        
    }
}

Servicio.java

package controladores;

public interface Servicio {

    public String getSMS();
}

ServicioImpl.java

package controladores;

import org.springframework.stereotype.Service;

@Service(value="rfService")
public class ServicioImpl implements Servicio{

    @Override
    public String getSMS() {
    return "a message generated by a service";
    }
}

applicationContext.XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"       
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"       
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">


</beans>

dispatcher-servlet.XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"    
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"       
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.xhtml">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/"
          p:suffix=".xhtml" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />


</beans>

faces-config.XML

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
    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/web-facesconfig_2_2.xsd">

    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
</faces-config>

web.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <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>dispatcher</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

如您所见,该项目非常简单.只是一个按钮,它从服务中获取值并将其放入变量中.

As you can see the project is VERY simple. Just a button that takes the value from a service an puts it in a variable.

问题是,当我按下按钮 Glassfish 给我一个带有此堆栈跟踪的 NullPointer 时:

The problem is that when I press the button Glassfish gives me a NullPointer with this stackTrace:

javax.el.ELException: java.lang.NullPointerException
    at com.sun.el.parser.AstValue.invoke(AstValue.java:279)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:818)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
    at controladores.IndexBB.getServiceSMS(IndexBB.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:275)

我认为服务没有被正确注入,所以当我按下按钮时它是 NULL.

In my opinion the service is not being correctly injected, so when I press the button it is NULL.

我在网上搜索过但没有结果(我发现的大部分代码都不起作用).有配置Spring经验的可以看看我的项目吗?提前致谢!

I have searched over the net with no result (most of the code I have found doesn't work). May some person with experience configuring Spring take a look to my project? Thanks in advance!

推荐答案

您有 3 个问题.

首先,您有一个在 JSF 环境中不需要的 DispatcherServlet.删除它,它的映射.您不需要所有控制器的东西,因为它是由 JSF 处理的.

First you have a DispatcherServlet which isn't needed in a JSF environment. Remove it and it's mapping. You don't need all the controller stuff as that is handled by JSF.

其次,您有一个@Service,但没有任何东西可以获取该 bean.在您的 applicationContext.xml 中添加组件扫描.

Second you have a @Service but have nothing that is picking up that bean. In your applicationContext.xml add component scanning.

<context:component-scan base-package="controladores" />

第三,你的托管 bean 有错误的 setter.您必须有一个与 @ManagedProperty 注释中的值相匹配的 setter,因此在您的情况下,您应该有一个 setRfService(..) 方法.

Thirdly your managed bean has the wrong setter. You must have a setter that matches the value in your @ManagedProperty annotation, so in your case you should have a setRfService(..) method.

这篇关于在支持 bean 中注入 Spring 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆