2011-01-18 4 views
6

これをテストして、サービスオブジェクトを@ManagedBeanに注入しようとしましたが、nullpointerexceptionで失敗しました.userServiceはnullです。JSF2:Springからマネージドビーンにサービスオブジェクトを挿入しますか?

私は現在のTomcat 7、JSF 2を使用して、ここでここに私のpom.xmlの一部

<properties> 
    <java-version>1.6</java-version> 
    <org.springframework-version>3.0.3.RELEASE</org.springframework-version> 
    <org.hibernate-version>3.6.0.Final</org.hibernate-version> 
    .... 
</properties> 

あるのですが、例外トレースです:

javax.faces.el.EvaluationException: java.lang.NullPointerException 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
    at java.lang.Thread.run(Thread.java:636) 
Caused by: java.lang.NullPointerException 
    at org.albertkam.ui.LoginBean.login(LoginBean.java:38) 
    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:616) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98) 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) 
    ... 23 more 

は、ここに私のManagedBeanです:

@ManagedBean 
@RequestScoped 
public class LoginBean { 
    private MstUser entityMstUser; 

    @Autowired 
    private UserService userService; 

    @PostConstruct 
    private void init() { 
     this.entityMstUser = new MstUser(); 
    } 

私のサービス豆です。

@Service 
public class UserService { 

    @Autowired 
    private UserDAO userDao; 

    public void login(MstUser entityMstUser) { 

そして、ここに私の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:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" 
    default-autowire="byName"> 

    <context:component-scan base-package="org.albertkam" /> 
    <context:annotation-config /> 
    <tx:annotation-driven /> 

</beans> 

そして、私のweb.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <display-name>albert simpleWebapp</display-name> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext.xml 
      /WEB-INF/datasourceContext.xml 
     </param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>facesServlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>facesServlet</servlet-name> 
     <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
     <welcome-file>faces/index.xhtml</welcome-file> 
    </welcome-file-list> 
    <context-param> 
     <param-name>javax.faces.PROJECT_STAGE</param-name> 
     <param-value>Development</param-value> 
    </context-param> 

</web-app> 

そして、私の顔-config.xmlに、ここに関連するものは本当にありませんが...

<?xml version="1.0" encoding="UTF-8"?> 

<faces-config 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" 
    version="2.0"> 

    <application> 
     <locale-config> 
      <default-locale>in</default-locale> 
      <supported-locale>en</supported-locale> 
     </locale-config> 
     <resource-bundle> 
     <base-name>org.albertkam.common.messages</base-name> 
     <var>msgs</var> 
     </resource-bundle> 
     <resource-bundle> 
     <base-name>org.albertkam.common.errors</base-name> 
     <var>errs</var> 
     </resource-bundle> 

     <!-- <message-bundle>id.co.sofcograha.override_messages</message-bundle> --> 
    </application> 

    <factory> 
     <exception-handler-factory>org.albertkam.common.BusinessExceptionHandlerFactory</exception-handler-factory> 
    </factory> 
</faces-config> 

@ManagにSpringのマネージドBeanを注入することは可能でしょうか? edBean?

この中にあなたの経験を共有してください。..

ありがとうございます!

は***********私がしようとし、これをテストしている

<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 
<listener> 
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class> 
</listener>Hello, 

****************いくつかの新しい実験結果を追加しました@ManagedBeanにサービスオブジェクトを注入しますが、nullpointerexceptionで失敗しました.userServiceはnullです。

私は現在のTomcat 7、JSF 2を使用して、ここでここに私のpom.xmlの一部

<properties> 
    <java-version>1.6</java-version> 
    <org.springframework-version>3.0.3.RELEASE</org.springframework-version> 
    <org.hibernate-version>3.6.0.Final</org.hibernate-version> 
    .... 
</properties> 

あるのですが、例外トレースです:

javax.faces.el.EvaluationException: java.lang.NullPointerException 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
    at java.lang.Thread.run(Thread.java:636) 
Caused by: java.lang.NullPointerException 
    at org.albertkam.ui.LoginBean.login(LoginBean.java:38) 
    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:616) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98) 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) 
    ... 23 more 

は、ここに私のManagedBeanです:

@ManagedBean 
@RequestScoped 
public class LoginBean { 
    private MstUser entityMstUser; 

    @Autowired 
    private UserService userService; 

    @PostConstruct 
    private void init() { 
     this.entityMstUser = new MstUser(); 
    } 

私のサービス豆です。

@Service 
public class UserService { 

    @Autowired 
    private UserDAO userDao; 

