1

I'm trying to do a simple appliaction that saves a user using Spring and Hibernate. The problem is that the controller object instance is null (I'm using @Autowired).

I've tried to create a SpringUtils file, set a Scope, set @ComponentScan, but nothing worked.

Spring Main Application

@SpringBootApplication
@EnableJpaRepositories
@ComponentScan({"com.springprojectdao","com.springprojectc.controller"})
@EntityScan("com.springprojectentity")
public class SpringProjectApplication {
    @Autowired
    static UserController userController; //it's null

    public static void main(String[] args) {
        SpringApplication.run(SpringProjectApplication.class, args);        
        userController.createUser();        
    }
}

Controller

@Controller
public class UserController {
    @Autowired
    private UserDAO userDAO;
    //private UserVO user;

    public UserVO initUser() {
        UserVO user = new UserVO();
        user.setUsername("Charly");
        user.setPassword("alamo8");
        user.setPhone("654789321");
        return user;
     }  

    //@Bean
    public UserVO createUser() {
        UserVO user = initUser();
        userDAO.save(user);
        return null;
    }
}

DAO

@Repository
public interface UserDAO extends JpaRepository<UserVO,String> {     

}


@Service
public class UserDAOImpl implements UserDAO {

    @Override
    public <S extends UserVO> S save(S entity) {
    // TODO Auto-generated method stub      
        save(entity);
        return null;

     }
}

SpringUtils

@Component
public class SpringUtils {


    public static ApplicationContext ctx;

    /**
     * Make Spring inject the application context
     * and save it on a static variable,
     * so that it can be accessed from any point in the application. 
     */
    @Autowired
    private void setApplicationContext(ApplicationContext 
       applicationContext) {
        ctx = applicationContext;       
    }

}

This is the error I get:

Exception in thread "main" java.lang.NullPointerException at com.springproject.SpringProjectApplication.main(SpringProjectApplication.java)

Stevy
  • 3,228
  • 7
  • 22
  • 38
  • 5
    spring cannot auto wire static fields – Ryuzaki L Jul 26 '19 at 11:56
  • Instead of a static variable, why not just make a singleton scoped bean? That's assuming that the reason you want a static variable is so that you want shared values among whatever is using your UserController. – Molasses Jul 26 '19 at 12:25

1 Answers1

2

In Springframework, we can't make it a Spring bean with a static variable. Because the Spring context has not been loaded when the class loader loads static variables. So the class loader does not properly inject static classes into the bean and fails.

    @Autowired
    static UserController userController;
congco
  • 21
  • 1
  • How should I initialize the object then? It gives me error if I use any other prefix – Joan Losa Morlá Jul 26 '19 at 12:37
  • stead of a static variable, why not just make a singleton scoped bean? That's assuming that the reason you want a static variable is so that you want shared values among whatever is using your UserController. – Molasses Aug 15 '19 at 19:39