2011-01-18 7 views
6

मैंने इसका परीक्षण किया है, एक @ManagedBean पर एक सेवा ऑब्जेक्ट इंजेक्ट करने का प्रयास कर रहा है, लेकिन यह nullpointerexception के साथ विफल रहा है, उपयोगकर्ता सेवा शून्य है।जेएसएफ 2: स्प्रिंग से प्रबंधितबीन के लिए सेवा ऑब्जेक्ट इंजेक्ट करें?

मैं वर्तमान में बिलाव 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 में इंजेक्शन देना एडबीन?

कृपया इस में अपना अनुभव साझा करें ..

धन्यवाद!

*********** कुछ नए प्रयोग के परिणामों को ****************

<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 के साथ विफल रहा, उपयोगकर्ता सेवा शून्य है।

मैं वर्तमान में बिलाव 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 करने के लिए एक स्प्रिंग कामयाब सेम इंजेक्शन?

कृपया इस में अपना अनुभव साझा करें ..

धन्यवाद!

*********** बस अब मैं @Named का उपयोग कर की जगह करने की कोशिश की कुछ नए प्रयोग के परिणामों को ****************

जोड़ा @ प्रबंधित Bean और @Inject को @Autowired को प्रतिस्थापित करने के लिए, और यह काम किया, हालांकि मैं अभी भी कारण को समझ नहीं पा रहा हूं। हालांकि userService स्प्रिंग द्वारा किया जाता है,

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

    @Inject 
    private UserService userService; 

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

यह काम करता है, सेवा वस्तु इंजेक्शन गया:

तो यहाँ modifed कामयाब सेम है? वो कैसे संभव है ?

यहाँ .. कुछ ऐसी बातों का है कि मैं रास्ते में जोड़ने है

मैं web.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> 

मैं भी एक खाली वेब-INF/beans.xml

जोड़ा

और यह चेहरे-config.xml अंदर org.springframework.web.jsf.el.SpringBeanFacesELResolver

इम थोड़ा @Na उपयोग करने के साथ यहां चिंतित मेड, क्योंकि मेरे पास View12coped के साथ bad experience का उपयोग कर रहा है। जब मैं @ManagedBean वसंत ढांचे द्वारा सेवा वस्तुओं के साथ इंजेक्शन करने में सक्षम होने के नाते बहुत सुरक्षित महसूस करता हूं।

मुझे आश्चर्य है कि आप आमतौर पर प्रबंधित बिजनेस को अपने व्यापार सेवा ऑब्जेक्ट्स से कनेक्ट करने के लिए क्या करते हैं .. सीडीआई, या शुद्ध वसंत, या उनका संयोजन का उपयोग करते हुए?

उत्तर

5

web.xml

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

परिभाषित हैलो , अपनी त्वरित प्रतिक्रिया के लिए धन्यवाद। मैंने मूल पोस्ट में कुछ विवरण जोड़े हैं। अगर मैं गलत हूं, तो मुझे सही करें, लेकिन DelegatingVariableResolver ऐसा है कि मैं # {springBeanName} कर सकता हूं, और RequestContextListener ऐसा है कि मुझे अब @ManagedBean का उपयोग करने की आवश्यकता नहीं है, और इसके बजाय मैं बस वसंत xml में बीन को परिभाषित कर सकता हूं या @ कॉम्पोनेंट का उपयोग कर सकता हूं एक बीन की देखभाल करते हुए, 2 अलग कंटेनर (जेएसएफ और वसंत) से परहेज करते हुए बीन। लेकिन मैं जेएसएफ द्वारा @ManagedBean को संभालना पसंद करता हूं, और स्प्रिंग ने प्रबंधित बीन में एक सेवा बीन इंजेक्ट किया है। मैंने आपके द्वारा उल्लिखित लोगों को जोड़ने का प्रयास किया, लेकिन फिर भी विफल रहा। – bertie

+0

मुझे 'चेहरे-config.xml' में परिवर्तनीय रिज़ॉल्यूशन दिखाई नहीं देता है, इसका उपयोग आपके द्वारा इंजेक्ट करने वाली किसी भी निर्भरता को हल करने के लिए किया जाता है। ताकि आपका जेएसएफ कंटेनर वसंत के संदर्भ पर लुकअप करेगा यदि यह जेएसएफ के संदर्भ में नहीं मिलता है –

+0

आह क्षमा करें, मैं यह उल्लेख करना भूल गया कि मैंने इसे @ चेहरे-config.xml: org.springframework.web.jsf भी जोड़ा है। एल।SpringBeanFacesELResolver .. मुझे पता चला कि यह जेएसएफ 1.2 और ऊपर के लिए है। आपके द्वारा सुझाए गए 2 जोड़ों के साथ, मैं @ManagedBean को काम करने के लिए नहीं मिला, जो मेरे लिए एक बहुत अच्छा समाधान होगा। मैं @Named से बचना चाहता हूं क्योंकि यह @ViewScoped के साथ संघर्ष करता है, मैं कई स्थानों पर भारी उपयोग करना चाहता हूं। क्या आप कृपया मुझे कुछ और रोशनी छोड़ सकते हैं? मैं काफी उलझन में हूं कि @ नामित कामों का उपयोग क्यों करें, लेकिन @ प्रबंधित बीन नहीं। और आप आमतौर पर इस तरह के सामान के लिए क्या उपयोग करते हैं? – bertie

0

अपने अनुप्रयोगों में में faces-config.xml

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

RequestContextListener श्रोता लापता में चर समाधानकर्ता लापता संदर्भ एक्सएमएल एक सेम "UserService" के लिए उदाहरण के लिए,

<bean id="userService" class="org.albertkam.yourclassname"/> 
संबंधित मुद्दे