Give the container the keys, he’ll give you flexibility
The container creates, provides and destroys the components
careful with new for components in the code.
@Bean/@Produces : Method used for building complex beans of a given type
@Autowired/@Inject : Where to inject what (attribute, parameter)
@Scope : Life of beans (singleton, request, prototype, etc.)
Manage them all : The framework launches the application to manage everything
Wrap them all : Wrappers (proxies, decorators, facades) are injected rather than our actual objects
Composition over inheritance
Automatic DAO / DAL
@Entity
public class User {
@ID String email;
String firstname;
String lastname;
}
Basic operations on repositories are very simple to code
public interface UserRepository extends CrudRepository<User, String> {
Page<User> findByFirstnameAndLastname(String fname,
String lname, Pageable pageable);
}
Transport code is transparent
@RestContoller
public class UserContoller {
@Autowired
protected UserRepository repository;
@GetMapping("/users")
public Page<User> findUsers(
@QueryParam("firstname") String firstname,
@QueryParam("lastname") String lastname,
Pageable pageable) {
return repository
.findByFirstnameAndLastname(firstname, lastname, pageable);
}
}
can only inject what is known : careful with new
can only inject into what is known
default scope is singleton
request scope is stored on ThreadLocal : careful with parallel streams in Java 8+