title: How to Autowire business objects in Spring
body: <p>I'm following the Controller -> Service -> DAO pattern. When I call a DAO implementation, I get back a DTO/Data object. Which then gets passed to the service layer, bringing together it's respective business object and it's data object. Like so:</p>
  
  <pre><code>public User getUserById(int id) {
      return new User(userDAO.getUserById(id));
  }
  
  class User {
      private UserDTO userDTO;
  
      public User(UserDTO userDTO) {
          this.userDTO = userDTO;
      }
  }
  </code></pre>
  
  <p>What I'd like to do is wrap ALL my business logic inside the business class but I require additional dependencies.
  For example, I'd like to be able to do something like this:</p>
  
  <pre><code>//... some code
  
  User user = userService.getByUserId(1);
  
  user.delete(); // this should delete the user from the database
  </code></pre>
  
  <p>In order for me to delete the user this way, I would need to Autowire the UserService into the business class but this will not work since I am manually instantiating the User class in the User Service.</p>
  
  <pre><code>class User {
      @Autowired
      private UserService userService; // this won't work since I call instantiate User myself, ie. new User()
      private UserDTO userDTO;
  
      public User(UserDTO userDTO) {
          this.userDTO = userDTO;
      }
  
      public boolean delete() {
          userService.deleteByUserId(userDTO.getId());
      }
  }
  </code></pre>
  
  <p>Is there a pattern I can follow to allow me to do what I want?</p>