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

发布于 2024-10-05 01:38:52 字数 3908 浏览 4 评论 0原文

我有这个问题无法解决。

从我的 @Controller 中,我可以轻松访问我的自动装配的 @Service 类并毫无问题地使用它。 但是,当我在没有注释的单独类中执行此操作时,它会给我一个 NullPointerException 异常。

我的控制器(工作)-

@Controller
 public class UserController {
 @Autowired
 UserService userService;...

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

public final class UsersManagementUtil {
  @Autowired
  UserService userService;

@Autowired
UserDao userDao;

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>

有什么线索吗?

I have this problem that I cannot fix.

From my @Controller, i can easily access my autowired @Service class and play with it no problem.
But when I do that from a separate class without annotations, it gives me a NullPointerException.

My Controller (works)-

@Controller
 public class UserController {
 @Autowired
 UserService userService;...

My separate Java class (not working)-

public final class UsersManagementUtil {
  @Autowired
  UserService userService;

or

@Autowired
UserDao userDao;

userService or userDao are always null!
Was just trying if any one of them works.

My component scan setting has the root level package set for scanning so that should be OK.

my servlet context -

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

Any clue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一生独一 2024-10-12 01:38:52

来自 Spring 参考3.0

默认情况下,类注释为
@Component@Repository@Service
@Controller,或自定义注释
它本身被注释为
@Component 是唯一检测到的
候选组件。

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

From Spring Reference 3.0

By default, classes annotated with
@Component, @Repository, @Service,
@Controller, or a custom annotation
that itself is annotated with
@Component are the only detected
candidate components.

UsersManagementUtil should be annotated with one of them based on your need.

远昼 2024-10-12 01:38:52

Spring 依赖注入仅适用于 Spring 管理的组件。如果您的UsersManagementUtil 不是由Spring 管理的(即不是Spring bean),则@Autowired 在其中不起作用。将其声明为 Spring bean(使用 或注释),或者在对象实例化后手动触发自动装配

applicationContext.getAutowireCapableBeanFactory().autowireBean(object);

Spring dependency injection works only in components managed by Spring. If your UsersManagementUtil is not managed by Spring (i.e. is not a Spring bean), @Autowired doesn't work inside it. Either declare it as a Spring bean (using <bean> or annotation), or trigger autowiring manually after instantiation of the object using

applicationContext.getAutowireCapableBeanFactory().autowireBean(object);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文