java - 使用@Autowired获取正确的实例

标签 java spring autowired spring-annotations

当我运行并点击休息服务时,我遇到了一些问题,我看到我的服务实例有一个 @Proxy 值。您可以在下面看到我的类(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;
    }

}

配置类:

@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 类:

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

服务接口(interface)

public interface SupplierService {

    public List<SupplierDto> getSuppliers();
}

服务实现

@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 | -> h = JdkDynamicAopProxy

有人可以指导我吗? 问候, 埃德加多·基罗斯

最佳答案

当您使用 Spring 时,这不是问题。 Spring框架使用Proxy来使不同的功能成为可能。 这里有一个事务注释,并使用代理来实现它。

关于java - 使用@Autowired获取正确的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30742044/

相关文章:

java.lang.NumberFormatException : For input string: "1538956627792"

java - Spring Boot : Unable to read resources in lib/*. jar 使用 lib/*.jar 中的类

java - 如何优化hibernate循环中的方法调用?

java - 集成测试中 JsonDeserializer 的 Spring Boot Autowiring

tomcat - 打开 2 个网页时启动多个 Web 服务实例

java - 在 hibernate 中制作示例程序

Java 并发 - 发布不可变对象(immutable对象)(Java 并发实践)

java - 如何在 Mac 上使用 Jib 对 Java 应用程序进行 docker 化

java - Spring 非阻塞 Rest "Send and forget"

java - Spring:@Component 与 @Bean