Spring Injeksjon med JSF

stemmer
3

I'got et problem med å injisere bønner med merknader som bruker Spring Framework i JSF-prosjektet.
Jeg tror jeg gjorde akkurat det i utgangspunktet hver Tutorial jeg har funnet så langt sier at jeg skal gjøre, men når jeg publiserer krig min til Tomcat og prøver å bruke loginService fra LoginBean jeg får en NullPointerException. Noen ideer?

min Bean (forkortet):

package de.homer.server.config.beans;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import de.homer.server.config.user.LoginService;

@Component
@Scope(session)
public class LoginBean implements Serializable {

        @Autowired
        LoginService loginService;

    public final String doLogin() {
        // try to use loginService here
        // if I set breakpoint here I can see that loginService is null
    }

}

min tjeneste (forkortet):

package de.homer.server.config.user;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
public class LoginService {

}

min applicationContext.xml:

<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
    xsi:schemaLocation=http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd>

    <context:annotation-config />
    <context:component-scan base-package=de.homer.server.config.beans />
    <context:component-scan base-package=de.homer.server.config.user />
</beans>

min 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 xmlns:web=http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
    xsi:schemaLocation=http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
    id=WebApp_ID version=2.5>
    <display-name>homeR</display-name>

    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>


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

    <!-- JSF mapping -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map these files with JSF -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>

min ansikter-config.xml

<?xml version=1.0 encoding=UTF-8?>
<!DOCTYPE faces-config PUBLIC
    -//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN
    http://java.sun.com/dtd/web-facesconfig_1_1.dtd>
<faces-config>
    <application>
        <variable-resolver>
            org.springframework.web.jsf.DelegatingVariableResolver
        </variable-resolver>
    </application>
</faces-config>

og hvis det betyr et utdrag av min pom.xml

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>

sist men ikke minst en del av loggen:

DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner  - Identified candidate component class: file [/home/stonedsquirrel/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/homeR-server-runtime/WEB-INF/classes/de/homer/server/config/user/LoginService.class]
...
DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory  - Creating shared instance of singleton bean 'loginService'
DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory  - Creating instance of bean 'loginService'
DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory  - Eagerly caching bean 'loginService' to allow for resolving potential circular references
DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory  - Finished creating instance of bean 'loginService'

og stabelen spor:

WARNING: #{loginBean.doLogin}: java.lang.NullPointerException
javax.faces.FacesException: #{loginBean.doLogin}: java.lang.NullPointerException
at    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
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:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
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:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at   org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
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:722)
Caused by: 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)
... 24 more
Caused by: java.lang.NullPointerException
at de.homer.server.config.beans.LoginBean.doLogin(LoginBean.java:49)
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:601)
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:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 25 more

Jan 16, 2012 9:42:26 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/homer] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at de.homer.server.config.beans.LoginBean.doLogin(LoginBean.java:49)
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:601)
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:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
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:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
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:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
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:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
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:722)
Publisert på 15/01/2012 klokken 22:46
bruker
På andre språk...                            


2 svar

stemmer
1

Fjern Autowired alternativ:

Definer din service og DAO bønner her;

@Configuration
public class ApplicationConfig {

    @Bean
    public LoginService loginService() {
        return new LoginService();
    }
}

Initialiseres Bønner,

@Configuration
@Import( { PersistenceHibernateConfig.class, SecurityConfig.class, ApplicationConfig.class } )
public class SpringMainConfig{

}

Legg dette param til web.xml,

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.ecs.esr.spring.conf.SpringMainConfig</param-value>
</context-param>

Legg dette til å administrere bønne konstruktør,

WebApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
loginService = ctx.getBean(LoginService.class);
Svarte 16/01/2012 kl. 01:02
kilden bruker

stemmer
4

Jeg endelig fikk den. Det viser seg at det var et problem med en foreldet versjon av ansikter-config.xml Det fungerer med denne config:

ansikter-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>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

</faces-config>

Bean:

package de.homer.server.config.beans;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import de.homer.server.config.user.LoginService;

@Component
@Scope("session")
@Qualifier("loginBean")
public class LoginBean implements Serializable {

    @Autowired
    LoginService loginService;

    public final String doLogin() {
    // try to use loginService here
    // if I set breakpoint here I can see that loginService is null
    }

}

tjenesten:

package de.homer.server.config.user;

import org.springframework.stereotype.Service;

@Service
public class LoginService {

}

Takk for all inspirasjon!

Svarte 16/01/2012 kl. 12:44
kilden bruker

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more