1

I've the below JSF managed bean:

package com.example;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@ManagedBean
public class Bean implements Serializable {

    @Autowired
    private SomeService someService;

    public void update() {
        someService.update(someEntity);
    }

    // ...
}

And the below Spring service:

@Transactional
@Repository
public class SomeService {

    @Autowired
    private SessionFactory sessionFactory;

    // ...
}

Spring is configured as below:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <description>Example configuration to get you started.</description>

    <context:component-scan base-package="com.example" />

    <!-- To add exception translation to a template-less Hibernate DAO -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <context:annotation-config />

    <!-- Database Configuration -->
    <import resource="database/DataSource.xml" />
    <import resource="database/Hibernate.xml" />
    <tx:annotation-driven />
 </beans>

When I call the JSF managed bean update() method, it throws a NullPointerException:

javax.faces.FacesException: #{bean.update()}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:117)
    at org.springframework.faces.webflow.FlowActionListener.processAction(FlowActionListener.java:71)
    at org.springframework.faces.model.SelectionTrackingActionListener.processAction(SelectionTrackingActionListener.java:55)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
    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:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:191)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    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:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
    ... 28 more
Caused by: java.lang.NullPointerException
    at com.example.Bean.update(Bean.java:45)
    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:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 29 more

In other words, someService is null. I cannot for the life of me figure out why. I have @Component declared at the top of my bean. So I expect @Autowired to just work. Any help will be great!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
MrTimotheos
  • 937
  • 1
  • 10
  • 18
  • Can you post your configuration file? Are you using component-scan? – Sean Carroll May 05 '14 at 01:00
  • I am using component: scan. When I try another class that is in the same package, I do not get a null pointer.I have posted my config file. – MrTimotheos May 05 '14 at 01:51
  • And you also annotated FilmBo with @Component? – Sean Carroll May 05 '14 at 01:56
  • No I have not. However, I tried that and it did not solve the probelm. – MrTimotheos May 05 '14 at 02:47
  • 1
    Also please post the class of the calling code. – jordan May 05 '14 at 02:47
  • There you go, I have posted the code. The method is at the bottom. Don't mind the mix of username and city. This is jsut a test query to see if it registers with my form. – MrTimotheos May 05 '14 at 02:58
  • I meant the code calling "TextEntryIndicatorBean". Is that class also a spring-managed bean? Or are you manually creating any of these classes? – jordan May 05 '14 at 03:00
  • possible duplicate of [Why is my Spring @Autowired field null?](http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – chrylis -cautiouslyoptimistic- May 05 '14 at 03:02
  • Oh. It is a JSF page. I have psoted the code for it. – MrTimotheos May 05 '14 at 03:02
  • @chrylis It is not as I am not using Spring MVC, I am using JSF managed beans. – MrTimotheos May 05 '14 at 03:11
  • I am not faimilar with JSF. What configuration have you done to get the JSF file use the spring bean? Ive read something about putting the following in your "faces-context.xml" file: org.springframework.web.jsf.el.SpringBeanFacesELResolver – jordan May 05 '14 at 03:14
  • You provided FilmDAO but not FilmBo which is what you are using in TextEntryIndicatorBean. If you want to have FilmBo picked up for component-scan (autowiring) you will need to annotated it. – Sean Carroll May 05 '14 at 03:17
  • Well I can navigate to another bean that is using filmbo and it intialises it just fine. it's something with this particular bean. – MrTimotheos May 05 '14 at 03:23
  • I think the answer you're looking for can be found in [How to inject service in JSF managed bean without using Spring IOC](http://stackoverflow.com/questions/14468770/how-to-inject-service-in-jsf-managed-bean-without-using-spring-ioc). In short, though - your injection is not working because the lifecycle of TextEntryIndicatorBean is managed by JSF rather than the Spring container. – Steve C May 05 '14 at 03:26
  • My writeup isn't about Spring MVC, it's about Spring IoC, which you *are* attempting to use. Even though you annotated your class with `@Component`, the instance being used is managed by JSF, not Spring, and the problem is the same. Use `@Configurable`. – chrylis -cautiouslyoptimistic- May 05 '14 at 03:49

1 Answers1

1

You cannot mix annotations and contexts like you're attempting here.

First, to inject a Spring bean (or really, any bean) into a JSF managed bean, use the @ManagedProperty annotation. So, if everything else is setup properly (Spring-wise, including the Spring Faces EL resolver), you should have:

@ManagedProperty("#{someService}")
private SomeService someService;

When you declare a class as a @ManagedBean, it's outside the context of Spring. As a result, @Autowired has no power here; spring cannot inject anything into anything that's outside of its context.

If you want to use the pair of @Component and @Autowired, you need to remove @ManagedBean so that it becomes a Spring managed bean instead of a JSF managed bean.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
kolossus
  • 20,559
  • 3
  • 52
  • 104