Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 使用JSF@managed属性从spring容器调用预实例化bean时的空指针_Jsf 2_Nullpointerexception_Spring 3 - Fatal编程技术网

Jsf 2 使用JSF@managed属性从spring容器调用预实例化bean时的空指针

Jsf 2 使用JSF@managed属性从spring容器调用预实例化bean时的空指针,jsf-2,nullpointerexception,spring-3,Jsf 2,Nullpointerexception,Spring 3,我试图使用JSF ManagedBean中的spring容器中创建的bean,使用@ManagedProperty注释。但是使用该bean时,我得到了空指针。一旦启动服务器,我可以看到我的bean在这里创建 Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9d532ae: defining beans [org.springframework.

我试图使用JSF ManagedBean中的spring容器中创建的bean,使用
@ManagedProperty
注释。但是使用该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)
 }
}
}
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);

}
我可以看到我的服务器在何时启动我尝试使用的bean是预先实例化的。我正在
web.xml
中将我的
applicationContext.xml
定义为
上下文参数。我在
Spring.xml
中定义了所有bean,如下所示

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

org.springframework.web.jsf.el.SpringBeanFacesELResolver
com.test.boundles.messages
味精

我的方法有任何问题。

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

1) 您只需要
,删除注释配置和AutowiredNotationBeanPostProcessor

见理由: 及

2) 您的UserBean上没有作用域,如果您不提及任何作用域,那么缺省作用域将是Singleton,我认为在本文中这是不可取的

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

4) 然后,您应该将带有@Service的实现类标记为自动连接

5) 我希望你有能手和二传手,而不仅仅是那些评论

关于一个很好的例子,请参考此

另见:


这只是一个细节;但是你的代码根本就没有编译。如果无法从顶部写入可编译代码,请考虑使用CopyPress。这可以防止误会。@BalusC…嘿,你能检查一下我现在更新了我的问题吗?不,Spring超出了我的能力范围。好的,谢谢。但我想知道像你这样的java高手怎么没有进入Spring?为什么你选择Spring而不是EJB3+CDI(由标准java EE堆栈提供)?我想答案基本上是一样的。我不知道我的代码到底有什么问题。因为我按照你在答案中的建议做了。但这并不是如何使用ManagedProperty注释。但令人惊讶的是,当在faces-config.xml中提到我的服务作为依赖项时,它居然可以工作。你认为原因可能是当我在中定义bean时吗faces-config.xml并用@managedBean注释相同的内容,这个注释被faces-config-definiton抑制,所以managedProperty定义也不起作用?Ravi…这就是实际发生的情况。在faces-config.xml中创建的ManagedBean直接在bean中支持注释的ManagedBean,这就是为什么我在@managedProperty时它不起作用。无论如何,感谢您的宝贵帮助建议。是的,faces-config.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>