使用@Autowired获取正确的实例

时间:2015-06-09 20:19:01

标签: java spring autowired spring-annotations

当我跑步并点击我的休息服务时,我遇到了一些问题,我看到我的服务实例获得了@Proxy值。您可以在下面看到我的课程:

初始化程序类:

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;
    }

}

配置类:

@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
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;
    }
}

服务界面

public interface SupplierService {

    public List<SupplierDto> getSuppliers();
}

服务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;
    }

}

如果我调试,我看到实例值是:

supplierService - $ Proxy28  |   - &GT; h = JdkDynamicAopProxy

有人可以指导我吗? 问候, Edgardo Quiroz

1 个答案:

答案 0 :(得分:1)

使用Spring时,这不是问题。 Spring Framework使用Proxy来实现不同的功能。 这里有一个Transactional Annotation和代理用于实现它。