    public void login(MstUser entityMstUser) { 

そして、ここでは私の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:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" 
    default-autowire="byName"> 

    <context:component-scan base-package="org.albertkam" /> 
    <context:annotation-config /> 
    <tx:annotation-driven /> 

</beans> 

そして、私のweb.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <display-name>albert simpleWebapp</display-name> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext.xml 
      /WEB-INF/datasourceContext.xml 
     </param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>facesServlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>facesServlet</servlet-name> 
     <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
     <welcome-file>faces/index.xhtml</welcome-file> 
    </welcome-file-list> 
    <context-param> 
     <param-name>javax.faces.PROJECT_STAGE</param-name> 
     <param-value>Development</param-value> 
    </context-param> 

</web-app> 

そして、私の顔-config.xmlに、ここに関連するものは本当にありませんが...

<?xml version="1.0" encoding="UTF-8"?> 

<faces-config 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" 
    version="2.0"> 

    <application> 
     <locale-config> 
      <default-locale>in</default-locale> 
      <supported-locale>en</supported-locale> 
     </locale-config> 
     <resource-bundle> 
     <base-name>org.albertkam.common.messages</base-name> 
     <var>msgs</var> 
     </resource-bundle> 
     <resource-bundle> 
     <base-name>org.albertkam.common.errors</base-name> 
     <var>errs</var> 
     </resource-bundle> 

     <!-- <message-bundle>id.co.sofcograha.override_messages</message-bundle> --> 
    </application> 

    <factory> 
     <exception-handler-factory>org.albertkam.common.BusinessExceptionHandlerFactory</exception-handler-factory> 
    </factory> 
</faces-config> 

@ManagedBeanにSpringマネージドBeanを注入することは可能でしょうか?

この中にあなたの経験を共有してください。..

ありがとうございます!

は***********

はちょうど今、私は交換し@Namedを使用してみました****************いくつかの新しい実験結果を追加しました@ @Autowiredを置き換えるためのManagedBeanと@Inject、そしてそれはうまくいっていますが、私はまだなぜその理由を本当に理解していません。

は、だからここmodifed管理Beanです:

@Named("userBean") 
@RequestScoped 
public class LoginBean { 
    private MstUser entityMstUser; 

    @Inject 
    private UserService userService; 

    @PostConstruct 
    private void init() { 
     this.entityMstUser = new MstUser(); 
    } 

この作品は、UserServiceのは春で管理されているものの、サービスオブジェクトは、注入されてしまいましたか?そんなことがあるものか ?

ここで私はまた、空のWEB-INF/beans.xmlの

を追加

<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 
<listener> 
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class> 
</listener> 

私はweb.xmlにこれを追加..私は道に沿って追加し、いくつかの追加のものをです

そして@Naを使用して、ここで、この顔-config.xmlの内部 org.springframework.web.jsf.el.SpringBeanFacesELResolver

イム少し心配私はbad experienceをViewScopedと一緒に使っています。 @ManagedBeanに春のフレームワークでサービスオブジェクトを注入できるようになると、私はもっと安全に感じるでしょう。

あなたは通常、ManagedBeanをビジネスサービスオブジェクトと接続するために何をしているのでしょうか?CDI、純粋なSpring、またはそれらの組み合わせを使用していますか?アプリケーションのコンテキストでweb.xml

<listener> 
    <listener-class> 
    org.springframework.web.context.request.RequestContextListener 
    </listener-class> 
</listener> 

答えて

5

XMLこんにちは

<bean id="userService" class="org.albertkam.yourclassname"/> 
+1

、例えば "UserServiceの" ためのBeanを定義します、 早速の対応、ありがとうございました。元の投稿にいくつかの詳細を追加しました。私が間違っている場合は私を修正しますが、DelegatingVariableResolverは#{springBeanName}とできるので、もう@ManagedBeanを使用する必要はありません。豆を世話している2つの異なる容器(jsfとspring)を避けて、豆。しかし、私は@ManagedBeanをJSFで処理することを好み、SpringがManagedBeanにサービスBeanを注入するようにします。あなたが言及したものを追加しようとしましたが、それでも失敗しました。 – bertie

+0

'faces-config.xml'に変数リゾルバが表示されません。これは、あなたが注入する依存関係を解決するために使用されます。あなたのjsfコンテナがjsfのコンテキストで見つからない場合、春のコンテキスト上でルックアップを行います。 –

+0

ああ、申し訳ありませんが、これも@ faces-config.xml: org.springframework.web.jsfに追加しました。エル。SpringBeanFacesELResolver ..私はこれがjsf 1.2以上であることを知りました。あなたが提案した2つの追加で、私は@ManagedBeanを動作させることができませんでした。それは私にとって非常に良い解決策になります。私はそれが@ViewScopedと競合する@Namedを回避したいと思います。私は多くの場所で頻繁に使用したいです。あなたは私にこのことについていくつかの光を当ててください。 Imはなぜ@ Namedの作品を使うのか、@ ManagedBeanを使わないのはかなり混乱しています。あなたは通常、このようなものに何を使用しますか? – bertie

0

でRequestContextListenerリスナーが欠けfaces-config.xml

<application> 
    <variable-resolver> 
    org.springframework.web.jsf.DelegatingVariableResolver 
    </variable-resolver> 
</application> 

で変数リゾルバを逃す

関連する問題