无法从 spring 服务 Tomcat 7 创建 bean "helloController"

Impossible to create bean "helloController" from spring service Tomcat 7

您好,请帮助我创建一个动态项目 Spring/JSF。我用的技术:

我有这个错误:

avr. 06, 2015 7:41:34 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
GRAVE: Error Rendering View[/index.xhtml]
javax.el.ELException: /index.xhtml: Impossible de créer le bean géré «helloController».  Les problèmes suivants ont été détectés :
     - La propriété «helloSpringService» du bean géré «helloController» n’existe pas.
    at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:90)
    at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
    at com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.sun.faces.mgbean.ManagedBeanCreationException: Impossible de créer le bean géré «helloController».  Les problèmes suivants ont été détectés :
     - La propriété «helloSpringService» du bean géré «helloController» n’existe pas.
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:265)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:71)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:160)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:238)
    at com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:154)
    at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
    ... 30 more

avr. 06, 2015 7:41:34 PM com.sun.faces.context.ExceptionHandlerImpl log
1100: JSF1073 : javax.el.ELException intercepté durant le traitement de RENDER_RESPONSE 6 : UIComponent-ClientId=, Message=/index.xhtml: Impossible de créer le bean géré «helloController».  Les problèmes suivants ont été détectés :
     - La propriété «helloSpringService» du bean géré «helloController» n’existe pas.
avr. 06, 2015 7:41:34 PM com.sun.faces.context.ExceptionHandlerImpl log
1100: /index.xhtml: Impossible de créer le bean géré «helloController».  Les problèmes suivants ont été détectés :
     - La propriété «helloSpringService» du bean géré «helloController» n’existe pas.

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>DemandeAch</display-name>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

    <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>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>
    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>0</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
</web-app>

SpringConfiguration.java/MyWebAppInitializer.java

@Configuration
@ComponentScan
public class SpringConfiguration {

}

public class MyWebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext container) throws ServletException {
    // create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(SpringConfiguration.class);

    // Manage lifecycle of the root application context
    container.addListener(new ContextLoaderListener(rootContext));

}
}

HelloSpringService.java

@Service
public class HelloSpringService {
public String sayHello(){
        return "Hello from Spring Service";
    }
}

控制器

@ManagedBean
public class HelloController {

    @ManagedProperty("#{helloSpringService}")
    private HelloSpringService helloSpringService;
    public String showHello(){
        return helloSpringService.sayHello();
    }
}

index.xhtml

<h:head></h:head>
<h:body>
    managed bean says :
    #{helloController.showHello()}
</h:body>

我需要你的帮助:~(谢谢。

谢谢我解决了这个问题,只是我必须将 setter 添加到我的控制器:

你好控制器

public void setHelloSpringService(HelloSpringService helloSpringService) {
        this.helloSpringService = helloSpringService;
    }

谢谢 ;)