Spring注入导致nullpointer异常的注释

时间:2012-12-10 07:30:59

标签: java spring hibernate jsf-2

这篇文章是Declaration of beans in applicationContext.xml

的连续性

我有一个使用Spring 3的小应用程序和带有JSF2的Hibernate 4,当我运行我正在运行的应用程序时。

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

我错误地做了什么来获得 nullpointer异常?任何帮助都非常值得赞赏。

ManagedBean:

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

的applicationContext.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>

2 个答案:

答案 0 :(得分:2)

尝试使用org.springframework.stereotype.Component注释而不是ManagedBean,

@Component("test")
class Test

答案 1 :(得分:1)

我设法解决了获取空指针异常的问题,礼貌 Marten Deinum

我正在做的错误是DAO类没有 @Inject ,我已将DAO类修改为

@Named
public class EmployeesDAO implements IEmployeesDAO {

    @Inject
    private SessionFactory sessionFactory;

    @Override
    public List<Employees> getEmployees() {

             List query = sessionFactory.getCurrentSession().getNamedQuery("getEmp").list();

        return query;
    }
}

在ManagedBean中,我已经做了Daniel提到的更改,使用 @Named 而不是 @ManagedBean 。修改后的ManagedBean

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

    @Inject
    IEmployeesService employeesService;

当然在 applicationContext.xml 中添加以下内容来扫描实体类

<property name="annotatedClasses">  
          <list>
           <value>net.test.model.Employees</value>
           </list>  
           </property>