Java spring nullpointerexception-无法访问无注释类中的自动连线注释服务(或dao)

Java spring nullpointerexception-无法访问无注释类中的自动连线注释服务(或dao),java,spring,spring-mvc,annotations,autowired,Java,Spring,Spring Mvc,Annotations,Autowired,我有一个我无法解决的问题 从我的@控制器,我可以轻松访问我的autowired@服务类并毫无问题地使用它。 但是,当我从一个没有注释的单独类中执行此操作时,它会给我一个NullPointerException 我的控制员(工作)— 我的单独Java类(不工作)- 或 userService或userDao始终为空! 我只是想看看其中有没有一个有效 我的组件扫描设置已将根级别的包设置为扫描,因此应该可以 我的servlet上下文- <?xml version="1.0" encoding

我有一个我无法解决的问题

从我的
@控制器
,我可以轻松访问我的autowired
@服务
类并毫无问题地使用它。 但是,当我从一个没有注释的单独类中执行此操作时,它会给我一个
NullPointerException

我的控制员(工作)—

我的单独Java类(不工作)-

userService或userDao始终为空! 我只是想看看其中有没有一个有效

我的组件扫描设置已将根级别的包设置为扫描,因此应该可以

我的servlet上下文-

<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- the application context definition for the
         springapp DispatcherServlet -->
<!-- Enable annotation driven controllers, validation etc... -->

<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="x" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />

    <!-- package shortended -->
<bean id="messageSource"
class="o.s.c.s.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages" />
</bean>

<bean  id="dataSource" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>

    <!-- package shortened -->
<bean id="viewResolver"
    class="o.s.w.s.v.InternalResourceViewResolver">

    <property name="prefix">
        <value>/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
    <property name="order">
        <value>0</value>
    </property>
</bean>

      <!-- package shortened -->
  <bean id="sessionFactory" 
      class="o.s.o.h3.a.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
    <list>
        <value>orion.core.models.Question</value>
        <value>orion.core.models.User</value>
        <value>orion.core.models.Space</value>
        <value>orion.core.models.UserSkill</value>
        <value>orion.core.models.Question</value>
        <value>orion.core.models.Rating</value>
    </list>
    </property>
        <property name="hibernateProperties">

        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        </props>
        </property>
</bean>

<bean id="hibernateTransactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

/
.jsp
0
猎户座,核心,模型,问题
orion.core.models.User
猎户座。核心。模型。空间
orion.core.models.UserSkill
猎户座,核心,模型,问题
orion.core.models.Rating
${hibernate.dial}
${hibernate.show_sql}
${hibernate.hbm2ddl.auto}

有什么线索吗?

来自

默认情况下,使用
@组件
@存储库
@服务
@Controller
,或自定义注释 这本身就是用
@组件
是唯一检测到的组件 候选组件


UsersManagementUtil
应该根据您的需要使用其中一个进行注释。

Spring依赖项注入只在Spring管理的组件中工作。如果您的
UsersManagementUtil
不是由Spring管理的(即不是Springbean),
@Autowired
在其中不起作用。或者将其声明为Springbean(使用
或注释),或者在使用

applicationContext.getAutowireCapableBeanFactory().autowireBean(object);

这也是我所理解的,但并不适用于我@检测到组件,但自动连线bean仍然为null。。没有其他错误UserService呢,spring是被管理的吗?(在xml中注释或定义)的可能副本
@Autowired
UserDao userDao;
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- the application context definition for the
         springapp DispatcherServlet -->
<!-- Enable annotation driven controllers, validation etc... -->

<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="x" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />

    <!-- package shortended -->
<bean id="messageSource"
class="o.s.c.s.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages" />
</bean>

<bean  id="dataSource" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>

    <!-- package shortened -->
<bean id="viewResolver"
    class="o.s.w.s.v.InternalResourceViewResolver">

    <property name="prefix">
        <value>/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
    <property name="order">
        <value>0</value>
    </property>
</bean>

      <!-- package shortened -->
  <bean id="sessionFactory" 
      class="o.s.o.h3.a.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
    <list>
        <value>orion.core.models.Question</value>
        <value>orion.core.models.User</value>
        <value>orion.core.models.Space</value>
        <value>orion.core.models.UserSkill</value>
        <value>orion.core.models.Question</value>
        <value>orion.core.models.Rating</value>
    </list>
    </property>
        <property name="hibernateProperties">

        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        </props>
        </property>
</bean>

<bean id="hibernateTransactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
applicationContext.getAutowireCapableBeanFactory().autowireBean(object);