Prototype scope with annotation

To use prototype scope with annotations, we only need to mention prototype in the @Scope annotation instead of singleton. So, our EmailService.java class will be the same, except we change the annotation value, and it will look as follows:

@Service@Scope("prototype")public class EmailService {    ...}

As like the XML example output, this will also create a new instance each time it is called. In a similar way, we can use other scopes, such as request, session, application, and global session, using XML metadata or annotations.

Get Java 9 Dependency Injection now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.