【问题标题】:Trouble spring injection in JSF2 Bean with annotation带有注释的JSF2 Bean中的弹簧注入问题
【发布时间】:2015-03-28 11:24:30
【问题描述】:

我的 Web 项目中的 Spring Injection 有问题。我必须在 JSF2 bean 中使用。

展示我的作品:

SgbdServiceImpl.java(短路)

@Service
public class SgbdServiceImpl implements SgbdService {


    @Override
    public List<Sgbd> findAll() {
        return null;
    }

    @Override
    public Sgbd findOneByName(String nom) {
        return null;
    }

}

SgbdBean

@Component
@SessionScoped
@ManagedBean(name="sgbd")
public class SgbdBean {

    @Autowired
    SgbdService sgbdService;

    public List<Sgbd> findAll(){
        return sgbdService.findAll();
    }

}

我把这个配置放在文件里:web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

applicationContext.xml中的这个Spring配置:

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

    <context:annotation-config />
    <context:component-scan base-package="main.java.com.erdf.agir.services" />

</beans>

而且,在 faces-config.xml

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

我想从服务中调用 findAll(),但我一直从 sgbdService 属性获取 nullPointerException(Autowired 失败?)

我按照这个例子:http://rsuna.blogspot.fr/2013/05/how-to-integrate-jsf-20-with-spring-3.html

我错过了什么吗?

【问题讨论】:

标签: spring jsf-2


【解决方案1】:

你有上下文冲突;在同一个类定义上同时使用@Component@ManagedBean 将您的bean 置于两个上下文中:JSF 和Spring。现在让我们确定@Autowired 在JSF 上下文中不起作用。

  1. 您可以摆脱基于弹簧的注释并使用以 JSF 为中心的设置。这会给你留下什么

    @SessionScoped
    @ManagedBean(name="sgbd")
    public class SgbdBean {
    
    @ManagedProperty(value="#{sgbdServiceImpl}")
    SgbdService sgbdService;
    
    public List<Sgbd> findAll(){
        return sgbdService.findAll();
    }
    

    }

  2. 您可以坚持严格以弹簧为中心的方法

    @Component
    public class SgbdBean {
    
    @Autowired
    SgbdService sgbdService;
    
        public List<Sgbd> findAll(){
            return sgbdService.findAll();
        }
    
    }
    

【讨论】:

  • 谢谢,效果更好。我在 /WEB-INF/applicationContext.xml 中看到另一个错误。不是/WEB-INF,而是直接WEB-INF。
【解决方案2】:

我遇到的事情

1) 最好在注释中提及服务名称 - @Service("sgbdService")

2) 最好使用@Qualifier 注解,而不是@ManagedBean - @Qualifier("sgbdBean")

3) 在 applicationContext.xml 文件中添加&lt;context:sping-configured/&gt; 条目

4) 尝试使用顶级组件扫描条目作为 &lt;context:component-scan base-package="main.java.com.erdf" /&gt;

【讨论】:

  • 感谢您的快速回答。1) 我为我的服务使用名称。 2)我使用限定符但在视图文件(Xhtml)中没有发生获取bean。 3) 为什么使用 AOP 配置? 4) 完成了。
猜你喜欢
  • 2018-03-13
  • 1970-01-01
  • 2011-10-16
  • 2011-10-17
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
相关资源
最近更新 更多