0

i am using jsf2.0 as MVC framework and Spring just for Dependency Injection. i made it work but there is little problem when Spring Creating bean. Means on My JSFBean (ManagedBean) i have to use @Component Annotation of Spring otherwise i am not able to make it work. and because of that when my ManagedBean have some code in Constructor Spring is throwing Exception. It is working Perfect without Constructor CODE. Please comment if you need anything else.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myBean' defined in file [C:\Documents and Settings\KshiS\My Documents\NetBeansProjects\Sp_Js_1\build\web\WEB-INF\classes\com\ksh\excel\MyBean.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ksh.excel.MyBean]: Constructor threw exception; nested exception is java.lang.NullPointerException 

My JSF Bean code is

package com.ksh.excel;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author KshiS
 */
@ManagedBean
@ViewScoped
public class MyBean {

    @Autowired
    private Userdao userdao;
    ArrayList<String> arrayList = new ArrayList<String>();

    public MyBean()
    {
        Object object = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("Userid");
        if(object != null)
        {
            arrayList.add("Session Value");
        }
        else
        {
            arrayList.add("Temp Value");
        }
    }

    public void print()
    {
        System.out.println(userdao.print());
    }
}

How to Resolve it. ? OR Is there any possibility to Make it work without @Component Annotation on ManagedBean. ?

One More Important Question that I dont want use Spring As DI rather then i want use J2EE6 Dependency Injection. But also there is One problem That i have to use pure j2EE server like glassFish or JBOSS. Is it possible to use it in Tomcat. ? i know tomcat is not pure j2ee Server But i just want to use DI.

MRX
  • 1,611
  • 8
  • 33
  • 55

2 Answers2

0

For the first problem try adding an @Lazy annotation to your JSF bean. That will delay the creation of the object till it is required. That will resolve the problem with the exception on startup - but I think it will not fix the issue. When used with JSF the object created by spring will not be used.

Take a look at the example in this article to do it correctly - using a spring variable resolver. http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/

<?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_1.xsd"
    version="2.1">

    <application>
        <el-resolver>
                org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>

</faces-config>

On the second question - you can loook at TomEE. This article also has some details on using CDI in tomcat directly.

gkamal
  • 20,777
  • 4
  • 60
  • 57
  • Thanks for reply. but i have already seen that link. and also my faces-config.xml is looks like yours. – MRX May 27 '13 at 05:57
  • Did you try the @Lazy? You can also try @Scope("request") / @Scope("session") – gkamal May 27 '13 at 06:02
  • Yes kamal. it is working with request scope. but problem is i can not use view scope(of JSF) now. and if you know about it then ViewScope is very important for me. And also now all beans will be handle by Spring Container even my JSF beans. i want my JSFBean by JSF. – MRX May 27 '13 at 06:18
  • What happens if you don't use @Component on the JSF managed beans. The resolver is to inject dependencies into JSF components - you don't need to make them spring beans. – gkamal May 27 '13 at 06:21
  • At that time i am getting exception. Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException. – MRX May 27 '13 at 06:38
  • NullPointerException - Is that for a dependency? How are you injecting it, are you using autowired? – gkamal May 27 '13 at 06:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30669/discussion-between-kshitij-and-gkamal) – MRX May 27 '13 at 06:46
0

You need to post your full stacktrace here.

While your bean won't be injected using @AutoWired because your managed bean is not a spring managed bean (you need @ManagedProperty), that is not your primary problem.


The root cause is a failure in your bean's constructor. My guess would be that the following line is responsible for the NPE.

  Object object = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("Userid");

FacesContext.getCurrentInstance().getExternalContext().getSessionMap() will only return a non-null SessionMap if there's a valid HttpSession. Your chained method call assumes that this will always be the case, and it appears that's not true here.

After you fix that code, you can then use @ManagedProperty to inject the spring bean into your JSF bean (you don't need Spring MVC here.)

Related reading:

Community
  • 1
  • 1
kolossus
  • 20,559
  • 3
  • 52
  • 104
  • if i use @ManagedProperty that means those beans will handle by the JSF Container not by the Spring Container. i just want to make two different things Saparate by Jsf Beans should be handle by only JSF container and Spring Beans handle by Spring Container. – MRX May 28 '13 at 04:13
  • @kshitij, `@ManagedProperty` will let you use a spring bean inside a JSF bean. That means `Userdao` (which I assume is a spring bean) can be injected into the JSF bean `MyBean`. The whole point of `@ManagedProperty` is so you can inject components from different contexts. But like I said, that is not the immediate problem. Also look at the links I referenced – kolossus May 28 '13 at 04:20
  • well i like Solution with @ManagedProperty. Is there any problem if use it for Injection Spring Beans. – MRX May 28 '13 at 05:01
  • @kshitij no problems that I'm aware of. Look at the related reading and do some research to satisfy your doubts – kolossus May 28 '13 at 19:58