2011-01-18 23 views
6

Bunu, bir @ManagedBean öğesine bir hizmet nesnesini enjekte etmeye çalıştığımı test ettim, ancak userService boş olduğu için bir nullpointerexception ile başarısız oldu.JSF2: Spring'ten yönetilen hizmet nesnelerini enjekte edin?

: Burada
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 

benim ManagedBean var:

Şu anda burada Tomcat 7, JSF 2 kullanarak ve am benim pom.xml İşte

<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> 

bazı istisna izidir vardır

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

    @Autowired 
    private UserService userService; 

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

Ve işte benim servis çekirdeğim ..

@Service 
public class UserService { 

    @Autowired 
    private UserDAO userDao; 

    public void login(MstUser entityMstUser) { 

Ve işte benim applicationContext.xml var ..

<?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> 

Ve

<?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> 

benim web.xml Ve yüzler-config.xml, burada ilgili hiçbir şey gerçekten var olsa ..

<?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> 

Böyle bir şeyi yapmanın mümkün olup olmadığını merak ediyorum, bir Spring yönetilen fasulyeyi @Manag'a enjekte etmek edBean?

bu deneyiminizi paylaşın ..

teşekkür ederiz!

*********** çalışırken, ben bu test ettik

<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, 

bazı yeni deney sonuçlarını **************** eklendi Bir hizmet nesnesini @ManagedBean'a enjekte edin, ancak userService boş olduğu için bir nullpointerexception ile başarısız oldu.

: Burada
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 

benim ManagedBean var:

Şu anda burada Tomcat 7, JSF 2 kullanarak ve am benim pom.xml İşte

<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> 

bazı istisna izidir vardır

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

    @Autowired 
    private UserService userService; 

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

Ve işte benim servis çekirdeğim ..

Burada ilgili bir şey gerçekten var olsa

<?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> 

Ve yüzler-config.xml

@Service 
public class UserService { 

    @Autowired 
    private UserDAO userDao; 

    public void login(MstUser entityMstUser) { 

Ve işte benim applicationContext.xml var ..

<?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> 

Ve web.xml, ..Bir @ManagedBean bir bahar yönetilen fasulye enjekte böyle bir şey yapmak mümkün olup olmadığını

<?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> 

acaba?

bu deneyiminizi paylaşın ..

teşekkür ederiz!

*********** Hemen şimdi yerine @Named kullanarak çalıştı bazı yeni deney sonuçlarını ****************

eklendi @ ManagedBean ve @Autowired yerine @Inject ve hala nedenini anlamıyorum rağmen, çalıştı. userService Bahar tarafından yönetilen rağmen

Bu işleri
@Named("userBean") 
@RequestScoped 
public class LoginBean { 
    private MstUser entityMstUser; 

    @Inject 
    private UserService userService; 

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

, hizmet nesne enjekte var:

Yani burada modifed yönetilen fasulye mı? Bu nasıl mümkün olabilir ?

İşte ben de boş bir WEB-INF/beans.xml

eklendi

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

ben web.xml''deki bu katma .. ben yol boyunca eklemek bazı ek şeyler var

Ve @Na kullanarak burada bu yüzler-config.xml iç org.springframework.web.jsf.el.SpringBeanFacesELResolver

Im biraz endişe med, ben ViewScoped ile birlikte kullanarak bir bad experience vardı. Spring framework tarafından servis nesneleriyle enjekte edilebilmem için @ManagedBean'a sahip olduğumda çok daha güvende hissederim.

Genellikle, ManagedBean'ı iş hizmeti nesnelerinizle bağlamak için ne yaptığınızı merak ediyorum. CDI veya saf yay kullanarak veya bunların birleşimi?

cevap

5

senin uygulamalarda web.xml

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

örneğin "UserService" için fasulye tanımlamak , hızlı cevabın için teşekkürler. Orijinal yayında bazı ayrıntılar ekledim. Yanlış ı im, ancak DelegatingVariableResolver böylece ben [{SpringBeanName}, ve RequestContextListener böylece @ManagedBean artık kullanmak gerekmez, ve ben sadece xml bahar fasulye tanımlamak veya @Component üzerinde kullanmak olabilir Fasulye, bir bakla ile ilgilenen 2 farklı kap (jsf ve bahar) kaçınarak. Ancak, @ManagedBean'ın JSF tarafından ele alınmasını ve Spring'in ManagedBean'a servis çekirdeği enjekte etmesini tercih ediyorum. Bahsettiklerini eklemeye çalıştım ama yine de başarısız oldum. – bertie

+0

"yüzler-config.xml" içinde değişken çözümleyici görmüyorum, enjekte ettiğiniz bağımlılıkları çözmek için kullanılır. Böylece jsf konteynırınız jsf'nin bağlamında bulamadığı takdirde baharın içeriği üzerinde arama yapacaktır –

+0

Ah üzgünüm, bunu eklediğimi de eklemeyi unuttum: @ face-config.xml: org.springframework.web.jsf. el.SpringBeanFacesELResolver .. Bunun jsf 1.2 ve üstü için olduğunu öğrendim. Önerdiğiniz 2 ilavelerle, çalışmak için @ManagedBean alamadım, ki bu benim için çok iyi bir çözüm olacak. @ViewScoped ile çakıştığından @Named'den kaçınmak istiyorum, birçok yerde yoğun olarak kullanmak istiyorum. Bana biraz daha ışık tutabilir misin lütfen? @Named kullanmanın neden, ancak @ ManagedBean işlevini kullanmama konusunda kafam karıştı mı? Ve genellikle bu tür şeyler için ne kullanıyorsunuz? – bertie

0

yılında RequestContextListener dinleyicisi eksik faces-config.xml

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

değişken çözümleyicinizi eksik bağlam xml Merhaba,

<bean id="userService" class="org.albertkam.yourclassname"/> 
İlgili konular