java - Spring Inject注解导致空指针异常

标签 java spring hibernate jsf-2

这篇文章是Declaration of beans in applicationContext.xml的连续性

我有一个使用 Spring 3 和 Hibernate 4 以及 JSF2 的小型应用程序,当我运行该应用程序时,我得到了。

java.lang.NullPointerException at
net.test.managed.bean.EmployeesManagedBean.getEmpList(EmployeesManagedBean.java:53)

我做错了什么以获得空指针异常?非常感谢任何帮助。

托管 bean :

@ManagedBean(name="empMB")
@Named
@Scope("request")
public class EmployeesManagedBean implements Serializable {

    public List<Employees> getEmpList() {
        try {
            empList = new ArrayList<Employees>();
            empList.addAll(getEmployeesService().getEmployees());
        } catch (Exception e) {
            e.printStackTrace();            
        }
        return empList;
    }
}

并且我有注入(inject)注解:

@Inject
EmployeesService employeesService;

在 EmployeesService 中,我有如下注释:

@Named
public class EmployeesService implements IEmployeesService {

@Inject
EmployeesDAO employeesDAO;

@Override
public List<Employees> getEmployees() {
    return getEmployeesDAO().getEmployees();
}

在 EmployeesDAO 中,我有如下注释:

@Named
public class EmployeesDAO implements IEmployeesDAO {


@Override
public List<Employees> getEmployees() {
    List query = this.getSessionFactory().getCurrentSession().getNamedQuery("getEmp").list();               
    return query;
}

实体类:

@Entity
@javax.persistence.NamedNativeQuery(name = "getEmp", query = "{ ? = call getemployees }", resultClass = Employees.class, hints = { @javax.persistence.QueryHint(name = "org.hibernate.callable", value = "true") })
@Table(name = "EMPLOYEES")
public class Employees {

最后在 applicationContext.xml 中有

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

更新 1:

当我使用@Component("empMB") 或@Named("empMB") 时,出现以下异常

Error creating bean with name 'empMB': Injection of autowired 
dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: net.test.employees.service.EmployeesService
net.test.managed.bean.EmployeesManagedBean.employeesService; 
nested exception is org.springframework.beans.factory.
NoSuchBeanDefinitionException: No matching bean of type
[net.test.employees.service.EmployeesService] found for 
dependency: expected at least 1 bean which qualifies as autowire 
candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}

更新 2

应用程序上下文.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

    <context:component-scan base-package="net.test" />
    <!-- Data Source Declaration -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="oracle.jdbc" />
        <property name="jdbcUrl" value="jdbc:oracle:thin:@server:1521:DEV" />
        <property name="user" value="scott" />
        <property name="password" value="tiger" />
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
    </bean> 
    <!-- Session Factory Declaration -->
    <bean id="SessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="DataSource" />     
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    <!-- Enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager" />
    <!-- Transaction Manager is defined -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>
</beans>

最佳答案

尝试使用 org.springframework.stereotype.Component 注解代替 ManagedBean,如

@Component("test")
class Test

关于java - Spring Inject注解导致空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13796698/

相关文章:

java - Android JavaMail 检测服务器是否离线

java - 如果未提供标志,如何关闭/不启动 Spring 应用程序?

hibernate - 让 Hibernate 生成外键索引

java - Spring 中 HTTP session 范围的 EntityManagerFactory

java - 为什么在托管模式下运行 GWT App Engine 应用程序时会出现 ClassNotPersistableException?

java - 如何从一个方法返回不同数据类型的多个值?

java - 模拟小型图书馆查询系统时出现NumberFormatException

java - 需要清理 Java 8 中的 ResponseEntity 主体

java - 推荐一个用于 java 和 javascript 的 JSON-RPC 库

java - 阻止 Hibernate 尝试从数据库中获取计算字段