【问题标题】:Null pointer when calling pre-instantiated bean from spring Container with JSF @managed property使用 JSF @managed 属性从 Spring Container 调用预实例化 bean 时的空指针
【发布时间】:2012-10-19 15:24:25
【问题描述】:

我正在尝试使用 @ManagedProperty 注释在 JSF ManagedBean 中使用 spring 容器中创建的 bean。但是在使用该 bean 时我得到空指针。一旦我启动我的服务器,我可以看到我的 bean 是在这里创建的

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)
 }
}
}

用户服务.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);

}

我可以看到我的服务器何时启动我尝试使用的 bean 是预先实例化的。我将 web.xml 中的 applicationContext.xml 定义为 Context-param。我正在像这样定义Spring.xml 中的所有豆类

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> 

在我的类路径中并将其作为资源导入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>

我的面孔-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>

我的方法有任何问题。

【问题讨论】:

  • 这只是一个细节;但是您的代码甚至根本无法编译。如果您无法从头开始编写可编译的代码,请考虑使用复制粘贴。这可以防止红鲱鱼。
  • @BalusC...嘿,你现在可以检查一下我更新了我的问题吗?
  • 不,春天已经超越了我。
  • 好的,谢谢。但是我想知道像你这样精通java的人怎么不进入春天?
  • 为什么选择 Spring 而不是 EJB3+CDI(标准 Java EE 堆栈提供)?我想答案基本上是一样的。

标签: jsf-2 nullpointerexception spring-3


【解决方案1】:

有很多事情应该处理得更好。

1) 你只需要&lt;context:component-scan base-package="com.test" /&gt;,去掉 annotation-config 和 AutowiredAnnotationBeanPostProcessor。

查看原因:Difference between <context:annotation-config> vs <context:component-scan>Documentation

2) 您的 UserBean 上没有范围,如果您没有提及任何范围,则默认范围将是 Singleton,我认为在这种情况下不希望这样做。

3) 您正在尝试使用接口而不是实现此接口的可实例化类。

4) 然后你应该用@Service 标记实现类以自动装配。

5) 我希望你有 getter 和 setter 而不仅仅是那些 cmets。

一个很好的例子参考这个link

另见:

【讨论】:

  • 我不知道我的代码的确切问题是什么。因为我做了你在答案中建议的任何事情。但这并不是如何使用 ManagedPropety 注释。但是当提到我的服务作为依赖项时令人惊讶地工作在 faces-config.xml 中。您认为原因可能是当我在 faces-config.xml 中定义 bean 并使用 @managedBean 注释相同时,并且该注释被 faces-config 定义抑制,因此 managedProperty 定义也不起作用?
  • Ravi...这正是发生的事情。在 faces-config.xml 中创建的 ManagedBean 直接在 bean 中抑制了带注释的 ManagedBean,这就是为什么当我执行 @ManagedPropety 时它不起作用。无论如何感谢您的宝贵建议。
  • 是的,faces-config.xml 优先且注释不起作用。另见jsf-manged-property-for-multivalues
猜你喜欢
  • 2010-11-19
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 2011-09-05
  • 2012-05-22
  • 2013-03-24
  • 2013-08-30
相关资源
最近更新 更多