Getting correct instance using @Autowired

404 views Asked by At

I'm facing some issues when I run and hit my rest service, I saw that my service instance got a @Proxy value. Below you can see the classes that I have:

Initializer class:

public class AllureWebInitializer  implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        createDispatcherServlet(servletContext);
        initializeListener(servletContext);
    }

    private void initializeListener(ServletContext servletContext) {
        AnnotationConfigWebApplicationContext rootContext =  createContext();
        servletContext.addListener(new ContextLoaderListener(rootContext));
    }

    private void createDispatcherServlet(final ServletContext context) {
        AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
        dispatcherServlet.register(AllureWebConfig.class);

        ServletRegistration.Dynamic dispatcher = context.addServlet(AllureWebConstants.DISPATCHER, new DispatcherServlet(dispatcherServlet));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping(AllureWebConstants.SLASH);
    }

    private AnnotationConfigWebApplicationContext createContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(AllureWebConfig.class);
        return context;
    }

}

Config class:

@Configuration
@Import(AllureServiceConfig.class)
@ComponentScan(basePackages = {"com.allure.events.web.controller"})
@EnableWebMvc
public class AllureWebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private ApplicationContextProvider applicationContextProvider;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(AllureWebConstants.RESOURCES_DOUBLE_WILDCARD)
            .addResourceLocations(AllureWebConstants.RESOURCES);
    }

    @Bean
    public ApplicationContextProvider applicationContextProvider() {
        return applicationContextProvider;
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource;
        messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:META-INF/web/resources/release");
        messageSource.setUseCodeAsDefaultMessage(true);
        return messageSource;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        converter.setObjectMapper(objectMapper);
        converters.add(converter);
        super.configureMessageConverters(converters);
    }
}

Controller class:

@Controller
public class SupplierEndpoint extends AllureEndpoint {

    @Autowired
    SupplierService supplierService;

    @RequestMapping(value = AllureWebConstants.SUPPLIERS, method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<Supplier> getSuppliers() {
        List<Supplier> suppliers = getSuppliersList(supplierService.getSuppliers());
        return suppliers;
    }
}

Service interface

public interface SupplierService {

    public List<SupplierDto> getSuppliers();
}

Service Impl

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private SupplierDtoHelper supplierHelper;

    @Override
    @Transactional
    public List<SupplierDto> getSuppliers() {
        List<Supplier> suppliers = supplierMapper.getSuppliers();
        List<SupplierDto> dtos = new ArrayList<SupplierDto>();
        for (Supplier supplier : suppliers) {
            dtos.add(supplierHelper.convertEntityToDto(supplier));
        }
        return dtos;
    }

}

if I debug, I see that the instance value is :

supplierService - $Proxy28 | -> h = JdkDynamicAopProxy

Can someone guide me, please? Regards, Edgardo Quiroz

1

There are 1 answers

3
Soroosh Sarabadani On

It's not an issue when you work with Spring. Spring Framework uses Proxy to make the different features possible. Here you have a Transactional Annotation and proxy is been used to implement it.