0

I am working with spring boot, In Controller, I tried to create an object using new operator, And tried to print object value its working.but for repository I used @Autowired but resulted in null

@RestController
public class SignupController {
    //@Autowired
    UserService userservice =new UserService() ;

    @GetMapping("/Allusers")
    public ResponseEntity<List<User>> allUsers() {
        System.out.print(userservice+"\n");
        List<User> userlist = userservice.getAllUsers();
        if (userlist.isEmpty()) {

            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity<>(userlist, HttpStatus.OK);
    }

This is service class in which i Auto-wired "UserRepository userrepo;"

public class UserService {
     @Autowired
    UserRepository userrepo;

    public List<User> getAllUsers() {
        List<User> userlist = new ArrayList<User>();
        System.out.println(userrepo);
        userrepo.findAll().forEach(users -> userlist.add(users));
        System.out.println("hii");
        return userlist;
    }
Romil Patel
  • 12,879
  • 7
  • 47
  • 76

2 Answers2

1

Add the @Service annotation to UserService class and also a constructor to the same class. Like shown below;

@Service
public class UserService {

    private final UserRepository userrepo;

    public UserService(UserRepository userRepository) {
        this.userrepo = userRepository;
    }

    public List<User> getAllUsers() {
        List<User> userlist = new ArrayList<User>();
        System.out.println(userrepo);
        userrepo.findAll().forEach(users -> userlist.add(users));
        System.out.println("hii");
        return userlist;
    }
}

If you want to use @Autowired annotation, make sure that UserRepository class is annotated with @Service or @Controller or @Component. This is needed because, we need to say that the object is a Spring managed component.

Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
cresclux
  • 76
  • 3
0

Add @Service in UserService class and make sure UserRepository have @Repository. OR

    @Service
    public class UserService {

        private final UserRepository userRepository;

        public UserService(UserRepository userRepository) {
            this.userRepository= userRepository;
        }

        //your code
    }

Difference between new & @Autowired

@Autowired private UserDao userdao;

private UserDao userDao = new UserDaoImpl();

Performance

new

With the new approach, you get a new object every time, whether you want to or not. Even if UserDaoImpl is reusable, stateless and threadsafe (which DAO classes should strive to be) you will still create new instances of them at every place they are needed.

Consider a situation where the UserDaoImpl has implemented at multiple places which needs a Hibernate session, which needs a DataSource, which needs a JDBC connection - it quickly becomes a lot of objects that has to be created and initialized over and over again.

@Autowired

Springs dependency injection costs a little more in startup-time as the container has to be initialized and all managed objects set up. However, since you won't be creating new instances of your managed objects (assuming that is how you design them), you'll gain some runtime performance from less GC load and less object creation.

Scope

Another important thing is with spring annotation even you can manage object scope, but with new keyword no way(unless you do dumb singleton or something like that), I am sure with a new keyword you cannot have Prototype, Request, Single

Code Maintiblity

Consider 100 classes that uses UserDao class, And you get dao instance like that: private UserDao userDao = new UserDaoImpl(); in 100 places. Now consider a requirement changed and we need to use SomeOtherUserDao as class LdapUserDao implements UserDao, How we will handle new keywords, we tied impl class into usage.

Romil Patel
  • 12,879
  • 7
  • 47
  • 76