spring-nullpointerexception- cant access autowired annotated service (or dao) in a no-annotations class

15,588

Solution 1

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.

Solution 2

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);
Share:
15,588
user286806
Author by

user286806

Updated on August 09, 2022

Comments

  • user286806
    user286806 over 1 year

    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?

  • user286806
    user286806 over 13 years
    this is what i understood as well but was not working for me.. @Component gets detected but the autowired bean is still null.. no error otherwise
  • Michael Wiles
    Michael Wiles over 13 years
    What about UserService, is that spring managed? (annotated or defined in your xml)