0

I am trying to use the bean created in the spring Container in the JSF ManagedBean using @ManagedProperty annotation.But I am getting null pointer when using that bean.Once I start my server I can see my beans are created Here

Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9d532ae: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,userBean,userService];

HomepageBean.java

package come.test.backingbean

 @ManagedBean
    @sessionScoped

        public Class HomepageBean{

        @ManagedProperty(value="#{userBean}")
        private UserBean userBean;// getters and setters


       public String doLogin() {
            String url = "login.xhtml";
            LoginBean manager = new LoginBean();  // This bean has a condition which check for Username and password entered by user.
            if (manager.auth(username, password)) {
                isLoggedIn = true;
                url = "homepage";
                String username=sample;
                userBean.getUserInfo(username);
            } else {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(username, new FacesMessage(
                        "Invalid Username and or Password"));
            }
            return url;
        }

UserBean.java

package com.test.mypackage

@Component
Public Class UserBean{

@Autowired
private UserService userServie  // getters and setters.

     public void getUserInfo(String userId){
      userService.findByUserId(userId)
 }
}
}

UserService.java

package com.test.service;

public interface UserService {

    public void save(User User);
    public void update(User user);
    public void delete(User user);
    public User findByUserId(String userId);

}

I can see when my server started the bean I am trying to use is pre-instantiated.I am defining my applicationContext.xml in web.xml as a Context-param. And I am defining all the beans in my Spring.xml like this

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config />

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

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

</beans> 

which is in my class path and importing that as a resource in applicationContext.xml.

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <import resource="classpath:database/DataSource.xml" />

    <import resource="classpath:database/Hibernate.xml" />

   <import resource="classpath:config/Spring.xml" />

</beans>

my faces-confi.xml

<application>
   <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    <resource-bundle>
        <base-name>com.test.boundles.messages</base-name>
        <var>msg</var>
    </resource-bundle>
    </application>

Any problem with my approach.

SRy
  • 2,901
  • 8
  • 36
  • 57
  • This is just a detail; but your code doesn't even compile at all. If you're unable to write compilable code from top of head, consider copypasting instead. This prevents red herrings. – BalusC Oct 19 '12 at 16:00
  • @BalusC...Hey can you check now I updated my question? – SRy Oct 19 '12 at 17:33
  • No, Spring is beyond me. – BalusC Oct 19 '12 at 17:40
  • Ok Thanks.But I wonder how a java savvy like you not into the spring? – SRy Oct 19 '12 at 17:48
  • 1
    Why did *you* choose Spring over EJB3+CDI (as offered by standard Java EE stack)? I think the answer is basically the same. – BalusC Oct 19 '12 at 18:03

1 Answers1

1

There are multiple things that should have been handled better.

1) You just need <context:component-scan base-package="com.test" />, remove annotation-config and AutowiredAnnotationBeanPostProcessor.

See Reason: Difference between <context:annotation-config> vs <context:component-scan> and Documentation

2) You do not have scope on your UserBean, if you do not mention any scope the default scope will be Singleton which I do not think is desirable in this context.

3) You are trying to use an interface instead of an instantiable class that implements this interface.

4) You should then mark the implementation class with @Service to be autowired.

5) I hope you have getters and setters instead of just those comments.

For a good example refer to this link

See also:

Community
  • 1
  • 1
Ravi Kadaboina
  • 8,494
  • 3
  • 30
  • 42
  • I don't know what's the exact problem with my code.Cause I did whatever u suggested in the answer.But it's not some how working with ManagedPropety annoatation.But surprisingly working when mentioned my service as dependency in the faces-config.xml.Do u think reason might be when i define bean in faces-config.xml and annotate the same with @managedBean, and this annotation suppressed by faces-config definiton so that managedProperty definiton is also not working? – SRy Oct 20 '12 at 03:55
  • Ravi...That's what exactly happening.ManagedBean created in the faces-config.xml is supperssing the annoated ManagedBean directly in the bean that's why when i do @ManagedPropety it's not working.Anyway thanks for valuable suggestions. – SRy Oct 20 '12 at 04:06
  • Yes, faces-config.xml takes precedence and annotations do not work. See also [jsf-manged-property-for-multivalues](http://stackoverflow.com/questions/8067100/jsf-manged-property-for-multivalues) – Ravi Kadaboina Oct 20 '12 at 04:39