托管属性(服务)上的NullPointer异常

时间:2016-11-04 15:48:53

标签: java spring

我在我的应用上面临一个空指针异常,我用@Repository注释Dao,@Service提供服务,@Controller控制器和@ManagedProperty内的服务我怀疑我的应用程序上下文没有很好地配置,所以这里有:

 <?xml version="1.0" encoding="UTF-8"?>

<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"
    xml:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <!-- Enable Spring Annotation Configuration -->

    <context:annotation-config/> 
    <!-- Scan for all of Spring components such as Spring Service -->
    <context:component-scan base-package="com.domain.nameOfapp.*" />

    <!-- Necessary to get the entity manager injected into the factory bean -->
    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <!-- Define Hibernate JPA Vendor Adapter -->
    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
    </bean>

    <!-- Entity Manager Factory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="jpa-persistence" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="packagesToScan">
            <list>
                <value>com.domain.nameOfapp.*</value>
            </list>
        </property>
    </bean>

    <!-- Transaction Manager -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <!-- Detect @Transactional -->
    <tx:annotation-driven transaction-manager="transactionManager"  />

</beans>

任何帮助都会很棒!感谢

1 个答案:

答案 0 :(得分:0)

您的组件扫描需要如下所示

<context:component-scan base-package="com.domain.nameOfapp" />

这将扫描此包下的所有类,包括任何子包。

还使用@ManagedProperty,您是否尝试自动装配spring服务bean?我相信你应该使用@Autowired