An example of CDI

When we use CDI, this is what the annotations on the different classes would look like. There is no change in how we create and launch the Spring application context.

CDI marks no differentiation between @Repository, @Controller, @Service, and @Component. We use @Named instead of all the preceding annotations.

In the example, we use @Named for DataServiceImpl and BusinessServiceImpl. We use @Inject to inject dataService into BusinessServiceImpl (instead of @Autowired):

    @Named //Instead of @Repository     public class DataServiceImpl implements DataService     @Named //Instead of @Service     public class BusinessServiceImpl {        @Inject //Instead of @Autowired        private DataService dataService;

Get Mastering Spring 5.0 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.