当我运行并点击休息服务时,我遇到了一些问题,我看到我的服务实例有一个 @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/